Package com.oss.util

Class ByteTool

java.lang.Object
com.oss.util.ByteTool

public abstract class ByteTool extends Object
This class provides utility methods for testing and manipulating byte arrays.
Since:
ASN.1/Java 6.0
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    compare(byte[] array1, byte[] array2)
    Compare two byte arrays for equality.
    static byte[]
    copy(byte[] src)
    Create a copy of an array of bytes.
    static byte[]
    Parses a string containing an unsigned decimal number and converts it to the Binary-Coded Decimal representation.
    static byte[]
    Take a textual IP address, convert it to the binary format and return the result in a byte[] array.
    static byte[]
    Parses a string containing one or more '0'-'9', 'a', 'b', 'c', 'd' and 'e' digits and converts it to the Binary-Coded Decimal representation.
    static byte[]
    Parses a string containing one or more '0'-'9', 'a', 'b', 'c', '+' and '#' digits and converts it to the Telephony Binary-Coded Decimal representation.
    static String
    toBCD(byte[] bcd)
    Converts a Binary-Coded Decimal that is stored in the byte[] to a decimal number.
    static String
    toIPAddress(byte[] address)
    Convert a binary IP address to text format.
    static String
    toTAP3BCD(byte[] tap3Bcd)
    Converts a TAP3 Binary-Coded Decimal that is stored in the byte[] to text format.
    static String
    toTBCD(byte[] tbcd)
    Converts a Telephony Binary-Coded Decimal (TBCD) that is stored in the byte[] to the text format.

    Methods inherited from class java.lang.Object

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

    • ByteTool

      public ByteTool()
  • Method Details

    • compare

      public static boolean compare(byte[] array1, byte[] array2)
      Compare two byte arrays for equality.
      Parameters:
      array1 - is one byte array.
      array2 - is another byte array.
      Returns:
      true if "array1" references the same array of bytes as "array2" or both are null or the contents of "array1" is equal to the contents of "array2".
    • copy

      public static byte[] copy(byte[] src)
      Create a copy of an array of bytes.
      Parameters:
      src - is the byte array to make an exact copy of.
      Returns:
      an exact copy of the input byte array.
    • toBCD

      public static String toBCD(byte[] bcd) throws NumberFormatException
      Converts a Binary-Coded Decimal that is stored in the byte[] to a decimal number.
      Parameters:
      bcd - the Binary-Coded Decimal.
      Returns:
      An unsigned decimal number.
      Throws:
      NumberFormatException - when the bcd violates the BCD format.
    • parseBCD

      public static byte[] parseBCD(String bcd) throws NumberFormatException
      Parses a string containing an unsigned decimal number and converts it to the Binary-Coded Decimal representation. The Binary-Coded Decimal representation packs each pair of digits into a single octet where the most significant digit occupies bits 7..4, and the least significant digit occupies bits 3..0.
      Parameters:
      bcd - the unsigned decimal number.
      Returns:
      The Binary-Coded Decimal stored in the byte[].
      Throws:
      NumberFormatException - when the bcd string contains non-numeric characters.
    • toTAP3BCD

      public static String toTAP3BCD(byte[] tap3Bcd) throws NumberFormatException
      Converts a TAP3 Binary-Coded Decimal that is stored in the byte[] to text format. The TAP3 Binary-Coded Decimal can contain 'a', 'b', 'c', 'd', and 'e' digits in addition to '0'-'9'.
      Parameters:
      tap3Bcd - the TAP3 Binary-Coded Decimal.
      Returns:
      The string representing the TAP3 Binary-Coded Decimal number.
      Throws:
      NumberFormatException - when the tap3Bcd violates the TAP3 BCD format.
    • parseTAP3BCD

      public static byte[] parseTAP3BCD(String tap3Bcd) throws NumberFormatException
      Parses a string containing one or more '0'-'9', 'a', 'b', 'c', 'd' and 'e' digits and converts it to the Binary-Coded Decimal representation. The Binary-Coded Decimal representation packs each pair of digits into a single octet where the most significant digit occupies bits 7..4, and the least significant digit occupies bits 3..0.
      Parameters:
      tap3Bcd - the string representing the TAP3 Binary-Coded Decimal number.
      Returns:
      The TAP3 Binary-Coded Decimal stored in the byte[].
      Throws:
      NumberFormatException - when the tap3Bcd string contains characters that are not valid TAP3 BCD digits.
    • toTBCD

      public static String toTBCD(byte[] tbcd) throws NumberFormatException
      Converts a Telephony Binary-Coded Decimal (TBCD) that is stored in the byte[] to the text format. The TBCD can contain 'a', 'b', 'c', '*' and '#' digits in addition to '0'-'9'.
      Parameters:
      tbcd - the Telephony Binary-Coded Decimal.
      Returns:
      The string representing the Telephony Binary-Coded Decimal number.
      Throws:
      NumberFormatException - when the tbcd violates the TBCD format.
    • parseTBCD

      public static byte[] parseTBCD(String tbcd) throws NumberFormatException
      Parses a string containing one or more '0'-'9', 'a', 'b', 'c', '+' and '#' digits and converts it to the Telephony Binary-Coded Decimal representation. The Telephony Binary-Coded Decimal representation packs each pair of digits into a single octet where the most significant digit occupies bits 3..0, and the least significant digit occupies bits 7..4.
      Parameters:
      tbcd - the string representing the Telephony Binary-Coded Decimal number.
      Returns:
      The Telephony Binary-Coded Decimal stored in the byte[].
      Throws:
      NumberFormatException - when the tbcd string contains characters that are not valid TBCD digits.
    • toIPAddress

      public static String toIPAddress(byte[] address) throws NumberFormatException
      Convert a binary IP address to text format. If input byte array is 4 bytes then it is formatted as IPv4 address "d.d.d.d". If the input array is 16 bytes then it is formatted is IPv6 address. For IPv6 address the method outputs the shortened notation (like ::1) and IPv4-mapped IPv6 address notation (like ::ffff:192.168.0.1) when appropriate. The NumberFormatException is thrown if input array is neither 4 or 16 bytes in length.
      Parameters:
      address - input byte array.
      Returns:
      the string that contains the IP address formatted as text.
      Throws:
      NumberFormatException - if the byte[] array is neither 4 or 16 bytes in length.
    • parseIPAddress

      public static byte[] parseIPAddress(String str) throws NumberFormatException
      Take a textual IP address, convert it to the binary format and return the result in a byte[] array.
      Parameters:
      str - the string that contains the IP address. Both IP v4 and v6 formats are supported.
      Returns:
      byte[] array that contains the binary representation of the IP address. The length of the array is 4 for IPv4 addresses or 16 for IPv6 addresses.
      Throws:
      NumberFormatException - if the address string is badly formatted.