Class ByteContainer

java.lang.Object
org.kendar.sync.lib.buffer.ByteContainer

public class ByteContainer extends Object
A container for managing byte arrays with support for dynamic resizing, reading, writing, and type conversion using custom converters.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor for ByteContainer.
    ByteContainer(int size)
    Constructs a ByteContainer with a specified initial size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the container, resetting all internal states.
    Creates a shallow copy of the current ByteContainer.
     
    byte[]
    Returns the entire content of the container as a single byte array.
    int
    Returns the current read cursor position.
    int
    Returns the number of bytes remaining to be read.
    int
    Returns the current write-cursor position.
    byte
    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> T
    readType(Class<T> type)
    Reads an object of a specified type from the container using a registered converter.
    <T> T
    readType(Class<T> type, int offset)
    Reads an object of a specified type from a specific offset using a registered converter.
    void
    Resets the read cursor to the beginning.
    void
    Resets the write-cursor to the beginning.
    int
    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.
    void
    write(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.
    void
    write(byte value, int offset)
    Writes a single byte to the container at a specific offset.
    <T> void
    writeType(T toWrite)
    Writes an object to the container using a registered converter.
    <T> void
    writeType(T toWrite, int offset)
    Writes an object to the container at a specific offset using a registered converter.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static ByteContainer create()
    • clone

      public ByteContainer clone()
      Creates a shallow copy of the current ByteContainer.
      Overrides:
      clone in class Object
      Returns:
      a cloned ByteContainer instance.
    • withConverters

      public ByteContainer withConverters(ByteContainerConverter... offeredConverters)
      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

      public <T> T readType(Class<T> type)
      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

      public <T> T readType(Class<T> type, int offset)
      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

      public ByteContainer write(byte[] data)
      Writes a byte array to the container at the current write-cursor position.
      Parameters:
      data - the byte array to write.
    • write

      public ByteContainer write(byte[] buf, int offset, int length)
      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

      public ByteContainer splice(int offset, int length)
      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.