Encoder.overBuffer constructor

Encoder.overBuffer(
  1. Uint8List buffer, {
  2. int offset = 0,
  3. int depth = maxDepth,
})

Encodes into a caller-supplied buffer with no flush sink โ€” the first required capability of CORELIB_PLAN ยง5.1.

No flush can occur, so nothing is ever handed downstream and nothing is ever dropped: the buffer holds the message, which written returns, or the write that runs out of room throws SofabError.bufferFull. offset leaves room at the front of buffer for a framing header, exactly as in the sink-installed form.

No minimum applies to a sink-less buffer โ€” a minimum is a streaming precondition, and here there is no streaming โ€” so a message that encodes to two bytes fits a two-byte buffer. flush on such an encoder has nowhere to drain to and is a no-op that leaves the bytes in place.

final buf = Uint8List(Person.maxSize);   // sized from the schema
final enc = sofab.Encoder.overBuffer(buf);
person.serialize(enc);
enc.flush();
socket.add(enc.written);                 // the whole message, zero-copy

Implementation

Encoder.overBuffer(Uint8List buffer, {int offset = 0, int depth = maxDepth})
  : _flush = null,
    _buf = buffer,
    _pos = offset,
    _flushStart = offset,
    _pendingSeq = Int32List(_checkDepth(depth)) {
  _checkHandover(buffer.length, offset, streaming: false);
  _bufData = ByteData.sublistView(buffer);
  _fscratchBytes = _fscratch.buffer.asUint8List();
}