BinaryWriter extension type

A high-performance binary writer for encoding data into a byte buffer.

Provides methods for writing various data types including:

  • Variable-length integers (VarInt, ZigZag)
  • Fixed-width integers (8, 16, 32, 64-bit signed and unsigned)
  • Floating-point numbers (32 and 64-bit)
  • Byte arrays
  • UTF-8 encoded strings

The writer automatically expands its internal buffer as needed.

Example:

final writer = BinaryWriter();
// Write various data types
writer.writeUint32(42);
writer.writeFloat64(3.14);
// Write length-prefixed string
final text = 'Hello, World!';
writer.writeVarString(text);
// Extract bytes and optionally reuse writer
final bytes = writer.takeBytes(); // Resets writer for reuse
// or: final bytes = writer.toBytes(); // Keeps writer state
on
  • _WriterState
Available extensions

Constructors

BinaryWriter({int initialBufferSize = 128})
Creates a new BinaryWriter with the specified initial buffer size.

Methods

call(List<int> bytes) → void

Available on BinaryWriter, provided by the BinaryWriterCore extension

Writes a sequence of bytes.
getFloat32(int position, [Endian endian = Endian.big]) double

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 32-bit floating-point number at the specified position without changing the current write position.
getFloat64(int position, [Endian endian = Endian.big]) double

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 64-bit floating-point number at the specified position without changing the current write position.
getInt16(int position, [Endian endian = Endian.big]) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 16-bit signed integer at the specified position without changing the current write position.
getInt32(int position, [Endian endian = Endian.big]) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 32-bit signed integer at the specified position without changing the current write position.
getInt64(int position, [Endian endian = Endian.big]) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 64-bit signed integer at the specified position without changing the current write position.
getInt8(int position) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads an 8-bit signed integer at the specified position without changing the current write position.
getUint16(int position, [Endian endian = Endian.big]) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 16-bit unsigned integer at the specified position without changing the current write position.
getUint32(int position, [Endian endian = Endian.big]) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 32-bit unsigned integer at the specified position without changing the current write position.
getUint64(int position, [Endian endian = Endian.big]) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads a 64-bit unsigned integer at the specified position without changing the current write position.
getUint8(int position) int

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Reads an 8-bit unsigned value at the specified position without changing the current write position.
reserve(int count) int

Available on BinaryWriter, provided by the BinaryWriterPosition extension

Reserves count bytes for later backpatching and returns the starting offset of the reserved block.
reset() → void

Available on BinaryWriter, provided by the BinaryWriterCore extension

Resets the writer to its initial state, discarding all written data.
seek(int position) → void

Available on BinaryWriter, provided by the BinaryWriterPosition extension

Sets the write position to the specified byte offset.
setFloat32(int position, double value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 32-bit floating-point number at the specified position without changing the current write position.
setFloat64(int position, double value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 64-bit floating-point number at the specified position without changing the current write position.
setInt16(int position, int value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 16-bit signed integer at the specified position without changing the current write position.
setInt32(int position, int value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 32-bit signed integer at the specified position without changing the current write position.
setInt64(int position, int value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 64-bit signed integer at the specified position without changing the current write position.
setInt8(int position, int value) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes an 8-bit signed value at the specified position without changing the current write position.
setUint16(int position, int value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 16-bit unsigned integer at the specified position without changing the current write position.
setUint32(int position, int value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 32-bit unsigned integer at the specified position without changing the current write position.
setUint64(int position, int value, [Endian endian = Endian.big]) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes a 64-bit unsigned integer at the specified position without changing the current write position.
setUint8(int position, int value) → void

Available on BinaryWriter, provided by the BinaryWriterRandomAccess extension

Writes an 8-bit unsigned value at the specified position without changing the current write position.
shiftBytes(int start, int end, int target) → void

Available on BinaryWriter, provided by the BinaryWriterPosition extension

Shifts a block of written bytes within the buffer.
skip(int count) → void

Available on BinaryWriter, provided by the BinaryWriterPosition extension

Advances the write position by count bytes without writing data.
takeBytes({bool copy = false}) Uint8List

Available on BinaryWriter, provided by the BinaryWriterCore extension

Extracts all written bytes and resets the writer.
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).
toBytes() Uint8List

Available on BinaryWriter, provided by the BinaryWriterCore extension

Returns a view of the written bytes (from index 0 up to the current bytesWritten) without resetting the writer.
writeBool(bool value) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a boolean value as a single byte.
writeBytes(List<int> bytes, [int offset = 0, int? length]) → void

Available on BinaryWriter, provided by the BinaryWriterBytesString extension

Writes a sequence of bytes from the given list.
writeFloat32(double value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 32-bit floating-point number (IEEE 754 single precision).
writeFloat64(double value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 64-bit floating-point number (IEEE 754 double precision).
writeInt16(int value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 16-bit signed integer (-32768 to 32767).
writeInt32(int value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 32-bit signed integer (-2,147,483,648 to 2,147,483,647).
writeInt64(int value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 64-bit signed integer.
writeInt8(int value) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes an 8-bit signed integer (-128 to 127).
writeString(String value, {bool allowMalformed = true}) → void

Available on BinaryWriter, provided by the BinaryWriterBytesString extension

Writes a UTF-8 encoded string.
writeStringFixed(String value, {LengthEncoding lengthEncoding = .u8, bool allowMalformed = true}) → void

Available on BinaryWriter, provided by the BinaryWriterBytesString extension

Writes a UTF-8 encoded string prefixed with a fixed-width length.
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);.
writeUint16(int value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 16-bit unsigned integer (0-65535).
writeUint24(int value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 24-bit unsigned integer (0 to 16,777,215).
writeUint32(int value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 32-bit unsigned integer (0 to 4,294,967,295).
writeUint64(int value, [Endian endian = .big]) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes a 64-bit unsigned integer.
writeUint8(int value) → void

Available on BinaryWriter, provided by the BinaryWriterNumeric extension

Writes an 8-bit unsigned integer (0-255).
writeVarBytes(List<int> bytes) → void

Available on BinaryWriter, provided by the BinaryWriterBytesString extension

Writes a length-prefixed byte array.
writeVarInt(int value) → void

Available on BinaryWriter, provided by the BinaryWriterVarInt extension

Writes a signed variable-length integer using ZigZag encoding.
writeVarString(String value, {bool allowMalformed = true}) → void

Available on BinaryWriter, provided by the BinaryWriterBytesString extension

Writes a length-prefixed UTF-8 encoded string.
writeVarUint(int value) → void

Available on BinaryWriter, provided by the BinaryWriterVarInt extension

Writes an unsigned variable-length integer using VarInt encoding.
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.

Operators

operator [](int index) int

Available on BinaryWriter, provided by the BinaryWriterCore extension

Returns the byte at the specified index without changing the current write position.
operator []=(int index, int value) → void

Available on BinaryWriter, provided by the BinaryWriterCore extension

Writes a byte at the specified index without changing the current write position.