BinaryWriterScatterGather extension

Scatter-gather (zero-copy) output methods for BinaryWriter.

Instead of copying every payload into the writer's growing buffer, these methods let the writer accumulate an ordered list of segments — its own small owned buffers (headers, scalar fields) interleaved with borrowed views (writeView, no copy) — and yield them as a chunk list (takeChunks) or stream them to a sink (writeTo). This is the classic writev/iovec pattern, ideal for network protocols that emit large borrowed payloads (e.g. an HTTP/2 DATA frame body) that must not be copied.

The segment list is lazy — allocated only on the first writeView — so code that never calls writeView behaves exactly as before, with zero overhead on the contiguous path.

Example:

w
  ..writeUint24(payload.length)  // frame header (owned)
  ..writeUint8(0x0)              // type = DATA
  ..writeUint8(endStream ? 0x1 : 0)
  ..writeUint32(streamId)
  ..writeView(payload);         // header sealed as a segment, body borrowed
w.writeTo(socket.add);          // ordered segments → socket, no payload copy
on

Properties

pendingLength → int

Available on BinaryWriter, provided by the BinaryWriterScatterGather extension

The total number of bytes buffered across all sealed scatter-gather segments plus the current unsealed tail.
no setter

Methods

takeChunks() → List<Uint8List>

Available on BinaryWriter, provided by the BinaryWriterScatterGather extension

Returns the ordered output as a list of segments (owned buffers + borrowed views) and resets the writer (like takeBytes).
writeTo(void sink(List<int> bytes)) → void

Available on BinaryWriter, provided by the BinaryWriterScatterGather extension

Streams the ordered segments to sink (e.g. socket.add) and resets the writer. Equivalent to for (final s in takeChunks()) sink(s);.
writeView(Uint8List view) → void

Available on BinaryWriter, provided by the BinaryWriterScatterGather extension

Appends view to the output as a borrowed segment — its bytes are not copied.