Encoder class
Streaming SofaBuffers encoder (CORELIB_PLAN §5.1, §6).
Writes into a fixed output buffer and calls FlushCallback whenever the
buffer fills (or on explicit flush). The buffer can be far smaller than the
message. Supports a start offset (leave room for a framing header) and a
mid-stream buffer swap via installBuffer.
Every buffer the encoder writes into is caller-supplied, so buffer is
required: the corelib allocates no output buffer and never grows or replaces
one it was handed. There is one buffer-ownership model, not two — the layer
that knows how big a message can get is the one that allocates (CORELIB_PLAN
§5.1, "the generated-object layer allocates; the corelib does not"). The
one-shot encodeToBytes is that layer in miniature: it allocates once,
visibly, then drives this encoder like any other caller.
The hot path performs no heap allocation: scalars, headers, strings and array elements are written straight into the caller-owned buffer, and the only state the encoder keeps of its own — the held-back-sequence run and the 8-byte float landing zone — is sized once, in the constructor (CORELIB_PLAN §6.6).
Nested sequences are framed lazily (beginSequenceLazy): the header is held back until the sequence receives content, so a sequence-typed field that equals its declared default emits nothing at all (MESSAGE_SPEC §2). Close with endSequence to let a contentless one vanish, or endSequenceKeep to force the frame out — the wrapper-array element case, where presence carries the array's length (MESSAGE_SPEC §5.1).
A buffer can also be handed over without a sink (Encoder.overBuffer):
then no flush can occur, so the buffer either holds the whole message —
written hands it back — or the write that does not fit throws
SofabError.bufferFull. That is the shape a caller sizing its buffer from a
generated MAX_SIZE wants, and it stays exact: a two-byte message encodes
into a two-byte buffer (CORELIB_PLAN §5.1).
Constructors
- Encoder(FlushCallback _flush, {required Uint8List buffer, int offset = 0, int depth = maxDepth})
-
Encodes into the caller-supplied
buffer, draining it through the flush sink whenever it fills (CORELIB_PLAN §5.1). - Encoder.overBuffer(Uint8List buffer, {int offset = 0, int depth = maxDepth})
-
Encodes into a caller-supplied
bufferwith no flush sink — the first required capability of CORELIB_PLAN §5.1.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- pending → int
-
Bytes written into the current buffer but not yet flushed.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- written → Uint8List
-
The bytes written into the active buffer since it was installed — the
whole message for a sink-less encoder (Encoder.overBuffer) that had room
for it, and whatever has not been flushed yet otherwise.
no setter
Methods
-
beginSequenceLazy(
int id) → void - Opens a nested sequence — a fresh id scope (CORELIB_PLAN §4.9) — whose header is held back until the sequence turns out to have content.
-
endSequence(
) → void -
Closes the current sequence, letting it vanish if it received no
content; otherwise the single byte
0x07(CORELIB_PLAN §4.9). -
endSequenceKeep(
) → void - Closes the current sequence, keeping its frame even when it received no content.
-
flush(
) → void - Drains any buffered bytes downstream (CORELIB_PLAN §5.1). Call once at the end of a message.
-
installBuffer(
Uint8List buffer, {int offset = 0}) → void - Installs a fresh output buffer mid-stream (typically from inside the flush callback) so encoding continues without interruption (CORELIB_PLAN §5.1).
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
{int offset = 0}) → void -
Resets the write position to
offsetso the encoder + its buffer can be reused for the next message without reallocating (hot-path friendly). -
toString(
) → String -
A string representation of this object.
inherited
-
writeBlob(
int id, Uint8List value, [int? length]) → void -
Writes an opaque blob (fixlen subtype blob, CORELIB_PLAN §4.6): the first
lengthbytes ofvalue, all of them when omitted — so an InlineBytes'storageandlengthgo out as they are, uncopied. -
writeBool(
int id, bool value) → void -
Writes a boolean — an unsigned
0/1; booleans have no wire type of their own (CORELIB_PLAN §4.4). -
writeFp32(
int id, double value) → void - Writes an IEEE-754 32-bit float (fixlen subtype fp32, CORELIB_PLAN §4.6).
-
writeFp32Array(
int id, List< double> values, [int? count]) → void -
Writes an array of fp32 values (CORELIB_PLAN §4.8) — the first
countofvalues, all of them when omitted: a single sharedfixlen_word, thencount × 4little-endian bytes. The word is present even when empty so an empty fp32 array stays distinct from an empty fp64 array on the wire. -
writeFp32Bits(
int id, int bits) → void -
Writes an fp32 field from its raw 32-bit IEEE-754 bit pattern (the low 32
bits of
bits), bypassing the float widening that writeFp32 performs. -
writeFp64(
int id, double value) → void - Writes an IEEE-754 64-bit double (fixlen subtype fp64, CORELIB_PLAN §4.6).
-
writeFp64Array(
int id, List< double> values, [int? count]) → void -
Writes an array of fp64 values (CORELIB_PLAN §4.8) — the first
countofvalues, all of them when omitted. -
writeSigned(
int id, int value) → void - Writes a signed integer via zig-zag (CORELIB_PLAN §4.5).
-
writeSignedArray(
int id, List< int> values, [int? count]) → void -
Writes an array of signed integers via zig-zag (CORELIB_PLAN §4.7) — the
first
countofvalues, all of them when omitted. -
writeSignedArrayInRange(
int id, List< int> values, int count, int lo, int hi) → void - The signed twin of writeUnsignedArrayInRange: the bound is compared before the zig-zag step.
-
writeString(
int id, String value) → void - Writes a UTF-8 string (fixlen subtype string, no null terminator). Rejects an unpaired surrogate with SofabError.invalidArgument — strict UTF-8, never lossy (CORELIB_PLAN §6.4).
-
writeStringUtf8(
int id, Uint8List utf8, [int? length]) → void -
Writes a
stringfield from its UTF-8 bytes — the firstlengthofutf8, all of them when omitted — so an InlineString'sstorageandlengthgo out as they are, uncopied and untranscoded. -
writeUnsigned(
int id, int value) → void -
Writes an unsigned integer (CORELIB_PLAN §4.4).
valueis the raw 64-bit bit pattern; pass negative Dart ints to express values ≥ 2^63. -
writeUnsignedArray(
int id, List< int> values, [int? count]) → void -
Writes an array of unsigned integers (CORELIB_PLAN §4.7) — the first
countofvalues, all of them when omitted, so an InlineInt64Array'sstorageandlengthgo out as they are. The declared element width (u8..u64) is an API concern only; the wire carries varints. -
writeUnsignedArrayInRange(
int id, List< int> values, int count, int lo, int hi) → void -
Writes an array of unsigned integers like writeUnsignedArray — the
first
countofvalues— and refuses an element outside the declared widthlo..hiwith SofabError.invalidArgument (CORELIB_PLAN §6.3).
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited