encodeToBytes static method
Encodes a whole message to a fresh Uint8List (the 90 %-case convenience, CORELIB_PLAN §6.1). Internally this is just the streaming path with a collecting sink, proving the one-shot helper is a thin wrapper.
This is the one place the package allocates output storage, and it does
so as a caller would: it allocates a scratch buffer of bufferSize bytes
here, explicitly, and hands it to the encoder like any other caller
(CORELIB_PLAN §5.1, "the generated-object layer allocates; the corelib does
not" — the unbounded shape, a scratch buffer plus an appending sink). The
encoder itself allocates nothing: a caller that owns its storage uses the
streaming constructor or Encoder.overBuffer and never comes through here.
Implementation
static Uint8List encodeToBytes(
void Function(Encoder enc) build, {
int bufferSize = 4096,
int offset = 0,
}) {
final builder = BytesBuilder(copy: true);
final enc = Encoder(
builder.add,
buffer: Uint8List(bufferSize),
offset: offset,
);
build(enc);
enc.flush();
return builder.toBytes();
}