Class 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 void copyBytes​(long src, byte[] dest, int offset, int nbBytes)
      Copy the least significant bytes (LSB) of a number (byte, short, integer or long) into a byte array at a specific offset.
      static byte[] extractBytes​(byte[] src, int bitOffset, int nbBytes)
      Extracts "nbBytes" bytes from the "bitOffset" index (in bits) from a byte array.
      static byte[] extractBytes​(long src, int nbBytes)
      Extracts the least significant bytes (LSB) of a number into 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 long extractLong​(byte[] src, int offset, int nbBytes, boolean isSigned)
      Converts "nbBytes" bytes located at the "offset" provided in a source byte array into a "long".
      static short extractShort​(byte[] src, int offset)
      Extracts a 2-byte "short" located at a specific "offset" in a source byte array.
      static int fourBytesToInt​(byte[] bytes, int offset)
      Deprecated.
      Use extractInt(byte[], int, int, boolean) method instead with "nbBytes = 2" and "isSigned = true|false".
      static byte[] fromHex​(java.lang.String hex)
      Deprecated.
      Use HexUtil.toByteArray(String) method instead.
      static boolean isValidHexString​(java.lang.String hex)
      Deprecated.
      Use HexUtil.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.
      Use extractInt(byte[], int, int, boolean) method instead with "nbBytes = 3" and "isSigned = true".
      static int threeBytesToInt​(byte[] bytes, int offset)
      Deprecated.
      Use extractInt(byte[], int, int, boolean) method instead with "nbBytes = 3" and "isSigned = false".
      static java.lang.String toHex​(byte[] src)
      Deprecated.
      Use HexUtil.toHex(byte[]) method instead.
      static int twoBytesSignedToInt​(byte[] bytes, int offset)
      Deprecated.
      Use extractInt(byte[], int, int, boolean) method instead with "nbBytes = 2" and "isSigned = true".
      static int twoBytesToInt​(byte[] bytes, int offset)
      Deprecated.
      Use extractInt(byte[], int, int, boolean) method instead with "nbBytes = 2" and "isSigned = false".
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
      • extractBytes

        public static byte[] extractBytes​(long src,
                                          int nbBytes)
        Extracts the least significant bytes (LSB) of a number into a byte array.

        Caution: the result may be erroneous if the number of bytes to extract is greater than the number of bytes associated to the input number (e.g. 2 bytes max for a "short", 4 bytes max for an "integer" or 8 bytes max for a "long").

        Parameters:
        src - The source.
        nbBytes - The number of bytes to extract.
        Returns:
        An empty array if "nbBytes" is equal to 0.
        Throws:
        java.lang.NegativeArraySizeException - If "nbBytes" is negative.
        Since:
        2.3.0
      • extractShort

        public static short extractShort​(byte[] src,
                                         int offset)
        Extracts a 2-byte "short" located at a specific "offset" in a source byte array.
        Parameters:
        src - The source byte array.
        offset - The offset (in bytes).
        Returns:
        A short.
        Throws:
        java.lang.NullPointerException - If "src" is null.
        java.lang.ArrayIndexOutOfBoundsException - If "offset" is not in range [0..(src.length-2)]
        Since:
        2.3.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
      • extractLong

        public static long extractLong​(byte[] src,
                                       int offset,
                                       int nbBytes,
                                       boolean isSigned)
        Converts "nbBytes" bytes located at the "offset" provided in a source byte array into a "long".

        Caution: the result may be erroneous if "nbBytes" is not in range [1..8].

        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..7]).
        Returns:
        A long.
        Throws:
        java.lang.NullPointerException - If "src" is null.
        java.lang.ArrayIndexOutOfBoundsException - If "offset" is not in range [0..(src.length-nbBytes)]
        Since:
        2.3.0
      • copyBytes

        public static void copyBytes​(long src,
                                     byte[] dest,
                                     int offset,
                                     int nbBytes)
        Copy the least significant bytes (LSB) of a number (byte, short, integer or long) into a byte array at a specific offset.
        Parameters:
        src - The number.
        dest - The target byte array.
        offset - The offset (in bytes).
        nbBytes - The number of bytes to copy.
        Throws:
        java.lang.NullPointerException - If "dest" is null.
        java.lang.NegativeArraySizeException - If "nbBytes" is negative.
        java.lang.ArrayIndexOutOfBoundsException - If "offset" is not in range [0..(dest.length-nbBytes)]
        Since:
        2.3.0
      • isValidHexString

        @Deprecated
        public static boolean isValidHexString​(java.lang.String hex)
        Deprecated.
        Use HexUtil.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.
        Use HexUtil.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.
        Use HexUtil.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.
        Use extractInt(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.
        Use extractInt(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.
        Use extractInt(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.
        Use extractInt(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.
        Use extractInt(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