Module org.jnetpcap

Class PcapUtils

java.lang.Object
org.jnetpcap.util.PcapUtils

public final class PcapUtils extends Object
Utility methods for jNetPcap library.
Author:
Sly Technologies Inc, repos@slytechs.com, mark
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Parses a hex string to a byte array.
    static String
    toAddressString(byte[] array)
    Format an array as either IP ver 4, IP ver 6 or MAC address.
    static String
    toHexCurleyString(byte[] array)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within curley brackets {}.
    static String
    toHexCurleyString(byte[] array, int offset, int length)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within curley brackets {}.
    static String
    Formats individual bytes in a ByteBuffer to 2 digit hex values, each separated by ':' character enclosed within square brackets [].
    static String
    toHexSquareString(byte[] array)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within square brackets [].
    static String
    toHexSquareString(byte[] array, int offset, int length)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within square brackets [].
    static String
    Formats individual bytes in a ByteBuffer to 2 digit hex values, each separated by ':' character enclosed within square brackets [].
    static String
    toHexString(byte[] array)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character.
    static String
    toHexString(byte[] array, int offset, int length)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character.
    static String
    toHexString(byte[] array, int offset, int length, String prefix, String postfix)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character.
    static String
    toHexString(byte[] array, String prefix, String postfix)
    Formats individual bytes in an array to 2 digit hex values, each separated by ':' character.
    static String
    Formats individual bytes in a ByteBuffer to 2 digit hex values, each separated by ':' character.
    static String
    toIp4AddressString(byte[] array)
    Formats the given array as a IP ver 4 address in the form of 192.168.1.1.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • parseHexString

      public static byte[] parseHexString(String hexString)
      Parses a hex string to a byte array. Any whitespace and ':' characters are allowed and ignored.
      Parameters:
      hexString - the hex input string with characters in range [0-9a-fA-f\\s:]
      Returns:
      the byte[] comprised of the hex values parsed from the input string
    • toAddressString

      public static String toAddressString(byte[] array)
      Format an array as either IP ver 4, IP ver 6 or MAC address. If array length is 4 bytes, the address is treated as an IP ver 4 address. Otherwise both MAC and IP ver 6 addresses are formatted with a ':' separator and a 2 digit hex string.
      Parameters:
      array - the address
      Returns:
      the formatted string
    • toHexCurleyString

      public static String toHexCurleyString(byte[] array)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within curley brackets {}.

      For example this is the formatted string of an array {00:26:62:2f:47:87} using a toHexCurleyString(array, 0, 6).

      Parameters:
      array - the source array for bytes
      Returns:
      the formatted string
    • toHexCurleyString

      public static String toHexCurleyString(byte[] array, int offset, int length)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within curley brackets {}.

      For example this is the formatted string of 6 bytes out of the an array {00:26:62:2f:47:87} using a toHexCurleyString(array, 0, 6).

      Parameters:
      array - the source array for bytes
      offset - the offset into the array to start
      length - the number of bytes to format
      Returns:
      the formatted string
    • toHexCurleyString

      public static String toHexCurleyString(ByteBuffer buf)
      Formats individual bytes in a ByteBuffer to 2 digit hex values, each separated by ':' character enclosed within square brackets [].

      For example this is the formatted string of an ByteBuffer {00:26:62:2f:47:87} using a toHexCurleyString(buf).

      Parameters:
      buf - the source ByteBuffer for bytes
      Returns:
      the formatted string
    • toHexSquareString

      public static String toHexSquareString(byte[] array)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within square brackets [].

      For example this is the formatted string of an ByteBuffer [00:26:62:2f:47:87] using a toHexSquareString(array).

      Parameters:
      array - the source array for bytes
      Returns:
      the formatted string
    • toHexSquareString

      public static String toHexSquareString(byte[] array, int offset, int length)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character enclosed within square brackets [].

      For example this is the formatted string of 6 bytes out of the an array [00:26:62:2f:47:87] using a toHexSquareString(array, 0, 6).

      Parameters:
      array - the source array for bytes
      offset - the offset into the array to start
      length - the number of bytes to format
      Returns:
      the formatted string
    • toHexSquareString

      public static String toHexSquareString(ByteBuffer buf)
      Formats individual bytes in a ByteBuffer to 2 digit hex values, each separated by ':' character enclosed within square brackets [].

      For example this is the formatted string of an ByteBuffer [00:26:62:2f:47:87] using a toHexSquareString(buf).

      Parameters:
      buf - the source ByteBuffer for bytes
      Returns:
      the formatted string
    • toHexString

      public static String toHexString(byte[] array)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character.

      For example this is the formatted string of 6 bytes out of the an array 00:26:62:2f:47:87 using a toHexString(array).

      Parameters:
      array - the source array for bytes
      Returns:
      the formatted string
    • toHexString

      public static String toHexString(byte[] array, int offset, int length)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character.

      For example this is the formatted string of 6 bytes out of the an array 00:26:62:2f:47:87 using a toHexString(array, 0, 6).

      Parameters:
      array - the source array for bytes
      offset - the offset into the array to start
      length - the number of bytes to format
      Returns:
      the formatted string
    • toHexString

      public static String toHexString(byte[] array, int offset, int length, String prefix, String postfix)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character. Additionally a prefix and postfix are used to enclose the resultant hex string.

      For example this is the formatted string of 6 bytes out of the an array <00:26:62:2f:47:87> using a toHexString(array, 0, 6, "<", ">").

      Parameters:
      array - the source array for bytes
      offset - the offset into the array to start
      length - the number of bytes to format
      prefix - the prefix in front of the resulting string
      postfix - the postfix after the resulting string
      Returns:
      the formatted string
    • toHexString

      public static String toHexString(byte[] array, String prefix, String postfix)
      Formats individual bytes in an array to 2 digit hex values, each separated by ':' character. Additionally a prefix and postfix are used to enclose the resultant hex string.

      For example this is the formatted string an array <00:26:62:2f:47:87> using a toHexString(array, "<", ">").

      Parameters:
      array - the source array for bytes
      prefix - the prefix in front of the resulting string
      postfix - the postfix after the resulting string
      Returns:
      the formatted string
    • toHexString

      public static String toHexString(ByteBuffer buf)
      Formats individual bytes in a ByteBuffer to 2 digit hex values, each separated by ':' character.

      For example this is the formatted string a ByteBuffer 00:26:62:2f:47:87 using a toHexString(buf).

      Parameters:
      buf - the source ByteBuffer for bytes
      Returns:
      the formatted string
    • toIp4AddressString

      public static String toIp4AddressString(byte[] array)
      Formats the given array as a IP ver 4 address in the form of 192.168.1.1.
      Parameters:
      array - the IP ver 4 address
      Returns:
      the formatted string