# `BAM.Source`
[🔗](https://github.com/baoulo/bam-elixir/blob/v0.1.0/lib/bam/source.ex#L1)

Random-access byte source.

`pread/3` reads at most `length` bytes starting at `offset`. A read that
starts at or past the end returns `{:ok, ""}`. A read that extends past
the end returns the bytes that remain. Implementations return a tagged
error instead of raising.

`BAM.Source.File` and `BAM.Source.Memory` are the built-in sources. A
custom source implements the same three callbacks. Offsets are absolute,
so a caller can seek to a BGZF virtual offset without consuming the bytes
in between.

# `state`

```elixir
@type state() :: term()
```

Opaque handle owned by one source implementation.

# `close`

```elixir
@callback close(state()) :: {:ok, state()} | {:error, BAM.Error.t()}
```

Releases the source. Closing a closed source returns `{:ok, state}`.

# `pread`

```elixir
@callback pread(state(), non_neg_integer(), non_neg_integer()) ::
  {:ok, binary()} | {:error, BAM.Error.t()}
```

Reads at most `length` bytes starting at `offset`.

# `size`

```elixir
@callback size(state()) :: {:ok, non_neg_integer()} | {:error, BAM.Error.t()}
```

Byte length of the source.

# `pread_exact`

```elixir
@spec pread_exact(module(), state(), non_neg_integer(), non_neg_integer(), map()) ::
  {:ok, binary()} | {:error, BAM.Error.t()}
```

Reads exactly `length` bytes, or a truncated error.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
