Class ByteArray
java.lang.Object
cl.netswitch.lib.util.ByteArray
- All Implemented Interfaces:
Serializable
A random and sequential accessible array of zero or more bytes.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionByteArray(byte[] bytes) Creates a newByteArrayinstance with suppliedbytes.ByteArray(byte[] bytes, int offset, int length) Creates a newByteArrayinstance with suppliedbytes.ByteArray(int size) Creates a newByteArrayinstance of specifiedsize.ByteArray(ByteBuffer buffer) Creates a newByteArrayinstance with suppliedByteBuffer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Sets the values ofreadIndexandwriteIndexto 0.byte[]getArray()Returns the backing byte array of this array.intReturns the offset of the first byte within the backing byte.intReturns the number of bytes this array can contain.intReturns the number of bytes that can be read.intGets the value of thereadIndex.intReturns the number of bytes that can be written.intGets the value of thewriteIndex.booleanReturnstrueif at least one byte can be read.booleanReturnstrueif at least one byte can be written.byte[]Reads a 7bit encoded byte array starting at currentreadIndex.intReads a 7bit encoded integer value starting at currentreadIndex.Reads a 7bit encoded string starting at currentreadIndex.intreadByte()Reads a byte at currentreadIndex.intreadByte(int index) Reads a byte at the specified absoluteindex.voidreadBytes(byte[] bytes, int offset, int length) Transfers this array's data to the specified destination byte array starting at currentreadIndex.voidreadBytes(int index, byte[] bytes, int offset, int length) Transfers this array's data to the specified destination byte array starting at the specified absoluteindex.doubleReads an double at currentreadIndex.doublereadDouble(int index) Reads an double at the specified absoluteindex.floatReads an float at currentreadIndex.floatreadFloat(int index) Reads an float at the specified absoluteindex.intreadInt()Reads an integer at currentreadIndex.intreadInt(int index) Reads an integer at the specified absoluteindex.longreadLong()Reads a long at currentreadIndex.longreadLong(int index) Reads a long at the specified absoluteindex.shortReads a short at currentreadIndex.shortreadShort(int index) Reads a short at the specified absoluteindex.voidsetReadIndex(int readIndex) Sets the value of thereadIndex.voidsetReadWriteIndex(int readIndex, int writeIndex) Sets the value of thereadIndexandwriteIndex.voidsetWriteIndex(int writeIndex) Sets the value of thewriteIndex.Returns aByteBufferwith the readable bytes of this array.byte[]toBytes()Returns a byte array with the readable bytes of this array.toString()Returns a string representation of this array.Decodes this buffer's readable bytes into a string with the specified character set name.voidwrite7bitBytes(byte[] bytes) Writes the 7bit encoding of the supplied byte array at currentwriteIndex.voidwrite7bitInt(int value) Writes the 7bit encoding of the supplied integervalueat currentwriteIndex.voidwrite7bitString(String str) Writes the 7bit encoding of the supplied string at currentwriteIndex.voidwriteByte(int value) Writes the supplied bytevalueat currentwriteIndex.voidwriteByte(int index, int value) Writes a byte at the specified absoluteindex.voidwriteBytes(byte[] bytes, int offset, int length) Transfers the suppliedbytesto this array starting at currentwriteIndex.voidwriteBytes(ByteArray barray) Transfers the source byte array's readable data to this byte array.voidwriteDouble(double value) Writes the supplied doublevalueat currentwriteIndex.voidwriteDouble(int index, double value) Writes a double at the specified absoluteindex.voidwriteFloat(float value) Writes the supplied floatvalueat currentwriteIndex.voidwriteFloat(int index, float value) Writes a float at the specified absoluteindex.voidwriteInt(int value) Writes the supplied integervalueat currentwriteIndex.voidwriteInt(int index, int value) Writes an integer at the specified absoluteindex.voidwriteLong(int index, long value) Writes a long at the specified absoluteindex.voidwriteLong(long value) Writes the supplied longvalueat currentwriteIndex.voidwriteShort(int value) Writes the supplied shortvalueat currentwriteIndex.voidwriteShort(int index, int value) Writes a short at the specified absoluteindex.voidwriteToStream(OutputStream output) Write the readable bytes of this array to the supplied output stream.
-
Constructor Details
-
ByteArray
public ByteArray(int size) Creates a newByteArrayinstance of specifiedsize.- Parameters:
size- the initial size of theByteArray.- Throws:
IllegalArgumentException- ifsizeis negative.
-
ByteArray
Creates a newByteArrayinstance with suppliedByteBuffer.- Parameters:
buffer- the sourceByteBuffer.- Throws:
NullPointerException- ifbufferisnull.
-
ByteArray
public ByteArray(byte[] bytes) Creates a newByteArrayinstance with suppliedbytes.- Parameters:
bytes- the source byte array.- Throws:
NullPointerException- ifbytesisnull.
-
ByteArray
public ByteArray(byte[] bytes, int offset, int length) Creates a newByteArrayinstance with suppliedbytes.- Parameters:
bytes- the source byte array.offset- the start index inbytesarray.length- the number of bytes ofbytesarray.- Throws:
NullPointerException- ifbytesisnull.IllegalArgumentException- iflengthis 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 ofreadIndexandwriteIndexto 0. -
getReadIndex
public int getReadIndex()Gets the value of thereadIndex.- Returns:
- the value of the
readIndex.
-
setReadIndex
public void setReadIndex(int readIndex) Sets the value of thereadIndex.- Parameters:
readIndex- the new value ofreadIndex.- Throws:
IllegalArgumentException- ifreadIndexis invalid.
-
getWriteIndex
public int getWriteIndex()Gets the value of thewriteIndex.- Returns:
- the value of the
writeIndex.
-
setWriteIndex
public void setWriteIndex(int writeIndex) Sets the value of thewriteIndex.- Parameters:
writeIndex- the new value ofwriteIndex.- Throws:
IllegalArgumentException- ifwriteIndexis invalid.
-
setReadWriteIndex
public void setReadWriteIndex(int readIndex, int writeIndex) Sets the value of thereadIndexandwriteIndex.- Parameters:
readIndex- the new value ofreadIndex.writeIndex- the new value ofwriteIndex.- 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()Returnstrueif at least one byte can be written.- Returns:
trueif at least one byte can be written.
-
writeByte
public void writeByte(int index, int value) Writes a byte at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex.- Parameters:
index- the absolute position in the array.value- the byte to be written at absoluteindex.- Throws:
IndexOutOfBoundsException- ifindexis out-of-bounds.
-
writeByte
public void writeByte(int value) Writes the supplied bytevalueat currentwriteIndex. The value ofwriteIndexis increased by 1.- Parameters:
value- the byte to be written.
-
writeShort
public void writeShort(int index, int value) Writes a short at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex. 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 absoluteindex.- Throws:
IndexOutOfBoundsException- ifindexis out-of-bounds.
-
writeShort
public void writeShort(int value) Writes the supplied shortvalueat currentwriteIndex. The value ofwriteIndexis 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 absoluteindex. This method does not modifyreadIndexorwriteIndex. 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 absoluteindex.- Throws:
IndexOutOfBoundsException- ifindexis out-of-bounds.
-
writeInt
public void writeInt(int value) Writes the supplied integervalueat currentwriteIndex. The value ofwriteIndexis 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 absoluteindex. This method does not modifyreadIndexorwriteIndex. 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 absoluteindex.- Throws:
IndexOutOfBoundsException- ifindexis out-of-bounds.
-
writeLong
public void writeLong(long value) Writes the supplied longvalueat currentwriteIndex. The value ofwriteIndexis 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 absoluteindex. This method does not modifyreadIndexorwriteIndex. 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 absoluteindex.- Throws:
IndexOutOfBoundsException- ifindexis out-of-bounds.
-
writeFloat
public void writeFloat(float value) Writes the supplied floatvalueat currentwriteIndex. The value ofwriteIndexis 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 absoluteindex. This method does not modifyreadIndexorwriteIndex. 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 absoluteindex.- Throws:
IndexOutOfBoundsException- ifindexis out-of-bounds.
-
writeDouble
public void writeDouble(double value) Writes the supplied doublevalueat currentwriteIndex. The value ofwriteIndexis 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 suppliedbytesto this array starting at currentwriteIndex. The value ofwriteIndexis increased by the number of the transferred bytes (=length).- Parameters:
bytes- the array of bytes to write.offset- the start offset inbytes.length- the number of bytes to write.- Throws:
NullPointerException- ifbytesisnull.IllegalArgumentException- if an argument is invalid.IndexOutOfBoundsException- if access outside array bounds.
-
writeBytes
Transfers the source byte array's readable data to this byte array. The value ofwriteIndexis increased by the number of the transferred bytes (=barray.getReadableBytes()).- Parameters:
barray- the source byte array.- Throws:
NullPointerException- ifbarrayisnull.
-
write7bitInt
public void write7bitInt(int value) Writes the 7bit encoding of the supplied integervalueat currentwriteIndex. The value ofwriteIndexis increased by the number of bytes written to the array.- Parameters:
value- the non-negative integer value to be written.- Throws:
IllegalArgumentException- ifvalueis negative.
-
write7bitBytes
public void write7bitBytes(byte[] bytes) Writes the 7bit encoding of the supplied byte array at currentwriteIndex. The value ofwriteIndexis increased by the number of bytes written to the array.- Parameters:
bytes- the byte array value to be written.- Throws:
NullPointerException- ifbytesisnull.
-
write7bitString
Writes the 7bit encoding of the supplied string at currentwriteIndex. The value ofwriteIndexis increased by the number of bytes written to the array.- Parameters:
str- the string value to be written.- Throws:
NullPointerException- ifstrisnull.
-
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()Returnstrueif at least one byte can be read.- Returns:
trueif at least one byte can be read.
-
readByte
public int readByte(int index) Reads a byte at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex.- Parameters:
index- the absolute position in the array.- Returns:
- the byte at specified absolute
index. - Throws:
IndexOutOfBoundsException- ifindexis out-of-bounds.
-
readByte
public int readByte()Reads a byte at currentreadIndex. The value ofreadIndexis increased by 1.- Returns:
- the byte at
readIndexposition. - Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
readShort
public short readShort(int index) Reads a short at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex. 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- ifindexis out-of-bounds.
-
readShort
public short readShort()Reads a short at currentreadIndex. The value ofreadIndexis increased by 2. The bytes are read in big endian order (most significant to least significant).- Returns:
- the short at
readIndexposition. - Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
readInt
public int readInt(int index) Reads an integer at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex. 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- ifindexis out-of-bounds.
-
readInt
public int readInt()Reads an integer at currentreadIndex. The value ofreadIndexis increased by 4. The bytes are read in big endian order (most significant to least significant).- Returns:
- the integer at
readIndexposition. - Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
readLong
public long readLong(int index) Reads a long at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex. 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- ifindexis out-of-bounds.
-
readLong
public long readLong()Reads a long at currentreadIndex. The value ofreadIndexis increased by 8. The bytes are read in big endian order (most significant to least significant).- Returns:
- the long at
readIndexposition. - Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
readFloat
public float readFloat(int index) Reads an float at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex. 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- ifindexis out-of-bounds.
-
readFloat
public float readFloat()Reads an float at currentreadIndex. The value ofreadIndexis increased by 4. The bytes are read in big endian order (most significant to least significant).- Returns:
- the float at
readIndexposition. - Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
readDouble
public double readDouble(int index) Reads an double at the specified absoluteindex. This method does not modifyreadIndexorwriteIndex. 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- ifindexis out-of-bounds.
-
readDouble
public double readDouble()Reads an double at currentreadIndex. The value ofreadIndexis increased by 8. The bytes are read in big endian order (most significant to least significant).- Returns:
- the double at
readIndexposition. - Throws:
IndexOutOfBoundsException- ifreadIndexis 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 absoluteindex. This method does not modifyreadIndexorwriteIndex.- Parameters:
index- the absolute position in the array.bytes- the destination byte array.offset- the starting position in thebytesarray.length- the number of bytes to transfer.- Throws:
NullPointerException- ifbytesisnull.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 currentreadIndex. The value ofreadIndexis increased by the number of the transferred bytes (=length).- Parameters:
bytes- the destination byte array.offset- the starting position in thebytesarray.length- the number of bytes to transfer.- Throws:
NullPointerException- ifbytesisnull.IllegalArgumentException- if an argument is invalid.IndexOutOfBoundsException- if access outside array bounds.
-
read7bitInt
public int read7bitInt()Reads a 7bit encoded integer value starting at currentreadIndex. The value ofreadIndexis increased by the number of bytes read from the array.- Returns:
- the value of the 7bit encoded integer.
- Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
read7bitBytes
public byte[] read7bitBytes()Reads a 7bit encoded byte array starting at currentreadIndex. The value ofreadIndexis increased by the number of bytes read from the array.- Returns:
- the value of the 7bit encoded byte array.
- Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
read7bitString
Reads a 7bit encoded string starting at currentreadIndex. The value ofreadIndexis increased by the number of bytes read from the array.- Returns:
- the value of the 7bit encoded string.
- Throws:
IndexOutOfBoundsException- ifreadIndexis out-of-bounds.
-
toByteBuffer
Returns aByteBufferwith the readable bytes of this array.- Returns:
- a
ByteBufferwith 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
-
toString
Decodes this buffer's readable bytes into a string with the specified character set name. This method does not modifyreadIndexorwriteIndexof this buffer.- Parameters:
charset- the character set used to decode array bytes.- Returns:
- a string representation using specified character set name.
- Throws:
NullPointerException- ifcharsetisnull.
-
writeToStream
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- ifoutputisnull.IOException- if an I/O error occurs.
-