CRAM.Source (cram v0.1.0)

Copy Markdown View Source

Bounded reader over a local file or an in-memory binary.

Reads that ask for more than max_bytes fail with reason: :allocation_limit before any bytes are copied into a new binary of that size. Reads past the end fail with reason: :truncated.

File sources hold an open file descriptor. Call close/1 when finished. The struct does not print file bytes.

Summary

Types

t()

An open byte source.

Functions

Close a file source and return the closed struct.

Open a local file or an in-memory binary.

Byte length of the source.

Types

t()

@type t() :: %CRAM.Source{
  closed: boolean(),
  data: binary() | nil,
  fd: :file.fd() | nil,
  kind: :file | :binary,
  path: Path.t() | nil,
  size: non_neg_integer()
}

An open byte source.

  • :kind — :file or :binary

  • :path — filesystem path for a file, otherwise nil

  • :data — the bytes of a binary source, otherwise nil

  • :fd — file descriptor, or nil after close and for a binary

  • :size — byte length

  • :closed — true after close/1

    iex> {:ok, source} = CRAM.Source.open({:binary, "CRAM"}) iex> source.kind :binary

Functions

close(source)

@spec close(t()) :: {:ok, t()}

Close a file source and return the closed struct.

Returns {:ok, source}. Closing a binary source marks it closed and does not free a descriptor. Closing again returns the same shape and does not raise. Later read/4 calls on the returned struct fail with reason: :closed.

iex> {:ok, source} = CRAM.Source.open({:binary, "CRAM"})
iex> {:ok, closed} = CRAM.Source.close(source)
iex> closed.closed
true

open(path)

@spec open(Path.t() | {:file, Path.t()} | {:binary, iodata()}) ::
  {:ok, t()} | {:error, CRAM.Error.t()}

Open a local file or an in-memory binary.

source is a path, {:file, path}, or {:binary, iodata}. Returns {:ok, source}. A missing file is {:error, %CRAM.Error{reason: :io}}. This function does not raise.

iex> {:ok, source} = CRAM.Source.open({:binary, "CRAM"})
iex> CRAM.Source.size(source)
4

read(source, offset, length, max_bytes \\ 16_777_216)

@spec read(t(), non_neg_integer(), non_neg_integer(), non_neg_integer()) ::
  {:ok, binary()} | {:error, CRAM.Error.t()}

Read length bytes at offset.

max_bytes defaults to 16 MiB and is optional. All three numbers must be non-negative integers. Returns {:ok, binary} or {:error, %CRAM.Error{}}.

  • reason: :allocation_limit — length is above max_bytes. Nothing of that size is allocated
  • reason: :truncated — the span passes the end of the source
  • reason: :invalid_range — an argument is negative or not an integer
  • reason: :closed — close/1 was already called
  • reason: :io — the file read failed
iex> {:ok, source} = CRAM.Source.open({:binary, "CRAM"})
iex> CRAM.Source.read(source, 0, 4)
{:ok, "CRAM"}
iex> {:error, error} = CRAM.Source.read(source, 0, 3, 2)
iex> error.reason
:allocation_limit
iex> {:ok, closed} = CRAM.Source.close(source)
iex> {:error, closed_error} = CRAM.Source.read(closed, 0, 1)
iex> closed_error.reason
:closed

size(source)

@spec size(t()) :: non_neg_integer()

Byte length of the source.

This does not read the bytes and does not change when the source is closed.

iex> {:ok, source} = CRAM.Source.open({:binary, "CRAM"})
iex> CRAM.Source.size(source)
4