# ByteBuffer

`import { ByteBuffer } from "destack:bytes";`

Growable byte buffer.

```ds title="ByteBuffer"
export class ByteBuffer {
    storage: Unique<[MaybeUninit<uint8>]>;
    initializedLength: isize;
    allocatedCapacity: usize;
    static new(): ^ByteBuffer;
    static withCapacity(capacity: usize): ^ByteBuffer;
    static from(bytes: &'a readonly [uint8]): ^ByteBuffer;
    get length(): isize;
    get size(): isize;
    get capacity(): usize;
    get isEmpty(): boolean;
    reserve(additional: usize): void;
    clear(): void;
    compact(): void;
    writeByte(value: uint8): void;
    write(bytes: &'b readonly [uint8]): void;
    fill(value: uint8, count: isize): void;
    asBytes<const A: Access = "readonly">(): WithAccess<&[uint8], A>;
    toBytes(): readonly [uint8];
    intoBytes(): readonly [uint8];
}
```

[language/library/src/bytes/buffer.ds:7:98](https://github.com/destack-sh/destack/blob/main/language/library/src/bytes/buffer.ds#L7-L98)

## Members

### `storage: Unique<[MaybeUninit<uint8>]>`

The owned byte storage.

### `initializedLength: isize`

The initialized byte count.

### `allocatedCapacity: usize`

The allocated byte capacity.

### `static new(): ^ByteBuffer`

Create an empty byte buffer.

### `static withCapacity(capacity: usize): ^ByteBuffer`

Create a byte buffer with room for at least `capacity` bytes.

### `static from(bytes: &'a readonly [uint8]): ^ByteBuffer`

Create a byte buffer containing `bytes`.

### `get length(): isize`

Return the initialized byte count.

### `get size(): isize`

Return the initialized byte count.

### `get capacity(): usize`

Return the allocated byte capacity.

### `get isEmpty(): boolean`

Return whether this buffer has no initialized bytes.

### `reserve(additional: usize): void`

Reserve room for at least `additional` more bytes.

### `clear(): void`

Remove all initialized bytes.

### `compact(): void`

Release unused capacity.

### `writeByte(value: uint8): void`

Append one byte.

### `write(bytes: &'b readonly [uint8]): void`

Append bytes.

### `fill(value: uint8, count: isize): void`

Append `count` copies of `value`.

### `asBytes<const A: Access = "readonly">(): WithAccess<&[uint8], A>`

Borrow initialized bytes.

### `toBytes(): readonly [uint8]`

Clone initialized bytes into immutable storage.

### `intoBytes(): readonly [uint8]`

Move initialized bytes into immutable storage.
