ByteBuffer API Reference

Growable byte buffer for binary serialization and deserialization

Import

U std/bytebuffer

Struct

S ByteBuffer { data: i64, len: i64, cap: i64, pos: i64 }

Methods

MethodSignatureDescription
with_capacityF with_capacity(capacity: i64) -> ByteBufferCreate with capacity
lenF len(&self) -> i64Bytes written
capacityF capacity(&self) -> i64Buffer capacity
positionF position(&self) -> i64Current read position
data_ptrF data_ptr(&self) -> i64Get raw data pointer
remainingF remaining(&self) -> i64Bytes left to read
seekF seek(&self, pos: i64) -> i64Set read position
rewindF rewind(&self) -> i64Reset read position to 0
ensure_capacityF ensure_capacity(&self, needed: i64) -> i64Ensure capacity
write_u8F write_u8(&self, value: i64) -> i64Write one byte
read_u8F read_u8(&self) -> i64Read one byte
write_i32_leF write_i32_le(&self, value: i64) -> i64Write 4-byte integer (little-endian)
read_i32_leF read_i32_le(&self) -> i64Read 4-byte integer (little-endian)
write_i64_leF write_i64_le(&self, value: i64) -> i64Write 8-byte integer (little-endian)
read_i64_leF read_i64_le(&self) -> i64Read 8-byte integer (little-endian)
write_bytesF write_bytes(&self, src: i64, count: i64) -> i64Write byte range
read_bytesF read_bytes(&self, dst: i64, count: i64) -> i64Read bytes into destination
write_strF write_str(&self, s: str) -> i64Write length-prefixed string
clearF clear(&self) -> i64Clear buffer
dropF drop(&self) -> i64Free buffer

Usage

U std/bytebuffer

F main() -> i64 {
    buf := ByteBuffer.with_capacity(256)

    # Write data
    buf.write_i64_le(42)
    buf.write_u8(255)
    buf.write_i32_le(1000)

    # Rewind and read
    buf.rewind()
    val := buf.read_i64_le()  # 42
    byte := buf.read_u8()     # 255
    num := buf.read_i32_le()  # 1000

    buf.drop()
    0
}