Package com.oss.util
Class ByteTool
java.lang.Object
com.oss.util.ByteTool
This class provides utility methods for testing and manipulating
byte arrays.
- Since:
- ASN.1/Java 6.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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[]
parseIPAddress
(String str) Take a textual IP address, convert it to the binary format and return the result in a byte[] array.static byte[]
parseTAP3BCD
(String tap3Bcd) 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 thebyte[]
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 thebyte[]
to text format.static String
toTBCD
(byte[] tbcd) Converts a Telephony Binary-Coded Decimal (TBCD) that is stored in thebyte[]
to the text format.
-
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
Converts a Binary-Coded Decimal that is stored in thebyte[]
to a decimal number.- Parameters:
bcd
- the Binary-Coded Decimal.- Returns:
- An unsigned decimal number.
- Throws:
NumberFormatException
- when thebcd
violates the BCD format.
-
parseBCD
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 thebcd
string contains non-numeric characters.
-
toTAP3BCD
Converts a TAP3 Binary-Coded Decimal that is stored in thebyte[]
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 thetap3Bcd
violates the TAP3 BCD format.
-
parseTAP3BCD
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 thetap3Bcd
string contains characters that are not valid TAP3 BCD digits.
-
toTBCD
Converts a Telephony Binary-Coded Decimal (TBCD) that is stored in thebyte[]
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 thetbcd
violates the TBCD format.
-
parseTBCD
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 thetbcd
string contains characters that are not valid TBCD digits.
-
toIPAddress
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
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 theaddress
string is badly formatted.
-