Class SegmentBinaryWriter
- All Implemented Interfaces:
BinaryWriter
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongposition()Gets the current write offset in bytes.voidposition(long newPosition) Sets the current write offset in bytes.longGets the number of remaining writable bytes.skip(long bytesToSkip) Advances the writer's position by the given number of bytes.wrap(MemorySegment segment) Wraps a MemorySegment, preparing the writer for use and resetting its position.writeBoolean(boolean value) writeByte(byte value) writeBytes(byte[] value) writeDoubleBE(double value) writeDoubleLE(double value) writeFloatBE(float value) writeFloatLE(float value) writeIntBE(int value) writeIntLE(int value) writeLongBE(long value) writeLongLE(long value) writeNullableBoolean(Boolean value) writeNullableByte(Byte value) writeNullableBytes(byte[] value) writeNullableDoubleBE(Double value) writeNullableDoubleLE(Double value) writeNullableFloatBE(Float value) writeNullableFloatLE(Float value) writeNullableIntBE(Integer value) writeNullableIntLE(Integer value) writeNullableLongBE(Long value) writeNullableLongLE(Long value) writeNullableShortBE(Short value) writeNullableString(String value, MemorySegment scratchBuffer) Writes a nullable String using fixed-length encoding.writeSegment(MemorySegment source) Writes all bytes from the source MemorySegment to this writer's underlying sink.writeSegmentRaw(MemorySegment source, long offset, long length) Writes raw bytes from the source MemorySegment without a length prefix.writeShortBE(short value) writeShortLE(short value) writeString(String value, MemorySegment scratchBuffer) Encodes and writes a String with zero heap allocations by using a provided off-heap scratch buffer for the UTF-8 encoding process.writeStringFixedLength(String value) Writes a String using single-pass UTF-8 encoding directly to the target buffer with a fixed 4-byte big-endian length prefix.writeVarInt(int value) Writes a 32-bit integer in a variable-length format.writeVarIntFast(int value) Writes a variable-length integer using an optimized batch-write strategy.writeVarLong(long value) Writes a 64-bit integer in a variable-length format.writeVarLongFast(long value) Writes a variable-length long using an optimized batch-write strategy.
-
Constructor Details
-
SegmentBinaryWriter
public SegmentBinaryWriter()
-
-
Method Details
-
wrap
Wraps a MemorySegment, preparing the writer for use and resetting its position.- Parameters:
segment- The segment to write to.- Returns:
- This writer instance for chaining.
-
position
public long position()Description copied from interface:BinaryWriterGets the current write offset in bytes.- Specified by:
positionin interfaceBinaryWriter
-
position
public void position(long newPosition) Description copied from interface:BinaryWriterSets the current write offset in bytes.- Specified by:
positionin interfaceBinaryWriter
-
remaining
public long remaining()Description copied from interface:BinaryWriterGets the number of remaining writable bytes.- Specified by:
remainingin interfaceBinaryWriter
-
writeByte
- Specified by:
writeBytein interfaceBinaryWriter
-
writeNullableByte
- Specified by:
writeNullableBytein interfaceBinaryWriter
-
writeBoolean
- Specified by:
writeBooleanin interfaceBinaryWriter
-
writeShortBE
- Specified by:
writeShortBEin interfaceBinaryWriter
-
writeIntBE
- Specified by:
writeIntBEin interfaceBinaryWriter
-
writeLongBE
- Specified by:
writeLongBEin interfaceBinaryWriter
-
writeShortLE
- Specified by:
writeShortLEin interfaceBinaryWriter
-
writeIntLE
- Specified by:
writeIntLEin interfaceBinaryWriter
-
writeLongLE
- Specified by:
writeLongLEin interfaceBinaryWriter
-
writeFloatBE
- Specified by:
writeFloatBEin interfaceBinaryWriter
-
writeFloatLE
- Specified by:
writeFloatLEin interfaceBinaryWriter
-
writeVarInt
Description copied from interface:BinaryWriterWrites a 32-bit integer in a variable-length format.- Specified by:
writeVarIntin interfaceBinaryWriter
-
writeVarIntFast
Writes a variable-length integer using an optimized batch-write strategy.This method pre-computes all VarInt bytes and writes them in a single memory operation using a 64-bit write, avoiding the per-byte overhead of the standard
writeVarInt(int)method.Performance: Approximately 5-10% faster than
writeVarInt(int)for typical values by reducing memory operation count from 1-5 to exactly 1.Wire format: Identical to
writeVarInt(int)- fully compatible.- Parameters:
value- The integer value to encode as VarInt.- Returns:
- This writer instance, for chaining.
- See Also:
-
writeVarLongFast
Writes a variable-length long using an optimized batch-write strategy.This method pre-computes all VarLong bytes and writes them in batched memory operations, avoiding the per-byte overhead of the standard
writeVarLong(long)method.- Parameters:
value- The long value to encode as VarLong.- Returns:
- This writer instance, for chaining.
- See Also:
-
writeVarLong
Description copied from interface:BinaryWriterWrites a 64-bit integer in a variable-length format.- Specified by:
writeVarLongin interfaceBinaryWriter
-
writeString
Encodes and writes a String with zero heap allocations by using a provided off-heap scratch buffer for the UTF-8 encoding process.This method uses a two-pass encoding strategy:
- First pass: Encode UTF-8 bytes into the scratch buffer to determine exact byte length
- Second pass: Write VarInt length prefix, then copy encoded bytes to target segment
Wire format: VarInt length prefix (1-5 bytes) followed by UTF-8 encoded bytes.
Trade-offs:
- Pro: Compact wire format (VarInt uses 1 byte for strings < 128 bytes)
- Pro: Compatible with standard wire protocols
- Con: Requires scratch buffer allocation/management
- Con: Two memory operations (encode + copy)
Alternative: For maximum encode performance when a fixed-size length prefix is acceptable, use
writeStringFixedLength(String)which encodes directly to the target buffer in a single pass (approximately 15-25% faster for typical strings).- Specified by:
writeStringin interfaceBinaryWriter- Parameters:
value- The String to write. Must not be null.scratchBuffer- An off-heap MemorySegment used for temporary encoding. Its size must be sufficient to hold the string's UTF-8 bytes (worst case: 3 bytes per char for BMP, 4 bytes for supplementary characters).- Returns:
- This writer instance, for chaining.
- Throws:
IllegalArgumentException- if value or scratchBuffer is null, or if scratch buffer is too small- See Also:
-
writeStringFixedLength
Writes a String using single-pass UTF-8 encoding directly to the target buffer with a fixed 4-byte big-endian length prefix. This method achieves maximum encoding performance by eliminating the scratch buffer and intermediate copy required bywriteString(String, MemorySegment).Wire format: Fixed 4-byte big-endian length prefix followed by UTF-8 encoded bytes.
Algorithm:
- Reserve 4 bytes for length prefix at current position
- Encode UTF-8 bytes directly to target segment with bounds checking
- Write actual byte length back to reserved prefix position
Trade-offs compared to
writeString(String, MemorySegment):- Pro: ~15-25% faster encoding (single pass, no scratch buffer, no copy)
- Pro: No scratch buffer required
- Con: Fixed 4-byte overhead even for small strings (vs 1 byte for VarInt)
- Con: Maximum string size limited to ~2GB (Integer.MAX_VALUE bytes)
Use cases:
- High-frequency encoding in hot paths where performance is critical
- Internal/binary protocols where wire format overhead is acceptable
- Scenarios where scratch buffer management is undesirable
- Parameters:
value- The String to write. Must not be null.- Returns:
- This writer instance, for chaining.
- Throws:
IllegalArgumentException- if value is null or contains unpaired surrogatesIndexOutOfBoundsException- if the target segment lacks capacity for the encoded string- See Also:
-
writeNullableStringFixedLength
Writes a nullable String using fixed-length encoding.Format: 1-byte presence flag, followed by fixed-length string data if present.
- Parameters:
value- The String to write, or null.- Returns:
- This writer instance, for chaining.
- See Also:
-
writeBytes
- Specified by:
writeBytesin interfaceBinaryWriter
-
writeSegment
Description copied from interface:BinaryWriterWrites all bytes from the source MemorySegment to this writer's underlying sink.- Specified by:
writeSegmentin interfaceBinaryWriter- Parameters:
source- The MemorySegment to read from.- Returns:
- This writer instance for chaining.
-
writeSegmentRaw
Description copied from interface:BinaryWriterWrites raw bytes from the source MemorySegment without a length prefix. This is useful when the length is already known or stored elsewhere.- Specified by:
writeSegmentRawin interfaceBinaryWriter- Parameters:
source- The MemorySegment to copy from.offset- Starting offset in the source segment.length- Number of bytes to copy.- Returns:
- This writer instance for chaining.
-
writeNullableString
- Specified by:
writeNullableStringin interfaceBinaryWriter
-
writeNullableIntBE
- Specified by:
writeNullableIntBEin interfaceBinaryWriter
-
writeDoubleBE
- Specified by:
writeDoubleBEin interfaceBinaryWriter
-
writeDoubleLE
- Specified by:
writeDoubleLEin interfaceBinaryWriter
-
writeNullableLongBE
- Specified by:
writeNullableLongBEin interfaceBinaryWriter
-
writeNullableShortBE
- Specified by:
writeNullableShortBEin interfaceBinaryWriter
-
writeNullableBytes
- Specified by:
writeNullableBytesin interfaceBinaryWriter
-
writeNullableDoubleLE
- Specified by:
writeNullableDoubleLEin interfaceBinaryWriter
-
writeNullableBoolean
- Specified by:
writeNullableBooleanin interfaceBinaryWriter
-
writeNullableFloatLE
- Specified by:
writeNullableFloatLEin interfaceBinaryWriter
-
writeNullableIntLE
- Specified by:
writeNullableIntLEin interfaceBinaryWriter
-
writeNullableDoubleBE
- Specified by:
writeNullableDoubleBEin interfaceBinaryWriter
-
writeNullableLongLE
- Specified by:
writeNullableLongLEin interfaceBinaryWriter
-
writeNullableFloatBE
- Specified by:
writeNullableFloatBEin interfaceBinaryWriter
-
skip
Description copied from interface:BinaryWriterAdvances the writer's position by the given number of bytes.- Specified by:
skipin interfaceBinaryWriter- Parameters:
bytesToSkip- The number of bytes to skip.- Returns:
- This writer instance, for chaining.
-