On this page

zena:byte-buffer

zena
import {…} from 'zena:byte-buffer';

ByteBuffer - A growable buffer for efficiently constructing binary data.

Uses a chunked growth strategy (like StringBuilder) to avoid copying on every resize. Ideal for building binary formats like WASM modules.

Classes

ByteBuffer

zena
final class ByteBuffer

A growable buffer for efficiently constructing binary data.

Constructors
zena
new(capacity: i32 = 256)
#

Creates a new ByteBuffer with the specified initial chunk capacity.

capacity

Initial capacity for the first chunk (default: 256).

Properties
zena
length: i32 { get; }
#

Total number of bytes written to the buffer.

zena
capacity: i32 { get; }
#

Total allocated capacity across all chunks.

Methods
zena
writeByte(b: i32): ByteBuffer
#

Write a single byte.

zena
writeBytes(src: ByteArray): ByteBuffer
#

Write all bytes from a ByteArray.

zena
writeBytesSlice(src: ByteArray, offset: i32, len: i32): ByteBuffer
#

Write a slice of bytes from a ByteArray.

src

Source byte array

offset

Starting offset in source

len

Number of bytes to write

zena
writeBufferRange(other: ByteBuffer, start: i32, end: i32): ByteBuffer
#

Write the byte range [start, end) of another ByteBuffer's contents.

zena
writeBuffer(other: ByteBuffer): ByteBuffer
#

Write another ByteBuffer's contents.

zena
writeU16(value: i32): ByteBuffer
#

Write a 16-bit unsigned integer (little-endian).

zena
writeU32(value: i32): ByteBuffer
#

Write a 32-bit unsigned integer (little-endian).

zena
writeU64(value: i64): ByteBuffer
#

Write a 64-bit integer (little-endian).

zena
writeF32(value: f32): ByteBuffer
#

Write a 32-bit float (little-endian).

zena
writeF64(value: f64): ByteBuffer
#

Write a 64-bit float (little-endian).

zena
writeULEB128(value: i32): ByteBuffer
#

Write an unsigned LEB128 encoded integer.

zena
writeSLEB128(value: i32): ByteBuffer
#

Write a signed LEB128 encoded integer.

zena
writeULEB128_64(value: i64): ByteBuffer
#

Write a 64-bit unsigned LEB128.

zena
writeSLEB128_64(value: i64): ByteBuffer
#

Write a 64-bit signed LEB128.

zena
getByte(index: i32): i32
#

Read a byte at an absolute position.

zena
setByte(index: i32, value: i32): void
#

Write a byte at an absolute position (for patching).

zena
patchU32(index: i32, value: i32): void
#

Patch a U32 at an absolute position (little-endian).

zena
toByteArray(): ByteArray
#

Convert to a single contiguous ByteArray.

zena
clear(): void
#

Clear the buffer, reusing the first chunk.