Class SegmentBinaryReader

java.lang.Object
express.mvp.roray.utils.memory.SegmentBinaryReader
All Implemented Interfaces:
BinaryReader

public final class SegmentBinaryReader extends Object implements BinaryReader
A high-performance, zero-copy BinaryReader implementation using MemorySegment. This class is stateful and not thread-safe. A single instance should be used by a single thread at a time. It can be reused by calling the wrap() method.
  • Constructor Details

    • SegmentBinaryReader

      public SegmentBinaryReader()
      Creates a reader that is not yet backed by a segment. Call wrap() before use.
  • Method Details

    • wrap

      public SegmentBinaryReader wrap(MemorySegment segment)
      Wraps a MemorySegment, preparing the reader for use and resetting its position. This allows a single reader instance to be reused for multiple segments.
      Parameters:
      segment - The segment to read from.
      Returns:
      This reader instance for chaining.
    • position

      public long position()
      Description copied from interface: BinaryReader
      Gets the current read offset in bytes.
      Specified by:
      position in interface BinaryReader
    • position

      public void position(long newPosition)
      Description copied from interface: BinaryReader
      Sets the current read offset in bytes.
      Specified by:
      position in interface BinaryReader
    • skip

      public void skip(long bytesToSkip)
      Description copied from interface: BinaryReader
      Skips a specified number of bytes.
      Specified by:
      skip in interface BinaryReader
    • remaining

      public long remaining()
      Description copied from interface: BinaryReader
      Gets the number of remaining readable bytes.
      Specified by:
      remaining in interface BinaryReader
    • readByte

      public byte readByte()
      Specified by:
      readByte in interface BinaryReader
    • readBoolean

      public boolean readBoolean()
      Specified by:
      readBoolean in interface BinaryReader
    • readShortBE

      public short readShortBE()
      Specified by:
      readShortBE in interface BinaryReader
    • readIntBE

      public int readIntBE()
      Specified by:
      readIntBE in interface BinaryReader
    • readLongBE

      public long readLongBE()
      Specified by:
      readLongBE in interface BinaryReader
    • readFloatBE

      public float readFloatBE()
      Specified by:
      readFloatBE in interface BinaryReader
    • readDoubleBE

      public double readDoubleBE()
      Specified by:
      readDoubleBE in interface BinaryReader
    • readShortLE

      public short readShortLE()
      Specified by:
      readShortLE in interface BinaryReader
    • readIntLE

      public int readIntLE()
      Specified by:
      readIntLE in interface BinaryReader
    • readLongLE

      public long readLongLE()
      Specified by:
      readLongLE in interface BinaryReader
    • readFloatLE

      public float readFloatLE()
      Specified by:
      readFloatLE in interface BinaryReader
    • readDoubleLE

      public double readDoubleLE()
      Specified by:
      readDoubleLE in interface BinaryReader
    • readVarInt

      public int readVarInt()
      Description copied from interface: BinaryReader
      Reads a variable-length 32-bit integer (efficient for small numbers).
      Specified by:
      readVarInt in interface BinaryReader
    • readVarLong

      public long readVarLong()
      Description copied from interface: BinaryReader
      Reads a variable-length 64-bit integer (efficient for small numbers).
      Specified by:
      readVarLong in interface BinaryReader
    • readString

      public void readString(Utf8View viewToPopulate)
      Reads a VarInt-prefixed UTF-8 string into the provided Utf8View without allocating any objects on the heap.
      Specified by:
      readString in interface BinaryReader
      Parameters:
      viewToPopulate - A reusable view object that will be configured to point to the string data within the reader's segment.
    • readNullableString

      public boolean readNullableString(Utf8View viewToPopulate)
      Reads a nullable, presence-bit prefixed UTF-8 string.
      Specified by:
      readNullableString in interface BinaryReader
      Parameters:
      viewToPopulate - A reusable view object.
      Returns:
      true if the string was present (and the view was populated), false if the string was null.
    • readStringFixedLength

      public void readStringFixedLength(Utf8View viewToPopulate)
      Reads a fixed-length prefixed UTF-8 string into the provided Utf8View without allocating any objects on the heap. This method is the counterpart to SegmentBinaryWriter.writeStringFixedLength(String).

      Wire format: 4-byte big-endian length prefix followed by UTF-8 encoded bytes.

      Parameters:
      viewToPopulate - A reusable view object that will be configured to point to the string data within the reader's segment.
      Throws:
      IllegalArgumentException - if viewToPopulate is null or length is negative
      IndexOutOfBoundsException - if string length exceeds remaining bytes
      See Also:
    • readNullableStringFixedLength

      public boolean readNullableStringFixedLength(Utf8View viewToPopulate)
      Reads a nullable, presence-bit prefixed UTF-8 string with fixed-length encoding.
      Parameters:
      viewToPopulate - A reusable view object.
      Returns:
      true if the string was present (and the view was populated), false if the string was null.
      See Also:
    • readBytes

      public byte[] readBytes()
      Specified by:
      readBytes in interface BinaryReader
    • readNullableByte

      public Byte readNullableByte()
      Specified by:
      readNullableByte in interface BinaryReader
    • readNullableShortBE

      public Short readNullableShortBE()
      Specified by:
      readNullableShortBE in interface BinaryReader
    • readNullableIntBE

      public Integer readNullableIntBE()
      Specified by:
      readNullableIntBE in interface BinaryReader
    • readNullableLongBE

      public Long readNullableLongBE()
      Specified by:
      readNullableLongBE in interface BinaryReader
    • readNullableShortLE

      public Short readNullableShortLE()
      Specified by:
      readNullableShortLE in interface BinaryReader
    • readNullableIntLE

      public Integer readNullableIntLE()
      Specified by:
      readNullableIntLE in interface BinaryReader
    • readNullableLongLE

      public Long readNullableLongLE()
      Specified by:
      readNullableLongLE in interface BinaryReader
    • readNullableFloatLE

      public Float readNullableFloatLE()
      Specified by:
      readNullableFloatLE in interface BinaryReader
    • readNullableDoubleLE

      public Double readNullableDoubleLE()
      Specified by:
      readNullableDoubleLE in interface BinaryReader
    • readNullableBytes

      public byte[] readNullableBytes()
      Specified by:
      readNullableBytes in interface BinaryReader
    • readSegment

      public MemorySegment readSegment(long length)
      Description copied from interface: BinaryReader
      Reads a slice of the underlying data without copying it to the heap. This is a highly efficient way to process a sub-section of the data.
      Specified by:
      readSegment in interface BinaryReader
      Parameters:
      length - The number of bytes in the slice.
      Returns:
      A MemorySegment view of the requested data.