Class MessageBuffer
java.lang.Object
br.net.dd.netherwingcore.common.utilities.MessageBuffer
A utility class for managing a byte buffer for reading and writing messages.
It supports dynamic resizing, reading/writing data, and handling little-endian byte order.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a MessageBuffer with a default initial capacity of 4096 bytes.MessageBuffer(int initialCapacity) Constructs a MessageBuffer with the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionintGets the number of bytes currently available for reading in the buffer.intGets the total capacity of the buffer.intgetLimit()Gets the current limit of the buffer.byte[]getReadPointer(int length) Gets a byte array containing the data at the current read position without advancing the read position.intGets the current read position in the buffer.intGets the remaining capacity of the buffer for writing new data.intGets the current write position in the buffer.intGets the current read position in the buffer.byte[]read(int length) Reads a specified number of bytes from the buffer starting at the current read position.voidreadCompleted(int bytesRead) Advances the read position by the specified number of bytes after a successful read operation.intReads an unsigned short (2 bytes) from the buffer in little-endian order and returns it as an int.voidreset()Resets the buffer by clearing its contents and resetting the read and write positions to zero.voidresize(int newSize) Resizes the buffer to the specified new size.byte[]toArray()Converts the active portion of the buffer (from the current read position to the current write position) into a byte array.voidwrite(byte[] data) Writes the entire byte array to the buffer at the current write position.voidwrite(byte[] data, int length) Writes the specified byte array to the buffer at the current write position.voidwriteCompleted(int bytesWritten) Advances the write position by the specified number of bytes after a successful write operation.voidwriteShortLE(int value) Writes an unsigned short (2 bytes) to the buffer in little-endian order.
-
Constructor Details
-
MessageBuffer
public MessageBuffer(int initialCapacity) Constructs a MessageBuffer with the specified initial capacity.- Parameters:
initialCapacity- The initial capacity of the buffer in bytes.
-
MessageBuffer
public MessageBuffer()Constructs a MessageBuffer with a default initial capacity of 4096 bytes.
-
-
Method Details
-
resize
public void resize(int newSize) Resizes the buffer to the specified new size. If the new size is smaller than the current capacity, it simply adjusts the limit. If it's larger, it creates a new buffer and copies existing data.- Parameters:
newSize- The new size of the buffer in bytes.
-
write
public void write(byte[] data, int length) Writes the specified byte array to the buffer at the current write position.- Parameters:
data- The byte array to write.length- The number of bytes from the array to write.
-
write
public void write(byte[] data) Writes the entire byte array to the buffer at the current write position.- Parameters:
data- The byte array to write.
-
read
public byte[] read(int length) Reads a specified number of bytes from the buffer starting at the current read position.- Parameters:
length- The number of bytes to read.- Returns:
- A byte array containing the read data.
- Throws:
IllegalStateException- If there is not enough data to read.
-
getReadPointer
public byte[] getReadPointer(int length) Gets a byte array containing the data at the current read position without advancing the read position.- Parameters:
length- The number of bytes to get.- Returns:
- A byte array containing the data at the current read position.
- Throws:
IllegalStateException- If there is not enough data to get.
-
readCompleted
public void readCompleted(int bytesRead) Advances the read position by the specified number of bytes after a successful read operation.- Parameters:
bytesRead- The number of bytes that were read.
-
writeCompleted
public void writeCompleted(int bytesWritten) Advances the write position by the specified number of bytes after a successful write operation.- Parameters:
bytesWritten- The number of bytes that were written.
-
getActiveSize
public int getActiveSize()Gets the number of bytes currently available for reading in the buffer.- Returns:
- The number of bytes available for reading.
-
getRemainingSpace
public int getRemainingSpace()Gets the remaining capacity of the buffer for writing new data.- Returns:
- The number of bytes available for writing.
-
getWritePosition
public int getWritePosition()Gets the current read position in the buffer.- Returns:
- The current read position.
-
reset
public void reset()Resets the buffer by clearing its contents and resetting the read and write positions to zero. -
toArray
public byte[] toArray()Converts the active portion of the buffer (from the current read position to the current write position) into a byte array.- Returns:
- A byte array containing the active data in the buffer.
-
readUnsignedShortLE
public int readUnsignedShortLE()Reads an unsigned short (2 bytes) from the buffer in little-endian order and returns it as an int.- Returns:
- The unsigned short value read from the buffer (0 to 65535).
-
writeShortLE
public void writeShortLE(int value) Writes an unsigned short (2 bytes) to the buffer in little-endian order.- Parameters:
value- The unsigned short value to write (0 to 65535).
-
getCapacity
public int getCapacity()Gets the total capacity of the buffer.- Returns:
- The buffer capacity in bytes.
-
getWritePos
public int getWritePos()Gets the current write position in the buffer.- Returns:
- The current write position.
-
getReadPos
public int getReadPos()Gets the current read position in the buffer.- Returns:
- The current read position.
-
getLimit
public int getLimit()Gets the current limit of the buffer.- Returns:
- The buffer limit.
-