Class ByteArrayUtil
- java.lang.Object
-
- org.eclipse.keyple.core.util.ByteArrayUtil
-
public final class ByteArrayUtil extends java.lang.Object
Utility class around byte arrays.- Since:
- 2.0.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static byte[]
extractBytes(byte[] src, int bitOffset, int nbBytes)
Extracts "nbBytes" bytes from the "bitOffset" index (in bits) from a byte array.static int
extractInt(byte[] src, int offset, int nbBytes, boolean isSigned)
Converts "nbBytes" bytes located at the "offset" provided in a source byte array into an integer.static int
fourBytesToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 2" and "isSigned = true|false".static byte[]
fromHex(java.lang.String hex)
Deprecated.UseHexUtil.toByteArray(String)
method instead.static boolean
isValidHexString(java.lang.String hex)
Deprecated.UseHexUtil.isValid(String)
method instead.static java.lang.String
normalizeHexString(java.lang.String hex)
Deprecated.To be removed.static int
threeBytesSignedToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 3" and "isSigned = true".static int
threeBytesToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 3" and "isSigned = false".static java.lang.String
toHex(byte[] src)
Deprecated.UseHexUtil.toHex(byte[])
method instead.static int
twoBytesSignedToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 2" and "isSigned = true".static int
twoBytesToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 2" and "isSigned = false".
-
-
-
Method Detail
-
extractBytes
public static byte[] extractBytes(byte[] src, int bitOffset, int nbBytes)
Extracts "nbBytes" bytes from the "bitOffset" index (in bits) from a byte array.- Parameters:
src
- The source byte array.bitOffset
- The offset (in bits).nbBytes
- The number of bytes to extract.- Returns:
- A not null byte array.
- Throws:
java.lang.NullPointerException
- If "src" is null.java.lang.ArrayIndexOutOfBoundsException
- If "bitOffset" or "nbBytes" is out of range.java.lang.NegativeArraySizeException
- If "nbBytes" is negative.- Since:
- 2.1.0
-
extractInt
public static int extractInt(byte[] src, int offset, int nbBytes, boolean isSigned)
Converts "nbBytes" bytes located at the "offset" provided in a source byte array into an integer.Caution: the result may be erroneous if "nbBytes" is not in range [1..4].
- Parameters:
src
- The source byte array.offset
- The offset (in bytes).nbBytes
- The number of bytes to extract.isSigned
- True if the resulting integer is "signed" (relevant only if "nbBytes" is in range [1..3]).- Returns:
- An int.
- Throws:
java.lang.NullPointerException
- If "src" is null.java.lang.ArrayIndexOutOfBoundsException
- If "offset" is not in range [0..(src.length-nbBytes)]- Since:
- 2.1.0
-
isValidHexString
@Deprecated public static boolean isValidHexString(java.lang.String hex)
Deprecated.UseHexUtil.isValid(String)
method instead.Checks if the provided string is formed by an even number of hexadecimal digits.
"1234AB"
will match."1234AB2"
,"12 34AB"
or"x1234AB"
won't match.
- Parameters:
hex
- A string.- Returns:
- true if the string matches the expected hexadecimal representation, false otherwise.
- Since:
- 2.0.0
-
normalizeHexString
@Deprecated public static java.lang.String normalizeHexString(java.lang.String hex)
Deprecated.To be removed.Normalizes the input hex string by padding on the left by a zero if necessary.- Parameters:
hex
- The hex string to normalize.- Returns:
- A not null string.
- Throws:
java.lang.NullPointerException
- If the input string is null.- Since:
- 2.0.0
-
fromHex
@Deprecated public static byte[] fromHex(java.lang.String hex)
Deprecated.UseHexUtil.toByteArray(String)
method instead.Converts the provided hexadecimal string into a byte array.No checks are performed on the input string, except for nullity, zero length and length parity.
- Parameters:
hex
- The hexadecimal string to convert.- Returns:
- A not empty byte array.
- Throws:
java.lang.IllegalArgumentException
- If the provided string is null, empty or made of an odd number of characters.- Since:
- 2.0.0
- See Also:
isValidHexString(String)
-
toHex
@Deprecated public static java.lang.String toHex(byte[] src)
Deprecated.UseHexUtil.toHex(byte[])
method instead.Converts the provided byte array into a hexadecimal string.- Parameters:
src
- The byte array to convert.- Returns:
- An empty string if the byte array is null or empty.
- Since:
- 2.0.0
-
twoBytesToInt
@Deprecated public static int twoBytesToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 2" and "isSigned = false".Converts 2 bytes located at the offset provided in the byte array into an unsigned integer.The 2 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
- Parameters:
bytes
- A byte array.offset
- The position of the 2 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.NullPointerException
- If "bytes" is null.java.lang.ArrayIndexOutOfBoundsException
- If "offset" is not in range [0..(bytes.length-2)]- Since:
- 2.0.0
-
twoBytesSignedToInt
@Deprecated public static int twoBytesSignedToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 2" and "isSigned = true".Converts 2 bytes located at the offset provided in the byte array into a signed integer.The 2 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
The number is also considered as signed. That is, if the MSB (first left bit) is 1, then the number is negative and the conversion is done accordingly with the usual binary arithmetic.
- Parameters:
bytes
- A byte array.offset
- The position of the 2 bytes in the array.- Returns:
- A negative or positive int.
- Throws:
java.lang.NullPointerException
- If "bytes" is null.java.lang.ArrayIndexOutOfBoundsException
- If "offset" is not in range [0..(bytes.length-2)]- Since:
- 2.0.0
-
threeBytesToInt
@Deprecated public static int threeBytesToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 3" and "isSigned = false".Converts 3 bytes located at the offset provided in the byte array into an unsigned integer.The 3 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
- Parameters:
bytes
- A byte array.offset
- The position of the 3 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.NullPointerException
- If "bytes" is null.java.lang.ArrayIndexOutOfBoundsException
- If "offset" is not in range [0..(bytes.length-3)]- Since:
- 2.0.0
-
threeBytesSignedToInt
@Deprecated public static int threeBytesSignedToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 3" and "isSigned = true".Converts 3 bytes located at the offset provided in the byte array into an signed integer.The 3 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
The number is also considered as signed. That is, if the MSB (first left bit) is 1, then the number is negative and the conversion is done accordingly with the usual binary arithmetic.
- Parameters:
bytes
- A byte array.offset
- The position of the 3 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.NullPointerException
- If "bytes" is null.java.lang.ArrayIndexOutOfBoundsException
- If "offset" is not in range [0..(bytes.length-3)]- Since:
- 2.0.0
-
fourBytesToInt
@Deprecated public static int fourBytesToInt(byte[] bytes, int offset)
Deprecated.UseextractInt(byte[], int, int, boolean)
method instead with "nbBytes = 2" and "isSigned = true|false".Converts 4 bytes located at the offset provided in the byte array into an unsigned integer.The 4 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
- Parameters:
bytes
- A byte array.offset
- The position of the 4 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.NullPointerException
- If "bytes" is null.java.lang.ArrayIndexOutOfBoundsException
- If "offset" is not in range [0..(bytes.length-4)]- Since:
- 2.0.0
-
-