Package com.reve.utils.bytearray
Class ByteArrayUtil
java.lang.Object
com.reve.utils.bytearray.ByteArrayUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final char[]static final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanarraysEqual(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength) Check if the given two byte arrays are equal or not.static booleanarraysEqual(byte[] a, int aOffset, int aLength, String b) Check the equality of the contents of byte array with the stringstatic StringbyteArrayToHexString(byte[] bytes) Create hex string from the given byte arraystatic StringbyteArrayToHexString(byte[] bytes, int offset, int len) Create hex string from the given byte arraystatic intcopyAsHexString(byte[] source, int sIndex, int sLength, byte[] dest, int dIndex) Copy the given byte source array as hex char at the end of the given dest array For example if the given byte [] contains word 'hello' into it, the byte array data = {68, 65, 6c, 6c, 6f} after calling this method it will append '68656c6f' at the end of the given dest array so after adding dest instance will contain data = {36 38 36 35 36 63 36 63 36 66}static doublegetDoubleValue(byte[] data, int index, int numberOfBytes) Get a double value from the given byte array and given index.static intgetIntValue(byte[] data, int index, int numberOfBytes) Get an integer value from the given byte array and given index using given number of bytes.static longgetLongValue(byte[] data, int index, int numberOfBytes) Get a long value from the given byte array and given index using given number of bytes.static intgetRandomData(byte[] array) Fill the given byte array with random data.static intgetRandomData(byte[] array, int offset, int len) Fill the given byte array with random data.static intgetRandomHexData(byte[] data) Populate the given byte [] array from the given index with randomhexArraychar of given length.static intgetRandomHexData(byte[] data, int index, int length) Populate the given byte [] array from the given index with randomhexArraychar of given length.static StringgetString(byte[] data, int index, int length) When string value is sent from remote side, null string is sent using string 'null'.static byte[]Fill the given byte array with converted byte array from given hex string.static inthexStringToByteArray(String s, byte[] destByteArray, int offset) Fill the given byte array with converted byte array from given hex string.static intputValue(byte[] data, int index, long value, int numberOfBytes) Put a long value in the given index.
-
Field Details
-
LENGTH_OF_INTEGER
public static final int LENGTH_OF_INTEGER- See Also:
-
LENGTH_OF_LONG
public static final int LENGTH_OF_LONG- See Also:
-
hexArray
public static final char[] hexArray
-
-
Constructor Details
-
ByteArrayUtil
public ByteArrayUtil()
-
-
Method Details
-
byteArrayToHexString
Create hex string from the given byte array- Parameters:
bytes- byte array from which hex string will be created.- Returns:
- the String hex value of the given byte array.
-
byteArrayToHexString
Create hex string from the given byte array- Parameters:
bytes- byte array from which hex string will be created.offset- starting index of the byte arraylen- length of the byte array that should be converted to hex string.- Returns:
- the String hex value of the given byte array.
-
hexStringToByteArray
Fill the given byte array with converted byte array from given hex string.- Parameters:
s- The hex string that will be converted to byte array.- Returns:
- the length of converted byte array.
-
hexStringToByteArray
Fill the given byte array with converted byte array from given hex string.- Parameters:
s- The hex string that will be converted to byte array.destByteArray- This byte array will be filled with the converted bytes from given hex string.offset- starting index of the byte array to be filled.- Returns:
- the length of converted byte array.
-
getRandomData
public static int getRandomData(byte[] array) Fill the given byte array with random data.- Parameters:
array- The byte array to be filled.- Returns:
- the new index of the byte array after filling with random data.
-
getRandomData
public static int getRandomData(byte[] array, int offset, int len) Fill the given byte array with random data.- Parameters:
array- The byte array to be filled.offset- Starting index of the byte array to be filled.len- total number of bytes to be filled.- Returns:
- the new index of the byte array after filling with random data.
-
getRandomHexData
public static int getRandomHexData(byte[] data) Populate the given byte [] array from the given index with randomhexArraychar of given length.- Parameters:
data- this array will be populated with random hex bytes.- Returns:
- the number of the appended hex bytes.
-
getRandomHexData
public static int getRandomHexData(byte[] data, int index, int length) Populate the given byte [] array from the given index with randomhexArraychar of given length.- Parameters:
data- this array will be populated with random hex bytes.index- starting index of the byte array to be populatedlength- number or hex byte to be appended.- Returns:
- the number of the appended hex bytes.
-
copyAsHexString
public static int copyAsHexString(byte[] source, int sIndex, int sLength, byte[] dest, int dIndex) Copy the given byte source array as hex char at the end of the given dest array For example if the given byte [] contains word 'hello' into it, the byte array data = {68, 65, 6c, 6c, 6f} after calling this method it will append '68656c6f' at the end of the given dest array so after adding dest instance will contain data = {36 38 36 35 36 63 36 63 36 66}- Parameters:
source- ths byte [] will be copied to dest arraysIndex- source byte [] starting indexsLength- source byte [] length to be copieddest- source byte [] will be copied into this array.dIndex- starting index of dest byte []- Returns:
- the number of bytes copied into dest array.
-
putValue
public static int putValue(byte[] data, int index, long value, int numberOfBytes) Put a long value in the given index. This method will not append any type or length, it will just put the value.- Parameters:
data- byte array where the value should be put.index- starting index of the byte array where the value should be put.value- value that should be put in the given byte array at given index. Value will be added in little-endian format usingLENGTH_OF_LONGbytes.numberOfBytes- Defines the number of bytes required to put the value into byte array.- Returns:
- the new index of the byte array after putting the value.
-
getIntValue
public static int getIntValue(byte[] data, int index, int numberOfBytes) Get an integer value from the given byte array and given index using given number of bytes.- Parameters:
data- byte array from where value will be extracted.index- starting index of the valuenumberOfBytes- Defines the number of bytes that represents the value into byte array.- Returns:
- the parsed integer value from the given byte array.
-
getLongValue
public static long getLongValue(byte[] data, int index, int numberOfBytes) Get a long value from the given byte array and given index using given number of bytes.- Parameters:
data- byte array from where value will be extracted.index- starting index of the valuenumberOfBytes- Defines the number of bytes that represents the value into byte array.- Returns:
- the parsed long value from the given byte array.
-
getDoubleValue
public static double getDoubleValue(byte[] data, int index, int numberOfBytes) Get a double value from the given byte array and given index.- Parameters:
data- byte array from where value will be extracted.index- starting index of the valuenumberOfBytes- Defines the number of bytes that represents the value into byte array.- Returns:
- the parsed double value from the given byte array.
-
getString
When string value is sent from remote side, null string is sent using string 'null'. So, if we find a String 'null' we must return null instead. This method will do the checking. We could have done that other way, like, If the string value null, then don't send it. In that case, we would have to check if a certain parameter has arrived or not, if not arrived, then we must reset that value to null. The second way, looks more complex to me. That's why not implemented.- Parameters:
data- byte array from where string value will be extracted.index- starting index of the string value.length- length of the string value.- Returns:
- the String value from the given byte array, if the String value is 'null' a null string will be returned instead.
-
arraysEqual
public static boolean arraysEqual(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength) Check if the given two byte arrays are equal or not.- Parameters:
a- first byte arrayaOffset- starting index of first byte arrayaLength- length of first byte arrayb- second byte arraybOffset- starting index of second byte arraybLength- length of second byte array- Returns:
- true, if the two byte arrays are equal at given specification, otherwise, return false.
-
arraysEqual
Check the equality of the contents of byte array with the string- Parameters:
a- instance of the byte arrayaOffset- offset of the byte arrayaLength- length of the byte arrayb- String to be compared.- Returns:
- true if the byte array contents have same values as the given String in each corresponding index.
-