zena:byte-buffer
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
final class ByteBuffer
A growable buffer for efficiently constructing binary data.
new(capacity: i32 = 256)
Creates a new ByteBuffer with the specified initial chunk capacity.
writeByte(b: i32): ByteBuffer
Write a single byte.
writeBytes(src: ByteArray): ByteBuffer
Write all bytes from a ByteArray.
writeBytesSlice(src: ByteArray, offset: i32, len: i32): ByteBuffer
Write a slice of bytes from a ByteArray.
writeBufferRange(other: ByteBuffer, start: i32, end: i32): ByteBuffer
Write the byte range [start, end) of another ByteBuffer's contents.
writeBuffer(other: ByteBuffer): ByteBuffer
Write another ByteBuffer's contents.
writeU16(value: i32): ByteBuffer
Write a 16-bit unsigned integer (little-endian).
writeU32(value: i32): ByteBuffer
Write a 32-bit unsigned integer (little-endian).
writeU64(value: i64): ByteBuffer
Write a 64-bit integer (little-endian).
writeF32(value: f32): ByteBuffer
Write a 32-bit float (little-endian).
writeF64(value: f64): ByteBuffer
Write a 64-bit float (little-endian).
writeULEB128(value: i32): ByteBuffer
Write an unsigned LEB128 encoded integer.
writeSLEB128(value: i32): ByteBuffer
Write a signed LEB128 encoded integer.
writeULEB128_64(value: i64): ByteBuffer
Write a 64-bit unsigned LEB128.
writeSLEB128_64(value: i64): ByteBuffer
Write a 64-bit signed LEB128.
getByte(index: i32): i32
Read a byte at an absolute position.
setByte(index: i32, value: i32): void
Write a byte at an absolute position (for patching).
patchU32(index: i32, value: i32): void
Patch a U32 at an absolute position (little-endian).
toByteArray(): ByteArray
Convert to a single contiguous ByteArray.
clear(): void
Clear the buffer, reusing the first chunk.