Overview
Raw byte buffers with numeric lanes in either byte order.
ByteBuffer is a fixed-size block of bytes with an address, a length,
and endian-aware accessors for the usual integer and float widths. Use
it for file contents, network frames, C ABI calls, and hand-written
wire formats, where the byte order and the lane width both matter.
The size is picked once by allocate and never changes: there is no
resize, so length() and capacity() always report the same number.
Out-of-range access is neither clamped nor reported as an error value.
A read or write that reaches past the end, or starts at a negative
offset, stops the program with byte buffer out of bounds. A negative
capacity stops it with negative byte buffer capacity.
Offsets and sizes are plain Int. Narrow integer lanes widen into a
64-bit Int on the way out: signed lanes sign-extend, unsigned lanes
zero-extend. Float lanes widen the same way, and the 32-bit float
accessors narrow a Float to 32 bits on the way in, which leaves
about 7 decimal digits.
The LE and BE suffixes pick the byte order. Little-endian puts the
least significant byte at the lowest offset, which is what x86, ARM,
and most desktop formats use. Big-endian puts the most significant
byte at the lowest offset, which is network byte order and what most
file formats use. Write a lane with the same order you read it with;
mixing the two on one field is a common source of silent corruption.
Build a buffer with fromString or fromArray for text and byte
lists, or with allocate plus the write* methods for anything
else. slice copies a range out into a new buffer, and copyWithin
shifts a range inside the buffer that already exists. address hands
the raw pointer to Pointer.fromAddress for unsafe work and C calls.
When to use it
Reach for @std/bytes when a program talks to the outside world in binary: wire protocols, file headers, or any format where byte order matters. Text stays in String; everything else goes through a ByteBuffer.
Capabilities
- Fixed-size raw buffers
- Explicit-endian int/float lanes
- Zero-copy slicing
Symbols
- class ByteBuffer — Fixed-size raw byte buffer. All offsets and sizes are plain
Int; - fn utf8Len()