Class ByteArray

java.lang.Object
cl.netswitch.lib.util.ByteArray
All Implemented Interfaces:
Serializable

public final class ByteArray extends Object implements Serializable
A random and sequential accessible array of zero or more bytes.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteArray(byte[] bytes)
    Creates a new ByteArray instance with supplied bytes.
    ByteArray(byte[] bytes, int offset, int length)
    Creates a new ByteArray instance with supplied bytes.
    ByteArray(int size)
    Creates a new ByteArray instance of specified size.
    Creates a new ByteArray instance with supplied ByteBuffer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Sets the values of readIndex and writeIndex to 0.
    byte[]
    Returns the backing byte array of this array.
    int
    Returns the offset of the first byte within the backing byte.
    int
    Returns the number of bytes this array can contain.
    int
    Returns the number of bytes that can be read.
    int
    Gets the value of the readIndex.
    int
    Returns the number of bytes that can be written.
    int
    Gets the value of the writeIndex.
    boolean
    Returns true if at least one byte can be read.
    boolean
    Returns true if at least one byte can be written.
    byte[]
    Reads a 7bit encoded byte array starting at current readIndex.
    int
    Reads a 7bit encoded integer value starting at current readIndex.
    Reads a 7bit encoded string starting at current readIndex.
    int
    Reads a byte at current readIndex.
    int
    readByte(int index)
    Reads a byte at the specified absolute index.
    void
    readBytes(byte[] bytes, int offset, int length)
    Transfers this array's data to the specified destination byte array starting at current readIndex.
    void
    readBytes(int index, byte[] bytes, int offset, int length)
    Transfers this array's data to the specified destination byte array starting at the specified absolute index.
    double
    Reads an double at current readIndex.
    double
    readDouble(int index)
    Reads an double at the specified absolute index.
    float
    Reads an float at current readIndex.
    float
    readFloat(int index)
    Reads an float at the specified absolute index.
    int
    Reads an integer at current readIndex.
    int
    readInt(int index)
    Reads an integer at the specified absolute index.
    long
    Reads a long at current readIndex.
    long
    readLong(int index)
    Reads a long at the specified absolute index.
    short
    Reads a short at current readIndex.
    short
    readShort(int index)
    Reads a short at the specified absolute index.
    void
    setReadIndex(int readIndex)
    Sets the value of the readIndex.
    void
    setReadWriteIndex(int readIndex, int writeIndex)
    Sets the value of the readIndex and writeIndex.
    void
    setWriteIndex(int writeIndex)
    Sets the value of the writeIndex.
    Returns a ByteBuffer with the readable bytes of this array.
    byte[]
    Returns a byte array with the readable bytes of this array.
    Returns a string representation of this array.
    toString(Charset charset)
    Decodes this buffer's readable bytes into a string with the specified character set name.
    void
    write7bitBytes(byte[] bytes)
    Writes the 7bit encoding of the supplied byte array at current writeIndex.
    void
    write7bitInt(int value)
    Writes the 7bit encoding of the supplied integer value at current writeIndex.
    void
    Writes the 7bit encoding of the supplied string at current writeIndex.
    void
    writeByte(int value)
    Writes the supplied byte value at current writeIndex.
    void
    writeByte(int index, int value)
    Writes a byte at the specified absolute index.
    void
    writeBytes(byte[] bytes, int offset, int length)
    Transfers the supplied bytes to this array starting at current writeIndex.
    void
    Transfers the source byte array's readable data to this byte array.
    void
    writeDouble(double value)
    Writes the supplied double value at current writeIndex.
    void
    writeDouble(int index, double value)
    Writes a double at the specified absolute index.
    void
    writeFloat(float value)
    Writes the supplied float value at current writeIndex.
    void
    writeFloat(int index, float value)
    Writes a float at the specified absolute index.
    void
    writeInt(int value)
    Writes the supplied integer value at current writeIndex.
    void
    writeInt(int index, int value)
    Writes an integer at the specified absolute index.
    void
    writeLong(int index, long value)
    Writes a long at the specified absolute index.
    void
    writeLong(long value)
    Writes the supplied long value at current writeIndex.
    void
    writeShort(int value)
    Writes the supplied short value at current writeIndex.
    void
    writeShort(int index, int value)
    Writes a short at the specified absolute index.
    void
    Write the readable bytes of this array to the supplied output stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ByteArray

      public ByteArray(int size)
      Creates a new ByteArray instance of specified size.
      Parameters:
      size - the initial size of the ByteArray.
      Throws:
      IllegalArgumentException - if size is negative.
    • ByteArray

      public ByteArray(ByteBuffer buffer)
      Creates a new ByteArray instance with supplied ByteBuffer.
      Parameters:
      buffer - the source ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
    • ByteArray

      public ByteArray(byte[] bytes)
      Creates a new ByteArray instance with supplied bytes.
      Parameters:
      bytes - the source byte array.
      Throws:
      NullPointerException - if bytes is null.
    • ByteArray

      public ByteArray(byte[] bytes, int offset, int length)
      Creates a new ByteArray instance with supplied bytes.
      Parameters:
      bytes - the source byte array.
      offset - the start index in bytes array.
      length - the number of bytes of bytes array.
      Throws:
      NullPointerException - if bytes is null.
      IllegalArgumentException - if length is negative.
      IndexOutOfBoundsException - if out-of-bounds error occurs.
  • Method Details

    • getCapacity

      public int getCapacity()
      Returns the number of bytes this array can contain.
      Returns:
      the number of bytes this array can contain.
    • getArray

      public byte[] getArray()
      Returns the backing byte array of this array.
      Returns:
      the backing byte array of this array.
    • getArrayOffset

      public int getArrayOffset()
      Returns the offset of the first byte within the backing byte.
      Returns:
      the offset of the first byte within the backing byte.
    • clear

      public void clear()
      Sets the values of readIndex and writeIndex to 0.
    • getReadIndex

      public int getReadIndex()
      Gets the value of the readIndex.
      Returns:
      the value of the readIndex.
    • setReadIndex

      public void setReadIndex(int readIndex)
      Sets the value of the readIndex.
      Parameters:
      readIndex - the new value of readIndex.
      Throws:
      IllegalArgumentException - if readIndex is invalid.
    • getWriteIndex

      public int getWriteIndex()
      Gets the value of the writeIndex.
      Returns:
      the value of the writeIndex.
    • setWriteIndex

      public void setWriteIndex(int writeIndex)
      Sets the value of the writeIndex.
      Parameters:
      writeIndex - the new value of writeIndex.
      Throws:
      IllegalArgumentException - if writeIndex is invalid.
    • setReadWriteIndex

      public void setReadWriteIndex(int readIndex, int writeIndex)
      Sets the value of the readIndex and writeIndex.
      Parameters:
      readIndex - the new value of readIndex.
      writeIndex - the new value of writeIndex.
      Throws:
      IllegalArgumentException - if an index is invalid.
    • getWritableBytes

      public int getWritableBytes()
      Returns the number of bytes that can be written.
      Returns:
      the number of bytes that can be written.
    • isWritable

      public boolean isWritable()
      Returns true if at least one byte can be written.
      Returns:
      true if at least one byte can be written.
    • writeByte

      public void writeByte(int index, int value)
      Writes a byte at the specified absolute index. This method does not modify readIndex or writeIndex.
      Parameters:
      index - the absolute position in the array.
      value - the byte to be written at absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • writeByte

      public void writeByte(int value)
      Writes the supplied byte value at current writeIndex. The value of writeIndex is increased by 1.
      Parameters:
      value - the byte to be written.
    • writeShort

      public void writeShort(int index, int value)
      Writes a short at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      value - the short to be written at absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • writeShort

      public void writeShort(int value)
      Writes the supplied short value at current writeIndex. The value of writeIndex is increased by 2. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      value - the short to be written.
    • writeInt

      public void writeInt(int index, int value)
      Writes an integer at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      value - the integer to be written at absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • writeInt

      public void writeInt(int value)
      Writes the supplied integer value at current writeIndex. The value of writeIndex is increased by 4. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      value - the integer to be written.
    • writeLong

      public void writeLong(int index, long value)
      Writes a long at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      value - the long to be written at absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • writeLong

      public void writeLong(long value)
      Writes the supplied long value at current writeIndex. The value of writeIndex is increased by 8. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      value - the long to be written.
    • writeFloat

      public void writeFloat(int index, float value)
      Writes a float at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      value - the float to be written at absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • writeFloat

      public void writeFloat(float value)
      Writes the supplied float value at current writeIndex. The value of writeIndex is increased by 4. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      value - the float to be written.
    • writeDouble

      public void writeDouble(int index, double value)
      Writes a double at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      value - the double to be written at absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • writeDouble

      public void writeDouble(double value)
      Writes the supplied double value at current writeIndex. The value of writeIndex is increased by 8. The bytes are written in big endian order (most significant to least significant).
      Parameters:
      value - the double to be written.
    • writeBytes

      public void writeBytes(byte[] bytes, int offset, int length)
      Transfers the supplied bytes to this array starting at current writeIndex. The value of writeIndex is increased by the number of the transferred bytes (= length).
      Parameters:
      bytes - the array of bytes to write.
      offset - the start offset in bytes.
      length - the number of bytes to write.
      Throws:
      NullPointerException - if bytes is null.
      IllegalArgumentException - if an argument is invalid.
      IndexOutOfBoundsException - if access outside array bounds.
    • writeBytes

      public void writeBytes(ByteArray barray)
      Transfers the source byte array's readable data to this byte array. The value of writeIndex is increased by the number of the transferred bytes (= barray.getReadableBytes()).
      Parameters:
      barray - the source byte array.
      Throws:
      NullPointerException - if barray is null.
    • write7bitInt

      public void write7bitInt(int value)
      Writes the 7bit encoding of the supplied integer value at current writeIndex. The value of writeIndex is increased by the number of bytes written to the array.
      Parameters:
      value - the non-negative integer value to be written.
      Throws:
      IllegalArgumentException - if value is negative.
    • write7bitBytes

      public void write7bitBytes(byte[] bytes)
      Writes the 7bit encoding of the supplied byte array at current writeIndex. The value of writeIndex is increased by the number of bytes written to the array.
      Parameters:
      bytes - the byte array value to be written.
      Throws:
      NullPointerException - if bytes is null.
    • write7bitString

      public void write7bitString(String str)
      Writes the 7bit encoding of the supplied string at current writeIndex. The value of writeIndex is increased by the number of bytes written to the array.
      Parameters:
      str - the string value to be written.
      Throws:
      NullPointerException - if str is null.
    • getReadableBytes

      public int getReadableBytes()
      Returns the number of bytes that can be read.
      Returns:
      the number of bytes that can be read.
    • isReadable

      public boolean isReadable()
      Returns true if at least one byte can be read.
      Returns:
      true if at least one byte can be read.
    • readByte

      public int readByte(int index)
      Reads a byte at the specified absolute index. This method does not modify readIndex or writeIndex.
      Parameters:
      index - the absolute position in the array.
      Returns:
      the byte at specified absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • readByte

      public int readByte()
      Reads a byte at current readIndex. The value of readIndex is increased by 1.
      Returns:
      the byte at readIndex position.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • readShort

      public short readShort(int index)
      Reads a short at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are read in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      Returns:
      the short at specified absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • readShort

      public short readShort()
      Reads a short at current readIndex. The value of readIndex is increased by 2. The bytes are read in big endian order (most significant to least significant).
      Returns:
      the short at readIndex position.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • readInt

      public int readInt(int index)
      Reads an integer at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are read in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      Returns:
      the integer at specified absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • readInt

      public int readInt()
      Reads an integer at current readIndex. The value of readIndex is increased by 4. The bytes are read in big endian order (most significant to least significant).
      Returns:
      the integer at readIndex position.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • readLong

      public long readLong(int index)
      Reads a long at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are read in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      Returns:
      the long at specified absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • readLong

      public long readLong()
      Reads a long at current readIndex. The value of readIndex is increased by 8. The bytes are read in big endian order (most significant to least significant).
      Returns:
      the long at readIndex position.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • readFloat

      public float readFloat(int index)
      Reads an float at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are read in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      Returns:
      the float at specified absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • readFloat

      public float readFloat()
      Reads an float at current readIndex. The value of readIndex is increased by 4. The bytes are read in big endian order (most significant to least significant).
      Returns:
      the float at readIndex position.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • readDouble

      public double readDouble(int index)
      Reads an double at the specified absolute index. This method does not modify readIndex or writeIndex. The bytes are read in big endian order (most significant to least significant).
      Parameters:
      index - the absolute position in the array.
      Returns:
      the double at specified absolute index.
      Throws:
      IndexOutOfBoundsException - if index is out-of-bounds.
    • readDouble

      public double readDouble()
      Reads an double at current readIndex. The value of readIndex is increased by 8. The bytes are read in big endian order (most significant to least significant).
      Returns:
      the double at readIndex position.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • readBytes

      public void readBytes(int index, byte[] bytes, int offset, int length)
      Transfers this array's data to the specified destination byte array starting at the specified absolute index. This method does not modify readIndex or writeIndex.
      Parameters:
      index - the absolute position in the array.
      bytes - the destination byte array.
      offset - the starting position in the bytes array.
      length - the number of bytes to transfer.
      Throws:
      NullPointerException - if bytes is null.
      IllegalArgumentException - if an argument is invalid.
      IndexOutOfBoundsException - if access outside array bounds.
    • readBytes

      public void readBytes(byte[] bytes, int offset, int length)
      Transfers this array's data to the specified destination byte array starting at current readIndex. The value of readIndex is increased by the number of the transferred bytes (= length).
      Parameters:
      bytes - the destination byte array.
      offset - the starting position in the bytes array.
      length - the number of bytes to transfer.
      Throws:
      NullPointerException - if bytes is null.
      IllegalArgumentException - if an argument is invalid.
      IndexOutOfBoundsException - if access outside array bounds.
    • read7bitInt

      public int read7bitInt()
      Reads a 7bit encoded integer value starting at current readIndex. The value of readIndex is increased by the number of bytes read from the array.
      Returns:
      the value of the 7bit encoded integer.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • read7bitBytes

      public byte[] read7bitBytes()
      Reads a 7bit encoded byte array starting at current readIndex. The value of readIndex is increased by the number of bytes read from the array.
      Returns:
      the value of the 7bit encoded byte array.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • read7bitString

      public String read7bitString()
      Reads a 7bit encoded string starting at current readIndex. The value of readIndex is increased by the number of bytes read from the array.
      Returns:
      the value of the 7bit encoded string.
      Throws:
      IndexOutOfBoundsException - if readIndex is out-of-bounds.
    • toByteBuffer

      public ByteBuffer toByteBuffer()
      Returns a ByteBuffer with the readable bytes of this array.
      Returns:
      a ByteBuffer with the readable bytes of this array.
    • toBytes

      public byte[] toBytes()
      Returns a byte array with the readable bytes of this array.
      Returns:
      a byte array with the readable bytes of this array.
    • toString

      public String toString()
      Returns a string representation of this array. The readable bytes of this array are converted to a string using the ISO-8859-1 character set.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this array.
    • toString

      public String toString(Charset charset)
      Decodes this buffer's readable bytes into a string with the specified character set name. This method does not modify readIndex or writeIndex of this buffer.
      Parameters:
      charset - the character set used to decode array bytes.
      Returns:
      a string representation using specified character set name.
      Throws:
      NullPointerException - if charset is null.
    • writeToStream

      public void writeToStream(OutputStream output) throws IOException
      Write the readable bytes of this array to the supplied output stream.
      Parameters:
      output - the output stream where the bytes will be written.
      Throws:
      NullPointerException - if output is null.
      IOException - if an I/O error occurs.