Package org.kendar.sync.lib.buffer
Class ByteContainer
java.lang.Object
org.kendar.sync.lib.buffer.ByteContainer
A container for managing byte arrays with support for dynamic resizing,
reading, writing, and type conversion using custom converters.
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for ByteContainer.ByteContainer(int size) Constructs a ByteContainer with a specified initial size. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the container, resetting all internal states.clone()Creates a shallow copy of the current ByteContainer.static ByteContainercreate()byte[]getBytes()Returns the entire content of the container as a single byte array.intReturns the current read cursor position.intReturns the number of bytes remaining to be read.intReturns the current write-cursor position.byteread()Reads a single byte from the container at the current read cursor position.byte[]read(int length) Reads a specified number of bytes from the current read cursor position.byte[]read(int offset, int length) Reads a range of bytes from the container.byte[]Reads all remaining bytes from the current read cursor to the end.<T> TReads an object of a specified type from the container using a registered converter.<T> TReads an object of a specified type from a specific offset using a registered converter.voidResets the read cursor to the beginning.voidResets the write-cursor to the beginning.intsize()Returns the total size of the ByteContainer.splice(int offset, int length) Removes a portion of the container's data and returns it as a new ByteContainer.withConverters(ByteContainerConverter... offeredConverters) Adds converters to the ByteContainer for handling specific types.voidwrite(byte data) Writes a single byte to the container at the current write-cursor position.write(byte[] data) Writes a byte array to the container at the current write-cursor position.write(byte[] buf, int offset, int length) Writes a portion of a byte array to the container at a specific offset.voidwrite(byte value, int offset) Writes a single byte to the container at a specific offset.<T> voidwriteType(T toWrite) Writes an object to the container using a registered converter.<T> voidwriteType(T toWrite, int offset) Writes an object to the container at a specific offset using a registered converter.
-
Constructor Details
-
ByteContainer
public ByteContainer()Default constructor for ByteContainer. -
ByteContainer
public ByteContainer(int size) Constructs a ByteContainer with a specified initial size.- Parameters:
size- the initial size of the container.
-
-
Method Details
-
create
-
clone
Creates a shallow copy of the current ByteContainer. -
withConverters
Adds converters to the ByteContainer for handling specific types.- Parameters:
offeredConverters- the converters to add.- Returns:
- the current ByteContainer instance.
-
size
public int size()Returns the total size of the ByteContainer.- Returns:
- the size of the container.
-
read
public byte[] read(int offset, int length) Reads a range of bytes from the container.- Parameters:
offset- the starting offset.length- the number of bytes to read.- Returns:
- the read bytes as a byte array.
- Throws:
IndexOutOfBoundsException- if the range exceeds the container size.
-
write
public void write(byte data) Writes a single byte to the container at the current write-cursor position.- Parameters:
data- the byte to write.
-
clear
public void clear()Clears the container, resetting all internal states. -
read
public byte read()Reads a single byte from the container at the current read cursor position.- Returns:
- the read byte.
-
readToEnd
public byte[] readToEnd()Reads all remaining bytes from the current read cursor to the end.- Returns:
- the remaining bytes as a byte array.
-
getRemaining
public int getRemaining()Returns the number of bytes remaining to be read.- Returns:
- the remaining byte count.
-
getWriteCursor
public int getWriteCursor()Returns the current write-cursor position.- Returns:
- the write-cursor position.
-
resetWriteCursor
public void resetWriteCursor()Resets the write-cursor to the beginning. -
resetReadCursor
public void resetReadCursor()Resets the read cursor to the beginning. -
getReadCursor
public int getReadCursor()Returns the current read cursor position.- Returns:
- the read cursor position.
-
read
public byte[] read(int length) Reads a specified number of bytes from the current read cursor position.- Parameters:
length- the number of bytes to read.- Returns:
- the read bytes as a byte array.
- Throws:
IndexOutOfBoundsException- if the range exceeds the container size.
-
writeType
public <T> void writeType(T toWrite) Writes an object to the container using a registered converter.- Type Parameters:
T- the type of the object.- Parameters:
toWrite- the object to write.- Throws:
ClassCastException- if no converter is registered for the object's type.
-
writeType
public <T> void writeType(T toWrite, int offset) Writes an object to the container at a specific offset using a registered converter.- Type Parameters:
T- the type of the object.- Parameters:
toWrite- the object to write.offset- the offset at which to write the object.- Throws:
ClassCastException- if no converter is registered for the object's type.
-
readType
Reads an object of a specified type from the container using a registered converter.- Type Parameters:
T- the type of the object.- Parameters:
type- the class of the object to read.- Returns:
- the read object.
- Throws:
ClassCastException- if no converter is registered for the specified type.
-
readType
Reads an object of a specified type from a specific offset using a registered converter.- Type Parameters:
T- the type of the object.- Parameters:
type- the class of the object to read.offset- the offset at which to read the object.- Returns:
- the read object.
- Throws:
ClassCastException- if no converter is registered for the specified type.
-
write
Writes a byte array to the container at the current write-cursor position.- Parameters:
data- the byte array to write.
-
write
Writes a portion of a byte array to the container at a specific offset.- Parameters:
buf- the byte array to write from.offset- the offset at which to write.length- the number of bytes to write.- Throws:
IllegalArgumentException- if the length exceeds the buffer size.ArrayIndexOutOfBoundsException- if the offset exceeds the container size.
-
write
public void write(byte value, int offset) Writes a single byte to the container at a specific offset.- Parameters:
value- the byte to write.offset- the offset at which to write.
-
splice
Removes a portion of the container's data and returns it as a new ByteContainer.- Parameters:
offset- the starting offset of the portion to remove.length- the length of the portion to remove.- Returns:
- a new ByteContainer containing the removed portion.
- Throws:
IndexOutOfBoundsException- if the range exceeds the container size.
-
getBytes
public byte[] getBytes()Returns the entire content of the container as a single byte array.- Returns:
- the container's content as a byte array.
-