Package org.aion.base.util
Class ByteUtil
- java.lang.Object
-
- org.aion.base.util.ByteUtil
-
public class ByteUtil extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static byte[]EMPTY_BYTE_ARRAYstatic byte[]EMPTY_HALFWORDstatic StringEMPTY_STRINGstatic byte[]EMPTY_WORDstatic byte[]ZERO_BYTE_ARRAY
-
Constructor Summary
Constructors Constructor Description ByteUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]and(byte[] b1, byte[] b2)static byte[]appendByte(byte[] bytes, byte b)Creates a copy of bytes and appends b to the end of itstatic shortbigEndianToShort(byte[] bs)static shortbigEndianToShort(byte[] bs, int off)static byte[]bigIntegerToBytes(BigInteger value)Omitting sign indication byte.static byte[]bigIntegerToBytes(BigInteger b, int numBytes)The regularBigInteger.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.static byte[]bigIntegerToBytesSigned(BigInteger b, int numBytes)static intbyteArrayToInt(byte[] b)Cast hex encoded value from byte[] to intstatic longbyteArrayToLong(byte[] b)Cast hex encoded value from byte[] to longstatic BigIntegerbytesToBigInteger(byte[] bb)static int[]bytesToInts(byte[] arr, boolean bigEndian)static voidbytesToInts(byte[] b, int[] arr, boolean bigEndian)static byte[]calcPacketLength(byte[] msg)Calculate packet lengthstatic byte[]chop(byte[] in)Chops a 32-byte value into a 16-byte value.static byte[]copyToArray(BigInteger value)Utility function to copy a byte array into a new byte array with given size.static Set<byte[]>difference(Set<byte[]> setA, Set<byte[]> setB)static byte[]encodeDataList(Object... args)encode the values and concatenate togetherstatic byte[]encodeValFor32Bits(Object arg)static intfirstNonZeroByte(byte[] data)static intgetBit(byte[] data, int pos)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 - 0x0abcstatic booleanincrement(byte[] bytes)increment byte array as a number until max is reachedstatic byte[]intsToBytes(int[] arr, boolean bigEndian)static voidintsToBytes(int[] arr, byte[] b, boolean bigEndian)static byte[]intToBytes(int val)Converts int value into a byte array.static byte[]intToBytesBE(int val)Converts and int value to a byte array (Big Endian) @ param valstatic byte[]intToBytesLE(int val)Converts and int value to a byte array (Little Endian) @ param valstatic byte[]intToBytesNoLeadZeroes(int val)Converts a int value into a byte array.static booleanisNullOrZeroArray(byte[] array)static booleanisSingleZero(byte[] array)static intlength(byte[]... bytes)static byte[]longToBytes(long val)Converts a long value into a byte array.static byte[]longToBytesLE(long val)static byte[]longToBytesNoLeadZeroes(long val)Converts a long value into a byte array.static intmatchingNibbleLength(byte[] a, byte[] b)Returns the amount of nibbles that match each other from 0 ...static byte[]merge(byte[]... arrays)static StringnibblesToPrettyString(byte[] nibbles)Turn nibbles to a pretty looking output stringstatic intnumBytes(String val)Calculate the number of bytes need to encode the numberstatic StringoneByteToHexString(byte value)static byte[]or(byte[] b1, byte[] b2)static byte[]setBit(byte[] data, int pos, int val)static byte[]shortToBytes(short n)static byte[]stripLeadingZeroes(byte[] data)static bytetoByte(byte[] b)Used in conjunction w/ RLP, casts to bytestatic StringtoHexString(byte[] data)Convert a byte-array into a hex String.
Works similar toHex.toHexString(byte[])but allows fornullstatic StringtoHexStringWithPrefix(byte[] data)static voidtoLEByteArray(byte[] toConvert)Perform an in-place conversion of a byte array from big endian to little endian.static byte[]xor(byte[] b1, byte[] b2)static byte[]xorAlignRight(byte[] b1, byte[] b2)XORs byte arrays of different lengths by aligning length of the shortest via adding zeros at beginning
-
-
-
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
-
EMPTY_STRING
public static final String EMPTY_STRING
- See Also:
- Constant Field Values
-
-
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 regularBigInteger.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 arraynumBytes- 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 ofBigIntegers.asUnsignedByteArray(BigInteger)
we use this custom method to avoid an empty array in case of BigInteger.ZERO- Parameters:
value- - any big integer number. Anull-value will returnnull- 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 inputb- - 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 toHex.toHexString(byte[])but allows fornull- Parameters:
data- - byte-array to convert to a hex-string- Returns:
- hex representation of the data.
Returns an empty String if the input isnullTODO: 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 intLimited 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 longLimited 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 stringExample. [ 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)
-
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
-
-