---
sidebar:
  label: roboto.storage.sparse_buffer
  order: 7
title: roboto.storage.sparse_buffer
---
## Module Contents

### SparseBuffer

```python
class roboto.storage.sparse_buffer.SparseBuffer(file_size: int)
```

`from roboto.storage import SparseBuffer`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L10-L197)

A read-only, seekable file-like object over a file of which only some regions are held in memory.

[`HttpRangeReader`](/docs/reference/python-sdk/roboto/storage/http_range_reader#roboto.storage.http_range_reader.HttpRangeReader) caches the byte ranges it fetches from a remote file through it, so most code never builds one; reading those bytes again needs no new request. Each run of consecutive bytes held is a region. Regions that overlap or touch merge into one, and a read returns only bytes that are in a region.

**Parameters**

- **file_size** (`int`): Size of the whole file, in bytes. Reads stop at it, and `seek` with `whence=2` counts from it.

**Properties**

- **SparseBuffer.regions** (`list[tuple[int, int]]`): Regions held, as `(start, end)` byte offsets with `end` exclusive, sorted by `start`.
- **SparseBuffer.size** (`int`): Size of the whole file, in bytes.

#### SparseBuffer.add_region()

```python
def add_region(offset: int, data: bytes) -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L38-L106)

Store bytes at an offset in the file, merging them with every region they overlap or touch.

Where the new bytes overlap bytes already held, the new bytes replace them.

**Parameters**

- **offset** (`int`): Offset in the file of the first byte of `data`.
- **data** (`bytes`): Bytes to store. Empty `data` stores nothing.

**Returns**

- `None`

#### SparseBuffer.clear()

```python
def clear() -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L108-L110)

Remove every region.

**Returns**

- `None`

#### SparseBuffer.find_region()

```python
def find_region(start: int, size: int) -> bytes | None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L112-L129)

Return `size` bytes starting at offset `start` if a region holds all of them.

Unlike `read`, does not use or move the read position.

**Parameters**

- **start** (`int`): Offset in the file of the first byte.
- **size** (`int`): Number of bytes.

**Returns**

- `bytes | None`: The bytes, or `None` if any of them is outside every region.

#### SparseBuffer.read()

```python
def read(size: int = -1) -> bytes
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L131-L159)

Read up to `size` bytes from the read position, and advance the position past them.

Reading stops at the end of the region that holds the read position, or at the end of the file, so the result can be shorter than `size`. When no region holds the read position, or it is at or past the end of the file, this returns `b""` and leaves the position unchanged. A caller fetches more bytes when the result is shorter than it needs.

**Parameters**

- **size** (`int`): Maximum number of bytes to read. A negative value reads to the end of the file.

**Returns**

- `bytes`

#### SparseBuffer.readable()

```python
def readable() -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L161-L163)

Return `True`: the buffer can be read.

**Returns**

- `bool`

#### SparseBuffer.seek()

```python
def seek(offset: int, whence: int = 0) -> int
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L165-L185)

Move the read position and return it.

The position is not checked against the bounds of the file. A `whence` other than `0`, `1` or `2` leaves it unchanged.

**Parameters**

- **offset** (`int`): Number of bytes to move from the point `whence` names.
- **whence** (`int`): Point to count from: `0` (the default) for the start of the file, `1` for the read position, `2` for the end of the file.

**Returns**

- `int`: The new read position, as an offset from the start of the file.

#### SparseBuffer.seekable()

```python
def seekable() -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L187-L189)

Return `True`: the buffer supports `seek`.

**Returns**

- `bool`

#### SparseBuffer.tell()

```python
def tell() -> int
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L191-L193)

Return the current read position.

**Returns**

- `int`

#### SparseBuffer.writable()

```python
def writable() -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/sparse_buffer.py#L195-L197)

Return `False`: the buffer is read-only.

**Returns**

- `bool`
