Class ByteArray

All Implemented Interfaces:
Reusable, Serializable

public class ByteArray extends ByteArrayInterface implements Serializable
Traditional byte array can have three properties, byte [] data, int offset, and int length. This class is primarily aimed to put these three parameters togather.
See Also:
  • Constructor Details

    • ByteArray

      public ByteArray()
      Default constructor.
    • ByteArray

      public ByteArray(int length)
      Create a byte array of specified length.
      Parameters:
      length - a byte array will be allocated for this length.
    • ByteArray

      public ByteArray(byte[] p_data)
      Create a byte array of specified length and provided array.
      Parameters:
      p_data - a byte array will be allowed with this length and content will be copied.
    • ByteArray

      public ByteArray(byte[] p_data, int offset, int length)
      Create a byte array of specified length and provided array.
      Parameters:
      p_data - a byte array will be allowed with this
      offset - from where the data will be copied
      length - the length of the content will be copied.
  • Method Details

    • reset

      public void reset()
      Reset the byte array to initial value.
      Specified by:
      reset in interface Reusable
      Specified by:
      reset in class ByteArrayInterface
    • copy

      public ByteArray copy(ByteArray other)
      Copy the content from other ByteArray to this instance. Old content of this instance will be overwritten.
      Parameters:
      other - Content of this instance will be copied here.
      Returns:
      this instance after copy is done.
    • copy

      public ByteArray copy(byte[] other)
      Copy the content from other byte [] to this instance. Old content of this instance will be overwritten.
      Specified by:
      copy in class ByteArrayInterface
      Parameters:
      other - Content of this instance will be copied here.
      Returns:
      this instance after copy is done.
    • copy

      public ByteArray copy(byte[] data, int offset, int length)
      Copy the content from other ByteArray to this instance.
      Specified by:
      copy in class ByteArrayInterface
      Parameters:
      data - Content of this instance will be copied here.
      offset - starting index of source byte array.
      length - length of the source byte array.
      Returns:
      this instance after copy is done.
    • copy

      public ByteArray copy(String data)
      Copy the string data to this instance. Old content of this instance will be overwritten.
      Specified by:
      copy in class ByteArrayInterface
      Parameters:
      data - Content of this instance will be copied here.
      Returns:
      this instance after copy is done.
    • append

      public ByteArray append(byte[] data)
      Append the content of the given byte [] into this instance. Content will be appended at the end of this instance.
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      data - content of this byte [] will be copied.
      Returns:
      the
    • append

      public ByteArray append(String str)
      Append the string value in this ByteArray. ASCII byte of each char will be added.
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      str - The value to be appended.
      Returns:
      the instance of this ByteArray
    • append

      public ByteArray append(char ch)
      append all the chars to this instance of ByteArray
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      ch - char to be appended into this instance.
      Returns:
      the instance of this ByteArray
    • append

      public ByteArray append(char... str)
      append all the chars to this instance of ByteArray
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      str - multiple chars to be appended into this instance.
      Returns:
      the instance of this ByteArray
    • appendRandomHexChars

      public ByteArray appendRandomHexChars(int length)
      Append some random hex char at the end of this ByteArrayInterface
      Specified by:
      appendRandomHexChars in class ByteArrayInterface
      Parameters:
      length - Number of hex char to be added.
      Returns:
      the instance of this ByteArrayInterface
    • appendAsHexString

      public ByteArray appendAsHexString(byte[] data, int offset, int length)
      Append the given byte array as hex char at the end of this ByteArrayInterface. 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 this ByteArrayInterface so after adding this instance will contain data = {36 38 36 35 36 63 36 63 36 66}
      Specified by:
      appendAsHexString in class ByteArrayInterface
      Parameters:
      data - byte array whose data will be appended as hex string.
      offset - starting index of byte [] to be appended into this
      length - length of the given byte array to be appended as hex string.
      Returns:
      the instance of this ByteArrayInterface
    • getLength

      public int getLength()
      Get the current length of this ByteArray
      Specified by:
      getLength in class ByteArrayInterface
      Returns:
      the current length of this ByteArray
    • append

      public ByteArray append(byte b)
      Append the byte value into this ByteArray
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      b - byte value to be appended
    • append

      public ByteArray append(int n)
      Append the integer value as printable characters into this ByteArray
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      n - Integer value to be appended
    • append

      public ByteArray append(byte[] a, int p_offset, int p_length)
      Append the given byte array data to this instance.
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      a - byte array to be appended.
      p_offset - starting index of the byte array to be appended.
      p_length - length of the byte array to be appended.
      Returns:
      the instance of this ByteArray
    • append

      public ByteArray append(ByteArrayInterface p)
      Copy the content of the given parameter and append them at the end of this instance.
      Specified by:
      append in class ByteArrayInterface
      Parameters:
      p - content of this parameter will be appended here.
      Returns:
      the instance of this ByteArray
    • hashCode

      public int hashCode()
      Generate the hash code for this instance.
      Overrides:
      hashCode in class Object
      Returns:
      the generated hash code.
    • toString

      public String toString()
      Specified by:
      toString in class ByteArrayInterface