Class ByteUtil


  • public class ByteUtil
    extends Object
    • Field Detail

      • EMPTY_WORD

        public static final byte[] EMPTY_WORD
      • EMPTY_HALFWORD

        public static final byte[] EMPTY_HALFWORD
      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
      • ZERO_BYTE_ARRAY

        public static final byte[] ZERO_BYTE_ARRAY
    • Constructor Detail

      • ByteUtil

        public ByteUtil()
    • Method Detail

      • appendByte

        public static byte[] appendByte​(byte[] bytes,
                                        byte b)
        Creates a copy of bytes and appends b to the end of it
      • bigIntegerToBytes

        public static byte[] bigIntegerToBytes​(BigInteger b,
                                               int numBytes)
        The regular BigInteger.toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
        Parameters:
        b - the integer to format into a byte array
        numBytes - the desired size of the resulting byte array
        Returns:
        numBytes byte long array.
      • bigIntegerToBytesSigned

        public static byte[] bigIntegerToBytesSigned​(BigInteger b,
                                                     int numBytes)
      • bigIntegerToBytes

        public static byte[] bigIntegerToBytes​(BigInteger value)
        Omitting sign indication byte.

        Instead of BigIntegers.asUnsignedByteArray(BigInteger)
        we use this custom method to avoid an empty array in case of BigInteger.ZERO
        Parameters:
        value - - any big integer number. A null-value will return null
        Returns:
        A byte array without a leading zero byte if present in the signed encoding. BigInteger.ZERO will return an array with length 1 and byte-value 0.
      • bytesToBigInteger

        public static BigInteger bytesToBigInteger​(byte[] bb)
      • matchingNibbleLength

        public static int matchingNibbleLength​(byte[] a,
                                               byte[] b)
        Returns the amount of nibbles that match each other from 0 ... amount will never be larger than smallest input
        Parameters:
        a - - first input
        b - - second input
        Returns:
        Number of bytes that match
      • toLEByteArray

        public static void toLEByteArray​(byte[] toConvert)
        Perform an in-place conversion of a byte array from big endian to little endian.
      • longToBytes

        public static byte[] longToBytes​(long val)
        Converts a long value into a byte array.
        Parameters:
        val - - long value to convert
        Returns:
        byte[] of length 8, representing the long value
      • longToBytesLE

        public static byte[] longToBytesLE​(long val)
      • longToBytesNoLeadZeroes

        public static byte[] longToBytesNoLeadZeroes​(long val)
        Converts a long value into a byte array.
        Parameters:
        val - - long value to convert
        Returns:
        decimal value with leading byte that are zeroes striped
      • intToBytes

        public static byte[] intToBytes​(int val)
        Converts int value into a byte array.
        Parameters:
        val - - int value to convert
        Returns:
        byte[] of length 4, representing the int value
      • intToBytesLE

        public static byte[] intToBytesLE​(int val)
        Converts and int value to a byte array (Little Endian) @ param val
        Returns:
        byte[] of length 4; representing the int value in LE order
      • intToBytesBE

        public static byte[] intToBytesBE​(int val)
        Converts and int value to a byte array (Big Endian) @ param val
        Returns:
        byte[] of length 4; representing the int value in LE order
      • intToBytesNoLeadZeroes

        public static byte[] intToBytesNoLeadZeroes​(int val)
        Converts a int value into a byte array.
        Parameters:
        val - - int value to convert
        Returns:
        value with leading byte that are zeroes striped
      • toHexString

        public static String toHexString​(byte[] data)
        Convert a byte-array into a hex String.
        Works similar to Hex.toHexString(byte[]) but allows for null
        Parameters:
        data - - byte-array to convert to a hex-string
        Returns:
        hex representation of the data.
        Returns an empty String if the input is null TODO: swap out with more efficient implementation, for now seems like we are stuck with this
        See Also:
        Hex.toHexString(byte[])
      • toHexStringWithPrefix

        public static String toHexStringWithPrefix​(byte[] data)
      • calcPacketLength

        public static byte[] calcPacketLength​(byte[] msg)
        Calculate packet length
        Parameters:
        msg - byte[]
        Returns:
        byte-array with 4 elements
      • byteArrayToInt

        public static int byteArrayToInt​(byte[] b)
        Cast hex encoded value from byte[] to int

        Limited to Integer.MAX_VALUE: 2^31-1 (4 bytes)

        Parameters:
        b - array contains the values
        Returns:
        unsigned positive int value.
      • byteArrayToLong

        public static long byteArrayToLong​(byte[] b)
        Cast hex encoded value from byte[] to long

        Limited to Long.MAX_VALUE: 2^63-1 (8 bytes)

        Parameters:
        b - array contains the values
        Returns:
        unsigned positive long value.
      • toByte

        public static byte toByte​(byte[] b)
        Used in conjunction w/ RLP, casts to byte
      • nibblesToPrettyString

        public static String nibblesToPrettyString​(byte[] nibbles)
        Turn nibbles to a pretty looking output string

        Example. [ 1, 2, 3, 4, 5 ] becomes '\x11\x23\x45'

        Parameters:
        nibbles - - getting byte of data [ 04 ] and turning it to a '\x04' representation
        Returns:
        pretty string of nibbles
      • oneByteToHexString

        public static String oneByteToHexString​(byte value)
      • numBytes

        public static int numBytes​(String val)
        Calculate the number of bytes need to encode the number
        Parameters:
        val - - number
        Returns:
        number of min bytes used to encode the number
      • encodeValFor32Bits

        public static byte[] encodeValFor32Bits​(Object arg)
        Parameters:
        arg - - not more that 32 bits
        Returns:
        - bytes of the value pad with complete to 32 zeroes
      • encodeDataList

        public static byte[] encodeDataList​(Object... args)
        encode the values and concatenate together
        Parameters:
        args - Object
        Returns:
        byte[]
      • firstNonZeroByte

        public static int firstNonZeroByte​(byte[] data)
      • stripLeadingZeroes

        public static byte[] stripLeadingZeroes​(byte[] data)
      • increment

        public static boolean increment​(byte[] bytes)
        increment byte array as a number until max is reached
        Parameters:
        bytes - byte[]
        Returns:
        boolean
      • copyToArray

        public static byte[] copyToArray​(BigInteger value)
        Utility function to copy a byte array into a new byte array with given size. If the src length is smaller than the given size, the result will be left-padded with zeros.
        Parameters:
        value - - a BigInteger with a maximum value of 2^256-1
        Returns:
        Byte array of given size with a copy of the src
      • setBit

        public static byte[] setBit​(byte[] data,
                                    int pos,
                                    int val)
      • getBit

        public static int getBit​(byte[] data,
                                 int pos)
      • and

        public static byte[] and​(byte[] b1,
                                 byte[] b2)
      • or

        public static byte[] or​(byte[] b1,
                                byte[] b2)
      • xor

        public static byte[] xor​(byte[] b1,
                                 byte[] b2)
      • xorAlignRight

        public static byte[] xorAlignRight​(byte[] b1,
                                           byte[] b2)
        XORs byte arrays of different lengths by aligning length of the shortest via adding zeros at beginning
      • merge

        public static byte[] merge​(byte[]... arrays)
        Parameters:
        arrays - - arrays to merge
        Returns:
        - merged array
      • isNullOrZeroArray

        public static boolean isNullOrZeroArray​(byte[] array)
      • isSingleZero

        public static boolean isSingleZero​(byte[] array)
      • difference

        public static Set<byte[]> difference​(Set<byte[]> setA,
                                             Set<byte[]> setB)
      • length

        public static int length​(byte[]... bytes)
      • intsToBytes

        public static byte[] intsToBytes​(int[] arr,
                                         boolean bigEndian)
      • bytesToInts

        public static int[] bytesToInts​(byte[] arr,
                                        boolean bigEndian)
      • bytesToInts

        public static void bytesToInts​(byte[] b,
                                       int[] arr,
                                       boolean bigEndian)
      • intsToBytes

        public static void intsToBytes​(int[] arr,
                                       byte[] b,
                                       boolean bigEndian)
      • bigEndianToShort

        public static short bigEndianToShort​(byte[] bs)
      • bigEndianToShort

        public static short bigEndianToShort​(byte[] bs,
                                             int off)
      • shortToBytes

        public static byte[] shortToBytes​(short n)
      • hexStringToBytes

        public static byte[] hexStringToBytes​(String data)
        Converts string hex representation to data bytes Accepts following hex: - with or without 0x prefix - with no leading 0, like 0xabc - 0x0abc
        Parameters:
        data - String like '0xa5e..' or just 'a5e..'
        Returns:
        decoded bytes array
      • chop

        public static byte[] chop​(byte[] in)
        Chops a 32-byte value into a 16-byte value. Keep in mind the subtlety that a "chopped" bytearray is a different reference from a "unchopped" bytearray, so make no assummptions as to whether this function news the element.
        Returns:
        16-byte value representing the LOWER portion of the original