Class FrameWriteBuffer

java.lang.Object
dev.relism.flash.http2.frame.FrameWriteBuffer

public final class FrameWriteBuffer extends Object
Serializes HTTP/2 frames into a ByteWriter scratch buffer with the standard length-back-patching technique: beginFrame(dev.relism.flash.http2.frame.FrameType, int, int) writes a 9-byte header with a placeholder length, the caller writes the payload directly through writer() (the same ByteWriter), and endFrame() rewrites the length once it is known — the payload size is rarely known before it is serialized (an HPACK-encoded header block, in particular, has no cheap way to be measured in advance). issues one bulk write, rather than streaming bytes as they are produced: streaming would require knowing the length before the first byte goes out, which back-patching deliberately avoids needing.

Usage


 FrameWriteBuffer out = new FrameWriteBuffer(new ByteWriter(4096));
 out.beginFrame(FrameType.SETTINGS, 0, 0);
 out.writer().writeUInt16(SETTINGS_MAX_CONCURRENT_STREAMS);
 out.writer().writeUInt32(100);
 out.endFrame();
 // out.writer().array()[0, out.writer().length()) now holds one complete, correctly-lengthed frame
 

Multiple frames, one buffer

beginFrame(dev.relism.flash.http2.frame.FrameType, int, int)/endFrame() pairs may be repeated on the same instance without a ByteWriter.reset() between them — each pair appends one more complete frame after whatever was already written, which is exactly what Http2FrameWriter.write(dev.relism.flash.http2.frame.WriteIntent) wants for a single bulk write covering several frames (e.g. HEADERS followed immediately by its first DATA frame).

Thread-safety

Not thread-safe — exactly one writer at a time, the same convention every other per-connection scratch object in this codebase follows.