Class ByteArrayUtil

java.lang.Object
com.reve.utils.bytearray.ByteArrayUtil

public class ByteArrayUtil extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char[]
     
    static final int
     
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    static boolean
    arraysEqual(byte[] a, int aOffset, int aLength, String b)
    Check the equality of the contents of byte array with the string
    static String
    byteArrayToHexString(byte[] bytes)
    Create hex string from the given byte array
    static String
    byteArrayToHexString(byte[] bytes, int offset, int len)
    Create hex string from the given byte array
    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}
    static double
    getDoubleValue(byte[] data, int index, int numberOfBytes)
    Get a double value from the given byte array and given index.
    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.
    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.
    static int
    getRandomData(byte[] array)
    Fill the given byte array with random data.
    static int
    getRandomData(byte[] array, int offset, int len)
    Fill the given byte array with random data.
    static int
    getRandomHexData(byte[] data)
    Populate the given byte [] array from the given index with random hexArray char of given length.
    static int
    getRandomHexData(byte[] data, int index, int length)
    Populate the given byte [] array from the given index with random hexArray char of given length.
    static String
    getString(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 int
    hexStringToByteArray(String s, byte[] destByteArray, int offset)
    Fill the given byte array with converted byte array from given hex string.
    static int
    putValue(byte[] data, int index, long value, int numberOfBytes)
    Put a long value in the given index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static String byteArrayToHexString(byte[] bytes)
      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

      public static String byteArrayToHexString(byte[] bytes, int offset, int len)
      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 array
      len - length of the byte array that should be converted to hex string.
      Returns:
      the String hex value of the given byte array.
    • hexStringToByteArray

      public static byte[] hexStringToByteArray(String s)
      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

      public static int hexStringToByteArray(String s, byte[] destByteArray, int offset)
      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 random hexArray char 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 random hexArray char of given length.
      Parameters:
      data - this array will be populated with random hex bytes.
      index - starting index of the byte array to be populated
      length - 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 array
      sIndex - source byte [] starting index
      sLength - source byte [] length to be copied
      dest - 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 using LENGTH_OF_LONG bytes.
      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 value
      numberOfBytes - 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 value
      numberOfBytes - 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 value
      numberOfBytes - Defines the number of bytes that represents the value into byte array.
      Returns:
      the parsed double value from the given byte array.
    • getString

      public static String getString(byte[] data, int index, int length)
      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 array
      aOffset - starting index of first byte array
      aLength - length of first byte array
      b - second byte array
      bOffset - starting index of second byte array
      bLength - length of second byte array
      Returns:
      true, if the two byte arrays are equal at given specification, otherwise, return false.
    • arraysEqual

      public static boolean arraysEqual(byte[] a, int aOffset, int aLength, String b)
      Check the equality of the contents of byte array with the string
      Parameters:
      a - instance of the byte array
      aOffset - offset of the byte array
      aLength - length of the byte array
      b - String to be compared.
      Returns:
      true if the byte array contents have same values as the given String in each corresponding index.