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
ConstructorDescriptionByteArray
(byte[] bytes) Creates a newByteArray
instance with suppliedbytes
.ByteArray
(byte[] bytes, int offset, int length) Creates a newByteArray
instance with suppliedbytes
.ByteArray
(int size) Creates a newByteArray
instance of specifiedsize
.ByteArray
(ByteBuffer buffer) Creates a newByteArray
instance with suppliedByteBuffer
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Sets the values ofreadIndex
andwriteIndex
to 0.byte[]
getArray()
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 thereadIndex
.int
Returns the number of bytes that can be written.int
Gets the value of thewriteIndex
.boolean
Returnstrue
if at least one byte can be read.boolean
Returnstrue
if at least one byte can be written.byte[]
Reads a 7bit encoded byte array starting at currentreadIndex
.int
Reads a 7bit encoded integer value starting at currentreadIndex
.Reads a 7bit encoded string starting at currentreadIndex
.int
readByte()
Reads a byte at currentreadIndex
.int
readByte
(int index) Reads a byte at the specified absoluteindex
.void
readBytes
(byte[] bytes, int offset, int length) Transfers this array's data to the specified destination byte array starting at currentreadIndex
.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
.double
Reads an double at currentreadIndex
.double
readDouble
(int index) Reads an double at the specified absoluteindex
.float
Reads an float at currentreadIndex
.float
readFloat
(int index) Reads an float at the specified absoluteindex
.int
readInt()
Reads an integer at currentreadIndex
.int
readInt
(int index) Reads an integer at the specified absoluteindex
.long
readLong()
Reads a long at currentreadIndex
.long
readLong
(int index) Reads a long at the specified absoluteindex
.short
Reads a short at currentreadIndex
.short
readShort
(int index) Reads a short at the specified absoluteindex
.void
setReadIndex
(int readIndex) Sets the value of thereadIndex
.void
setReadWriteIndex
(int readIndex, int writeIndex) Sets the value of thereadIndex
andwriteIndex
.void
setWriteIndex
(int writeIndex) Sets the value of thewriteIndex
.Returns aByteBuffer
with 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.void
write7bitBytes
(byte[] bytes) Writes the 7bit encoding of the supplied byte array at currentwriteIndex
.void
write7bitInt
(int value) Writes the 7bit encoding of the supplied integervalue
at currentwriteIndex
.void
write7bitString
(String str) Writes the 7bit encoding of the supplied string at currentwriteIndex
.void
writeByte
(int value) Writes the supplied bytevalue
at currentwriteIndex
.void
writeByte
(int index, int value) Writes a byte at the specified absoluteindex
.void
writeBytes
(byte[] bytes, int offset, int length) Transfers the suppliedbytes
to this array starting at currentwriteIndex
.void
writeBytes
(ByteArray barray) Transfers the source byte array's readable data to this byte array.void
writeDouble
(double value) Writes the supplied doublevalue
at currentwriteIndex
.void
writeDouble
(int index, double value) Writes a double at the specified absoluteindex
.void
writeFloat
(float value) Writes the supplied floatvalue
at currentwriteIndex
.void
writeFloat
(int index, float value) Writes a float at the specified absoluteindex
.void
writeInt
(int value) Writes the supplied integervalue
at currentwriteIndex
.void
writeInt
(int index, int value) Writes an integer at the specified absoluteindex
.void
writeLong
(int index, long value) Writes a long at the specified absoluteindex
.void
writeLong
(long value) Writes the supplied longvalue
at currentwriteIndex
.void
writeShort
(int value) Writes the supplied shortvalue
at currentwriteIndex
.void
writeShort
(int index, int value) Writes a short at the specified absoluteindex
.void
writeToStream
(OutputStream output) Write the readable bytes of this array to the supplied output stream.
-
Constructor Details
-
ByteArray
public ByteArray(int size) Creates a newByteArray
instance of specifiedsize
.- Parameters:
size
- the initial size of theByteArray
.- Throws:
IllegalArgumentException
- ifsize
is negative.
-
ByteArray
Creates a newByteArray
instance with suppliedByteBuffer
.- Parameters:
buffer
- the sourceByteBuffer
.- Throws:
NullPointerException
- ifbuffer
isnull
.
-
ByteArray
public ByteArray(byte[] bytes) Creates a newByteArray
instance with suppliedbytes
.- Parameters:
bytes
- the source byte array.- Throws:
NullPointerException
- ifbytes
isnull
.
-
ByteArray
public ByteArray(byte[] bytes, int offset, int length) Creates a newByteArray
instance with suppliedbytes
.- Parameters:
bytes
- the source byte array.offset
- the start index inbytes
array.length
- the number of bytes ofbytes
array.- Throws:
NullPointerException
- ifbytes
isnull
.IllegalArgumentException
- iflength
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 ofreadIndex
andwriteIndex
to 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
- ifreadIndex
is 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
- ifwriteIndex
is invalid.
-
setReadWriteIndex
public void setReadWriteIndex(int readIndex, int writeIndex) Sets the value of thereadIndex
andwriteIndex
.- 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()Returnstrue
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
.- Parameters:
index
- the absolute position in the array.value
- the byte to be written at absoluteindex
.- Throws:
IndexOutOfBoundsException
- ifindex
is out-of-bounds.
-
writeByte
public void writeByte(int value) Writes the supplied bytevalue
at currentwriteIndex
. The value ofwriteIndex
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
writeShort
public void writeShort(int value) Writes the supplied shortvalue
at currentwriteIndex
. The value ofwriteIndex
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
writeInt
public void writeInt(int value) Writes the supplied integervalue
at currentwriteIndex
. The value ofwriteIndex
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
writeLong
public void writeLong(long value) Writes the supplied longvalue
at currentwriteIndex
. The value ofwriteIndex
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
writeFloat
public void writeFloat(float value) Writes the supplied floatvalue
at currentwriteIndex
. The value ofwriteIndex
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
writeDouble
public void writeDouble(double value) Writes the supplied doublevalue
at currentwriteIndex
. The value ofwriteIndex
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 suppliedbytes
to this array starting at currentwriteIndex
. The value ofwriteIndex
is 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
- ifbytes
isnull
.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 ofwriteIndex
is increased by the number of the transferred bytes (=barray.getReadableBytes()
).- Parameters:
barray
- the source byte array.- Throws:
NullPointerException
- ifbarray
isnull
.
-
write7bitInt
public void write7bitInt(int value) Writes the 7bit encoding of the supplied integervalue
at currentwriteIndex
. The value ofwriteIndex
is increased by the number of bytes written to the array.- Parameters:
value
- the non-negative integer value to be written.- Throws:
IllegalArgumentException
- ifvalue
is negative.
-
write7bitBytes
public void write7bitBytes(byte[] bytes) Writes the 7bit encoding of the supplied byte array at currentwriteIndex
. The value ofwriteIndex
is increased by the number of bytes written to the array.- Parameters:
bytes
- the byte array value to be written.- Throws:
NullPointerException
- ifbytes
isnull
.
-
write7bitString
Writes the 7bit encoding of the supplied string at currentwriteIndex
. The value ofwriteIndex
is increased by the number of bytes written to the array.- Parameters:
str
- the string value to be written.- Throws:
NullPointerException
- ifstr
isnull
.
-
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()Returnstrue
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
.- Parameters:
index
- the absolute position in the array.- Returns:
- the byte at specified absolute
index
. - Throws:
IndexOutOfBoundsException
- ifindex
is out-of-bounds.
-
readByte
public int readByte()Reads a byte at currentreadIndex
. The value ofreadIndex
is increased by 1.- Returns:
- the byte at
readIndex
position. - Throws:
IndexOutOfBoundsException
- ifreadIndex
is out-of-bounds.
-
readShort
public short readShort(int index) Reads a short at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
readShort
public short readShort()Reads a short at currentreadIndex
. The value ofreadIndex
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
- ifreadIndex
is out-of-bounds.
-
readInt
public int readInt(int index) Reads an integer at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
readInt
public int readInt()Reads an integer at currentreadIndex
. The value ofreadIndex
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
- ifreadIndex
is out-of-bounds.
-
readLong
public long readLong(int index) Reads a long at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
readLong
public long readLong()Reads a long at currentreadIndex
. The value ofreadIndex
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
- ifreadIndex
is out-of-bounds.
-
readFloat
public float readFloat(int index) Reads an float at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
readFloat
public float readFloat()Reads an float at currentreadIndex
. The value ofreadIndex
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
- ifreadIndex
is out-of-bounds.
-
readDouble
public double readDouble(int index) Reads an double at the specified absoluteindex
. This method does not modifyreadIndex
orwriteIndex
. 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
- ifindex
is out-of-bounds.
-
readDouble
public double readDouble()Reads an double at currentreadIndex
. The value ofreadIndex
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
- ifreadIndex
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 absoluteindex
. This method does not modifyreadIndex
orwriteIndex
.- Parameters:
index
- the absolute position in the array.bytes
- the destination byte array.offset
- the starting position in thebytes
array.length
- the number of bytes to transfer.- Throws:
NullPointerException
- ifbytes
isnull
.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 ofreadIndex
is increased by the number of the transferred bytes (=length
).- Parameters:
bytes
- the destination byte array.offset
- the starting position in thebytes
array.length
- the number of bytes to transfer.- Throws:
NullPointerException
- ifbytes
isnull
.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 ofreadIndex
is increased by the number of bytes read from the array.- Returns:
- the value of the 7bit encoded integer.
- Throws:
IndexOutOfBoundsException
- ifreadIndex
is out-of-bounds.
-
read7bitBytes
public byte[] read7bitBytes()Reads a 7bit encoded byte array starting at currentreadIndex
. The value ofreadIndex
is increased by the number of bytes read from the array.- Returns:
- the value of the 7bit encoded byte array.
- Throws:
IndexOutOfBoundsException
- ifreadIndex
is out-of-bounds.
-
read7bitString
Reads a 7bit encoded string starting at currentreadIndex
. The value ofreadIndex
is increased by the number of bytes read from the array.- Returns:
- the value of the 7bit encoded string.
- Throws:
IndexOutOfBoundsException
- ifreadIndex
is out-of-bounds.
-
toByteBuffer
Returns aByteBuffer
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
Returns a string representation of this array. The readable bytes of this array are converted to a string using theISO-8859-1
character set. -
toString
Decodes this buffer's readable bytes into a string with the specified character set name. This method does not modifyreadIndex
orwriteIndex
of this buffer.- Parameters:
charset
- the character set used to decode array bytes.- Returns:
- a string representation using specified character set name.
- Throws:
NullPointerException
- ifcharset
isnull
.
-
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
- ifoutput
isnull
.IOException
- if an I/O error occurs.
-