Class ByteArrayUtil


  • public class ByteArrayUtil
    extends Object
    Class providing utility methods for handling byte arrays.
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteArrayUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int compareRegion​(byte[] array1, int startIdx1, byte[] array2, int startIdx2, int length)
      Compares two regions of bytes, indicating whether one is larger than the other.
      static int find​(byte[] a, int fromIndex, int toIndex, byte key)
      Retrieve a byte from a byte array.
      static int find​(byte[] a, int fromIndex, int toIndex, byte[] key)
      Look for a sequence of bytes in a byte array.
      static byte[] get​(byte[] array, int offset)
      Gets the subarray from array that starts at offset.
      static byte[] get​(byte[] array, int offset, int length)
      Gets the subarray of length length from array that starts at offset.
      static int getInt​(byte[] array, int offset)
      Get an integer value from a byte array at a specific offset.
      static long getLong​(byte[] array, int offset)
      Get a long value from a byte array at a specific offset.
      static boolean matchesPattern​(byte[] value, byte[] mask, byte[] pattern)
      Checks whether value matches pattern with respect to the bits specified by mask.
      static void put​(byte[] source, byte[] target, int offset)
      Puts the entire source array in the target array at offset offset.
      static void putInt​(int value, byte[] array, int offset)
      Put an integer value (padded) in a byte array at a specific offset.
      static void putLong​(long value, byte[] array, int offset)
      Put a long value (padded) in a byte array at a specific offset.
      static boolean regionMatches​(byte[] subValue, byte[] superValue, int offset)
      Checks whether subValue matches the region in superValue starting at offset offset.
      static BitSet toBitSet​(byte[] array)
      Convert a byte array to a vector of bits.
      static byte[] toByteArray​(BitSet bitSet)
      Convert a bitset to a byte array.
      static String toHexString​(byte[] array)
      Returns the hexadecimal value of the supplied byte array.
    • Constructor Detail

      • ByteArrayUtil

        public ByteArrayUtil()
    • Method Detail

      • put

        public static void put​(byte[] source,
                               byte[] target,
                               int offset)
        Puts the entire source array in the target array at offset offset.
        Parameters:
        source - source array
        target - target array
        offset - non-negative offset
      • get

        public static byte[] get​(byte[] array,
                                 int offset)
        Gets the subarray from array that starts at offset.
        Parameters:
        array - source array
        offset - non-negative offset
        Returns:
        byte array
      • get

        public static byte[] get​(byte[] array,
                                 int offset,
                                 int length)
        Gets the subarray of length length from array that starts at offset.
        Parameters:
        array - byte array
        offset - non-negative offset
        length - length
        Returns:
        byte array
      • putInt

        public static void putInt​(int value,
                                  byte[] array,
                                  int offset)
        Put an integer value (padded) in a byte array at a specific offset.
        Parameters:
        value - integer value
        array - byte array
        offset - non-negative offset
      • getInt

        public static int getInt​(byte[] array,
                                 int offset)
        Get an integer value from a byte array at a specific offset.
        Parameters:
        array - byte array
        offset - non-negative offset
        Returns:
        integer value
      • putLong

        public static void putLong​(long value,
                                   byte[] array,
                                   int offset)
        Put a long value (padded) in a byte array at a specific offset.
        Parameters:
        value - long value
        array - byte array
        offset - non-negative offset
      • getLong

        public static long getLong​(byte[] array,
                                   int offset)
        Get a long value from a byte array at a specific offset.
        Parameters:
        array - byte array
        offset - offset
        Returns:
        long value
      • find

        public static int find​(byte[] a,
                               int fromIndex,
                               int toIndex,
                               byte key)
        Retrieve a byte from a byte array.
        Parameters:
        a - the byte array to look in
        fromIndex - the position from which to start looking
        toIndex - the position up to which to look
        key - the byte to find
        Returns:
        the position of the byte in the array, or -1 if the byte was not found in the array
      • find

        public static int find​(byte[] a,
                               int fromIndex,
                               int toIndex,
                               byte[] key)
        Look for a sequence of bytes in a byte array.
        Parameters:
        a - the byte array to look in
        fromIndex - the position from which to start looking
        toIndex - the position up to which to look
        key - the bytes to find
        Returns:
        the position of the bytes in the array, or -1 if the bytes were not found in the array
      • matchesPattern

        public static boolean matchesPattern​(byte[] value,
                                             byte[] mask,
                                             byte[] pattern)
        Checks whether value matches pattern with respect to the bits specified by mask. In other words: this method returns true if (value[i] ^ pattern[i]) & mask[i] == 0 for all i.
        Parameters:
        value - byte array
        mask -
        pattern - pattern
        Returns:
        true if pattern was found
      • regionMatches

        public static boolean regionMatches​(byte[] subValue,
                                            byte[] superValue,
                                            int offset)
        Checks whether subValue matches the region in superValue starting at offset offset.
        Parameters:
        subValue - value to search for
        superValue - byte array
        offset - non-negative offset
        Returns:
        true upon exact match, false otherwise
      • compareRegion

        public static int compareRegion​(byte[] array1,
                                        int startIdx1,
                                        byte[] array2,
                                        int startIdx2,
                                        int length)
        Compares two regions of bytes, indicating whether one is larger than the other.
        Parameters:
        array1 - The first byte array.
        startIdx1 - The start of the region in the first array.
        array2 - The second byte array.
        startIdx2 - The start of the region in the second array.
        length - The length of the region that should be compared.
        Returns:
        A negative number when the first region is smaller than the second, a positive number when the first region is larger than the second, or 0 if the regions are equal.
      • toBitSet

        public static BitSet toBitSet​(byte[] array)
        Convert a byte array to a vector of bits.
        Parameters:
        array - byte array
        Returns:
        bitset
      • toByteArray

        public static byte[] toByteArray​(BitSet bitSet)
        Convert a bitset to a byte array.
        Parameters:
        bitSet - bitset (should not be null)
        Returns:
        byte array
      • toHexString

        public static String toHexString​(byte[] array)
        Returns the hexadecimal value of the supplied byte array. The resulting string always uses two hexadecimals per byte. As a result, the length of the resulting string is guaranteed to be twice the length of the supplied byte array.
        Parameters:
        array - byte array
        Returns:
        hexadecimal string