file_streams/read_stream

Use Erlang file read streams from Gleam.

Types

A stream that binary data can be read from.

pub type ReadStream

Errors that can occur when using a read stream.

pub type ReadStreamError {
  EndOfStream
  OtherFileError(error: FileError)
}

Constructors

  • EndOfStream

    The end of the stream was reached before all of the requested data could be read.

  • OtherFileError(error: FileError)

    A file error occurred while reading from the stream.

Functions

pub fn close(stream: ReadStream) -> Nil

Closes a read stream.

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

Opens a new read stream that reads binary data from a file. Once the stream has been used it should be closed with read_stream.close().

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

Reads bytes from a read stream.

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