Class SegmentBinaryReader
java.lang.Object
express.mvp.roray.utils.memory.SegmentBinaryReader
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongposition()Gets the current read offset in bytes.voidposition(long newPosition) Sets the current read offset in bytes.booleanbytereadByte()byte[]doubledoublefloatfloatintintlonglongbyte[]booleanreadNullableString(Utf8View viewToPopulate) Reads a nullable, presence-bit prefixed UTF-8 string.booleanreadNullableStringFixedLength(Utf8View viewToPopulate) Reads a nullable, presence-bit prefixed UTF-8 string with fixed-length encoding.readSegment(long length) Reads a slice of the underlying data without copying it to the heap.shortshortvoidreadString(Utf8View viewToPopulate) Reads a VarInt-prefixed UTF-8 string into the provided Utf8View without allocating any objects on the heap.voidreadStringFixedLength(Utf8View viewToPopulate) Reads a fixed-length prefixed UTF-8 string into the provided Utf8View without allocating any objects on the heap.intReads a variable-length 32-bit integer (efficient for small numbers).longReads a variable-length 64-bit integer (efficient for small numbers).longGets the number of remaining readable bytes.voidskip(long bytesToSkip) Skips a specified number of bytes.wrap(MemorySegment segment) Wraps a MemorySegment, preparing the reader for use and resetting its position.
-
Constructor Details
-
SegmentBinaryReader
public SegmentBinaryReader()Creates a reader that is not yet backed by a segment. Call wrap() before use.
-
-
Method Details
-
wrap
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:BinaryReaderGets the current read offset in bytes.- Specified by:
positionin interfaceBinaryReader
-
position
public void position(long newPosition) Description copied from interface:BinaryReaderSets the current read offset in bytes.- Specified by:
positionin interfaceBinaryReader
-
skip
public void skip(long bytesToSkip) Description copied from interface:BinaryReaderSkips a specified number of bytes.- Specified by:
skipin interfaceBinaryReader
-
remaining
public long remaining()Description copied from interface:BinaryReaderGets the number of remaining readable bytes.- Specified by:
remainingin interfaceBinaryReader
-
readByte
public byte readByte()- Specified by:
readBytein interfaceBinaryReader
-
readBoolean
public boolean readBoolean()- Specified by:
readBooleanin interfaceBinaryReader
-
readShortBE
public short readShortBE()- Specified by:
readShortBEin interfaceBinaryReader
-
readIntBE
public int readIntBE()- Specified by:
readIntBEin interfaceBinaryReader
-
readLongBE
public long readLongBE()- Specified by:
readLongBEin interfaceBinaryReader
-
readFloatBE
public float readFloatBE()- Specified by:
readFloatBEin interfaceBinaryReader
-
readDoubleBE
public double readDoubleBE()- Specified by:
readDoubleBEin interfaceBinaryReader
-
readShortLE
public short readShortLE()- Specified by:
readShortLEin interfaceBinaryReader
-
readIntLE
public int readIntLE()- Specified by:
readIntLEin interfaceBinaryReader
-
readLongLE
public long readLongLE()- Specified by:
readLongLEin interfaceBinaryReader
-
readFloatLE
public float readFloatLE()- Specified by:
readFloatLEin interfaceBinaryReader
-
readDoubleLE
public double readDoubleLE()- Specified by:
readDoubleLEin interfaceBinaryReader
-
readVarInt
public int readVarInt()Description copied from interface:BinaryReaderReads a variable-length 32-bit integer (efficient for small numbers).- Specified by:
readVarIntin interfaceBinaryReader
-
readVarLong
public long readVarLong()Description copied from interface:BinaryReaderReads a variable-length 64-bit integer (efficient for small numbers).- Specified by:
readVarLongin interfaceBinaryReader
-
readString
Reads a VarInt-prefixed UTF-8 string into the provided Utf8View without allocating any objects on the heap.- Specified by:
readStringin interfaceBinaryReader- Parameters:
viewToPopulate- A reusable view object that will be configured to point to the string data within the reader's segment.
-
readNullableString
Reads a nullable, presence-bit prefixed UTF-8 string.- Specified by:
readNullableStringin interfaceBinaryReader- 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
Reads a fixed-length prefixed UTF-8 string into the provided Utf8View without allocating any objects on the heap. This method is the counterpart toSegmentBinaryWriter.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 negativeIndexOutOfBoundsException- if string length exceeds remaining bytes- See Also:
-
readNullableStringFixedLength
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:
readBytesin interfaceBinaryReader
-
readNullableByte
- Specified by:
readNullableBytein interfaceBinaryReader
-
readNullableShortBE
- Specified by:
readNullableShortBEin interfaceBinaryReader
-
readNullableIntBE
- Specified by:
readNullableIntBEin interfaceBinaryReader
-
readNullableLongBE
- Specified by:
readNullableLongBEin interfaceBinaryReader
-
readNullableShortLE
- Specified by:
readNullableShortLEin interfaceBinaryReader
-
readNullableIntLE
- Specified by:
readNullableIntLEin interfaceBinaryReader
-
readNullableLongLE
- Specified by:
readNullableLongLEin interfaceBinaryReader
-
readNullableFloatLE
- Specified by:
readNullableFloatLEin interfaceBinaryReader
-
readNullableDoubleLE
- Specified by:
readNullableDoubleLEin interfaceBinaryReader
-
readNullableBytes
public byte[] readNullableBytes()- Specified by:
readNullableBytesin interfaceBinaryReader
-
readSegment
Description copied from interface:BinaryReaderReads 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:
readSegmentin interfaceBinaryReader- Parameters:
length- The number of bytes in the slice.- Returns:
- A MemorySegment view of the requested data.
-