Class ByteUtils


  • public final class ByteUtils
    extends java.lang.Object
    This class is a utility class for manipulating byte arrays.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] clone​(byte[] array)
      Return a clone of the given byte array (performs null check beforehand).
      static byte[] concatenate​(byte[][] array)
      Convert a 2-dimensional byte array into a 1-dimensional byte array by concatenating all entries.
      static byte[] concatenate​(byte[] x1, byte[] x2)
      Concatenate two byte arrays.
      static int deepHashCode​(byte[] array)
      Computes a hashcode based on the contents of a one-dimensional byte array rather than its identity.
      static int deepHashCode​(byte[][] array)
      Computes a hashcode based on the contents of a two-dimensional byte array rather than its identity.
      static int deepHashCode​(byte[][][] array)
      Computes a hashcode based on the contents of a three-dimensional byte array rather than its identity.
      static boolean equals​(byte[][][] left, byte[][][] right)
      Compare two three-dimensional byte arrays.
      static boolean equals​(byte[][] left, byte[][] right)
      Compare two two-dimensional byte arrays.
      static boolean equals​(byte[] left, byte[] right)
      Compare two byte arrays (perform null checks beforehand).
      static byte[] fromHexString​(java.lang.String s)
      Convert a string containing hexadecimal characters to a byte-array.
      static byte[][] split​(byte[] input, int index)
      Split a byte array input into two arrays at index, i.e. the first array will have the lower index bytes, the second one the higher input.length - index bytes.
      static byte[] subArray​(byte[] input, int start)
      Generate a subarray of a given byte array.
      static byte[] subArray​(byte[] input, int start, int end)
      Generate a subarray of a given byte array.
      static java.lang.String toBinaryString​(byte[] input)
      Convert a byte array to the corresponding bit string.
      static char[] toCharArray​(byte[] input)
      Rewrite a byte array as a char array
      static java.lang.String toHexString​(byte[] input)
      Convert a byte array to the corresponding hexstring.
      static java.lang.String toHexString​(byte[] input, java.lang.String prefix, java.lang.String seperator)
      Convert a byte array to the corresponding hex string.
      static byte[] xor​(byte[] x1, byte[] x2)
      Compute the bitwise XOR of two arrays of bytes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • equals

        public static boolean equals​(byte[] left,
                                     byte[] right)
        Compare two byte arrays (perform null checks beforehand).
        Parameters:
        left - the first byte array
        right - the second byte array
        Returns:
        the result of the comparison
      • equals

        public static boolean equals​(byte[][] left,
                                     byte[][] right)
        Compare two two-dimensional byte arrays. No null checks are performed.
        Parameters:
        left - the first byte array
        right - the second byte array
        Returns:
        the result of the comparison
      • equals

        public static boolean equals​(byte[][][] left,
                                     byte[][][] right)
        Compare two three-dimensional byte arrays. No null checks are performed.
        Parameters:
        left - the first byte array
        right - the second byte array
        Returns:
        the result of the comparison
      • deepHashCode

        public static int deepHashCode​(byte[] array)
        Computes a hashcode based on the contents of a one-dimensional byte array rather than its identity.
        Parameters:
        array - the array to compute the hashcode of
        Returns:
        the hashcode
      • deepHashCode

        public static int deepHashCode​(byte[][] array)
        Computes a hashcode based on the contents of a two-dimensional byte array rather than its identity.
        Parameters:
        array - the array to compute the hashcode of
        Returns:
        the hashcode
      • deepHashCode

        public static int deepHashCode​(byte[][][] array)
        Computes a hashcode based on the contents of a three-dimensional byte array rather than its identity.
        Parameters:
        array - the array to compute the hashcode of
        Returns:
        the hashcode
      • clone

        public static byte[] clone​(byte[] array)
        Return a clone of the given byte array (performs null check beforehand).
        Parameters:
        array - the array to clone
        Returns:
        the clone of the given array, or null if the array is null
      • fromHexString

        public static byte[] fromHexString​(java.lang.String s)
        Convert a string containing hexadecimal characters to a byte-array.
        Parameters:
        s - a hex string
        Returns:
        a byte array with the corresponding value
      • toHexString

        public static java.lang.String toHexString​(byte[] input)
        Convert a byte array to the corresponding hexstring.
        Parameters:
        input - the byte array to be converted
        Returns:
        the corresponding hexstring
      • toHexString

        public static java.lang.String toHexString​(byte[] input,
                                                   java.lang.String prefix,
                                                   java.lang.String seperator)
        Convert a byte array to the corresponding hex string.
        Parameters:
        input - the byte array to be converted
        prefix - the prefix to put at the beginning of the hex string
        seperator - a separator string
        Returns:
        the corresponding hex string
      • toBinaryString

        public static java.lang.String toBinaryString​(byte[] input)
        Convert a byte array to the corresponding bit string.
        Parameters:
        input - the byte array to be converted
        Returns:
        the corresponding bit string
      • xor

        public static byte[] xor​(byte[] x1,
                                 byte[] x2)
        Compute the bitwise XOR of two arrays of bytes. The arrays have to be of same length. No length checking is performed.
        Parameters:
        x1 - the first array
        x2 - the second array
        Returns:
        x1 XOR x2
      • concatenate

        public static byte[] concatenate​(byte[] x1,
                                         byte[] x2)
        Concatenate two byte arrays. No null checks are performed.
        Parameters:
        x1 - the first array
        x2 - the second array
        Returns:
        (x2||x1) (little-endian order, i.e. x1 is at lower memory addresses)
      • concatenate

        public static byte[] concatenate​(byte[][] array)
        Convert a 2-dimensional byte array into a 1-dimensional byte array by concatenating all entries.
        Parameters:
        array - a 2-dimensional byte array
        Returns:
        the concatenated input array
      • split

        public static byte[][] split​(byte[] input,
                                     int index)
                              throws java.lang.ArrayIndexOutOfBoundsException
        Split a byte array input into two arrays at index, i.e. the first array will have the lower index bytes, the second one the higher input.length - index bytes.
        Parameters:
        input - the byte array to be split
        index - the index where the byte array is split
        Returns:
        the splitted input array as an array of two byte arrays
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if index is out of bounds
      • subArray

        public static byte[] subArray​(byte[] input,
                                      int start,
                                      int end)
        Generate a subarray of a given byte array.
        Parameters:
        input - the input byte array
        start - the start index
        end - the end index
        Returns:
        a subarray of input, ranging from start (inclusively) to end (exclusively)
      • subArray

        public static byte[] subArray​(byte[] input,
                                      int start)
        Generate a subarray of a given byte array.
        Parameters:
        input - the input byte array
        start - the start index
        Returns:
        a subarray of input, ranging from start to the end of the array
      • toCharArray

        public static char[] toCharArray​(byte[] input)
        Rewrite a byte array as a char array
        Parameters:
        input - - the byte array
        Returns:
        char array