Class Bytes


  • public final class Bytes
    extends java.lang.Object
    Helper methods that deal with byte arrays.
    Since:
    1.0.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int byteArrayToInt​(byte[] bytes)
      Transforms a passed LSB first byte array to an int
      static int byteArrayToInt​(byte[] bytes, int length)
      Transforms a passed LSB first byte array to an int
      static int byteArrayToInt​(byte[] bytes, int offset, int length)
      Transforms a passed LSB first byte array to an int
      static byte[] concat​(byte[]... chunks)
      Returns the concatenation of the input arrays in a single array.
      static boolean equal​(byte[] x, byte[] y)
      Best effort fix-timing array comparison.
      static byte[] intToByteArray​(int capacity, int value)
      Transforms a passed value to a LSB first byte array with the size of the specified capacity
      static byte[] xor​(byte[] x, byte[] y)
      Computes the xor of two byte arrays of equal size.
      static byte[] xor​(byte[] x, int offsetX, byte[] y, int offsetY, int len)
      Computes the xor of two byte arrays, specifying offsets and the length to xor.
      static void xor​(java.nio.ByteBuffer output, java.nio.ByteBuffer x, java.nio.ByteBuffer y, int len)
      Computes the xor of two byte buffers, specifying the length to xor, and stores the result to output.
      static byte[] xorEnd​(byte[] a, byte[] b)
      xors b to the end of a.
      • Methods inherited from class java.lang.Object

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

      • equal

        public static final boolean equal​(byte[] x,
                                          byte[] y)
        Best effort fix-timing array comparison.
        Returns:
        true if two arrays are equal.
      • concat

        public static byte[] concat​(byte[]... chunks)
                             throws java.security.GeneralSecurityException
        Returns the concatenation of the input arrays in a single array. For example, concat(new byte[] {a, b}, new byte[] {}, new byte[] {c} returns the array {a, b, c}.
        Returns:
        a single array containing all the values from the source arrays, in order
        Throws:
        java.security.GeneralSecurityException
      • xor

        public static final byte[] xor​(byte[] x,
                                       int offsetX,
                                       byte[] y,
                                       int offsetY,
                                       int len)
        Computes the xor of two byte arrays, specifying offsets and the length to xor.
        Returns:
        a new byte[] of length len.
      • xor

        public static final void xor​(java.nio.ByteBuffer output,
                                     java.nio.ByteBuffer x,
                                     java.nio.ByteBuffer y,
                                     int len)
        Computes the xor of two byte buffers, specifying the length to xor, and stores the result to output.
      • xor

        public static final byte[] xor​(byte[] x,
                                       byte[] y)
        Computes the xor of two byte arrays of equal size.
        Returns:
        a new byte[] of length x.length.
      • xorEnd

        public static final byte[] xorEnd​(byte[] a,
                                          byte[] b)
        xors b to the end of a.
        Returns:
        a new byte[] of length x.length.
      • intToByteArray

        public static byte[] intToByteArray​(int capacity,
                                            int value)
        Transforms a passed value to a LSB first byte array with the size of the specified capacity
        Parameters:
        capacity - size of the resulting byte array
        value - that should be represented as a byte array. 0 <= value < 256^capacity.
      • byteArrayToInt

        public static int byteArrayToInt​(byte[] bytes)
        Transforms a passed LSB first byte array to an int
        Parameters:
        bytes - that should be transformed to a byte array
      • byteArrayToInt

        public static int byteArrayToInt​(byte[] bytes,
                                         int length)
        Transforms a passed LSB first byte array to an int
        Parameters:
        bytes - that should be transformed to a byte array
        length - amount of the passed bytes that should be transformed
      • byteArrayToInt

        public static int byteArrayToInt​(byte[] bytes,
                                         int offset,
                                         int length)
        Transforms a passed LSB first byte array to an int
        Parameters:
        bytes - that should be transformed to a byte array
        offset - start index to start the transformation
        length - amount of the passed bytes that should be transformed