Packer extension type

A high-performance MessagePack serializer.

Packer uses a BinaryWriter from pro_binary to encode Dart objects into the MessagePack binary format. It utilizes a pool-based buffer strategy via BinaryWriterPool to minimize memory allocations.

Usage:

  1. Create a Packer (acquires a writer from the pool).
  2. Use pack to encode one or more objects.
  3. Call takeBytes to get the result and release the buffer back to the pool.

Example:

final packer = Packer();
packer.pack({'key': 'value', 'list': [1, 2, 3]});
final bytes = packer.takeBytes();
on
  • _PackerState

Constructors

Packer({EncodeExt? encodeExt, int initialBufferSize = 1024})
Creates a new Packer instance.

Properties

bytesWritten int
The number of bytes currently written to the internal buffer.
no setter

Methods

appendRaw(Uint8List bytes) → void
Appends bytes directly to the buffer without any encoding.
dispose() → void
Releases internal resources back to the pool without returning any data.
pack(dynamic value) → void
Encodes value into MessagePack format and writes it to the buffer.
packArray(Iterable? value) → void
Packs a value as a MessagePack array.
packArrayLength(int count) → void
Writes only the MessagePack array header for an array of count elements (fixarray/array16/array32).
packBinary(Uint8List? value) → void
Packs a binary value.
packBool(bool? value) → void
Packs a boolean value.
packDouble(double? value) → void
Packs a double as a 64-bit float.
packExt(int type, void builder(Packer)) → void
Packs a custom extension payload.
packFloat(Float? value) → void
Packs a Float wrapper as a 32-bit float.
packInt(int? value) → void
Packs an integer value.
packMap(Map? value) → void
Packs a Map as a MessagePack map.
packMapLength(int count) → void
Writes only the MessagePack map header for a map of count entries (fixmap/map16/map32).
packNull() → void
Packs a null value.
packRawExtension(int type, Uint8List data) → void
Writes a MessagePack ext format with the given type and data.
packString(String? value) → void
Packs a String value using UTF-8 encoding.
packTimestamp(DateTime? value) → void
Packs a DateTime value using the standard MessagePack timestamp extension.
takeBytes({bool copy = true, bool dispose = true}) Uint8List
Returns the serialized bytes and releases the internal buffer back to the pool.

Static Methods

encode(dynamic value, {EncodeExt? encodeExt, int initialBufferSize = 1024}) Uint8List
Encodes a single value to bytes, owning the full pool lifecycle.
encodeAll(Iterable values, {EncodeExt? encodeExt, int initialBufferSize = 1024}) Uint8List
Encodes a sequence of values into one buffer, owning the pool lifecycle.