Package dev.relism.flash.bytes
Class ByteWriter
java.lang.Object
dev.relism.flash.bytes.ByteWriter
Index-based writer into a growable
byte[] scratch buffer. Every write* method
bounds-checks and grows the backing array only when the write would not otherwise fit —
on an already-warm buffer (the steady-state case: the buffer has already grown to the
connection's high-water mark), no method here allocates.
Callers build a complete message in a ByteWriter-backed scratch buffer and then issue
one bulk write(buffer, 0, length()). The same writer is shared by HTTP/1.1 and HTTP/2.
Lifetime and thread-safety contract
Not thread-safe — exactly one writer at a time, matching every other per-connection scratch object in this codebase (ConnectionScratch, Http1HeaderMap). reset()
repositions this writer to the start of its backing array for the next message; the backing
array itself is never shrunk back down, only grown — the same amortized-to-zero-allocation
growth policy RequestParser's read buffer already uses.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()The backing buffer.intlength()How many bytes have been written since the lastreset().voidreset()Repositions this writer to the start of its buffer, ready for the next message.voidwriteAscii(String s) Writess's ASCII bytes, case preserved.voidWritess's ASCII bytes, lower-cased.voidwriteByte(byte b) voidwriteBytes(byte[] src) voidwriteBytes(byte[] src, int off, int srcLen) voidwriteDecimal(long value) Writesvalue's ASCII decimal digits (no sign — callers write'-'viawriteByte(byte)first if needed).voidwriteHex(int value) Writesvalue's lowercase hex digits, no leading zeros (except forvalue == 0, which writes"0").voidwriteUInt16(int value) Big-endian 16-bit write — an HTTP/2 frame's stream-dependent fields, SETTINGS values, etc.voidwriteUInt24(int value) Big-endian 24-bit write — an HTTP/2 frame header's length field.voidwriteUInt31(int value) Big-endian 31-bit write (top bit always 0) — an HTTP/2 stream identifier.voidwriteUInt32(int value) Big-endian 32-bit write — an HTTP/2 window-size increment, SETTINGS value, etc.
-
Constructor Details
-
ByteWriter
public ByteWriter(int initialCapacity)
-
-
Method Details
-
reset
public void reset()Repositions this writer to the start of its buffer, ready for the next message. -
array
public byte[] array()The backing buffer. Valid content is[0, length())— never assumebuf.length == length(). -
length
public int length()How many bytes have been written since the lastreset(). -
writeByte
public void writeByte(byte b) -
writeBytes
public void writeBytes(byte[] src) -
writeBytes
public void writeBytes(byte[] src, int off, int srcLen) -
writeDecimal
public void writeDecimal(long value) Writesvalue's ASCII decimal digits (no sign — callers write'-'viawriteByte(byte)first if needed).valuemust be non-negative. -
writeHex
public void writeHex(int value) Writesvalue's lowercase hex digits, no leading zeros (except forvalue == 0, which writes"0"). -
writeAsciiLower
Writess's ASCII bytes, lower-cased.smust be ASCII-only. -
writeAscii
Writess's ASCII bytes, case preserved.smust be ASCII-only. Unlikenew String(...).getBytes(UTF_8), writes each character directly into this buffer and avoids an intermediatebyte[]. -
writeUInt16
public void writeUInt16(int value) Big-endian 16-bit write — an HTTP/2 frame's stream-dependent fields, SETTINGS values, etc. -
writeUInt24
public void writeUInt24(int value) Big-endian 24-bit write — an HTTP/2 frame header's length field. -
writeUInt31
public void writeUInt31(int value) Big-endian 31-bit write (top bit always 0) — an HTTP/2 stream identifier. -
writeUInt32
public void writeUInt32(int value) Big-endian 32-bit write — an HTTP/2 window-size increment, SETTINGS value, etc.
-