file_streams/read_stream

Use Erlang binary file read streams in Gleam.

Types

A stream that data can be read from.

pub type ReadStream

Functions

pub fn close(stream: ReadStream) -> Result(Nil, ReadStreamError)

Closes a read stream.

pub fn open(filename: String) -> Result(ReadStream, FileError)

Opens a new read stream that reads binary or text data from a file. Once the stream is no longer needed it should be closed with close().

pub fn read_bytes(
  stream: ReadStream,
  byte_count: Int,
) -> Result(BitArray, ReadStreamError)

Reads bytes from a read stream. The returned number of bytes may be fewer than the number that was requested if the end of the stream was reached.

If the end of the stream is encountered before any bytes can be read then EndOfStream is returned.

pub fn read_bytes_exact(
  stream: ReadStream,
  byte_count: Int,
) -> Result(BitArray, ReadStreamError)

Reads the requested number of bytes from a read stream. If the requested number of bytes can’t be read prior to reaching the end of the stream then EndOfStream is returned.

pub fn read_float32_be(
  stream: ReadStream,
) -> Result(Float, ReadStreamError)

Reads a big-endian 32-bit float from a read stream.

pub fn read_float32_le(
  stream: ReadStream,
) -> Result(Float, ReadStreamError)

Reads a little-endian 32-bit float from a read stream.

pub fn read_float64_be(
  stream: ReadStream,
) -> Result(Float, ReadStreamError)

Reads a big-endian 64-bit float from a read stream.

pub fn read_float64_le(
  stream: ReadStream,
) -> Result(Float, ReadStreamError)

Reads a little-endian 64-bit float from a read stream.

pub fn read_int16_be(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a big-endian 16-bit signed integer from a read stream.

pub fn read_int16_le(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a little-endian 16-bit signed integer from a read stream.

pub fn read_int32_be(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a big-endian 32-bit signed integer from a read stream.

pub fn read_int32_le(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a little-endian 32-bit signed integer from a read stream.

pub fn read_int64_be(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a big-endian 64-bit signed integer from a read stream.

pub fn read_int64_le(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a little-endian 64-bit signed integer from a read stream.

pub fn read_int8(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads an 8-bit signed integer from a read stream.

pub fn read_list(
  stream: ReadStream,
  item_read_fn: fn(ReadStream) -> Result(a, ReadStreamError),
  item_count: Int,
) -> Result(List(a), ReadStreamError)

Reads the specified type the requested number of times, e.g. two little-endian 32-bit integers, or four big-endian 64-bit floating point values, and returns the values in a list.

Examples

read_list(stream, read_stream.read_int32_le, 2)
|> Ok([1, 2])

read_list(stream, read_stream.read_float64_be, 4)
|> Ok([1.0, 2.0])
pub fn read_uint16_be(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a big-endian 16-bit unsigned integer from a read stream.

pub fn read_uint16_le(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a little-endian 16-bit unsigned integer from a read stream.

pub fn read_uint32_be(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a big-endian 32-bit unsigned integer from a read stream.

pub fn read_uint32_le(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a little-endian 32-bit unsigned integer from a read stream.

pub fn read_uint64_be(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a big-endian 64-bit unsigned integer from a read stream.

pub fn read_uint64_le(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads a little-endian 64-bit unsigned integer from a read stream.

pub fn read_uint8(
  stream: ReadStream,
) -> Result(Int, ReadStreamError)

Reads an 8-bit unsigned integer from a read stream.

Search Document