file_streams/read_stream

Use Erlang binary file read streams in Gleam.

Types

A read stream location defined relative to the beginning of the file, the end of the file, or the current position in the read stream.

pub type FileStreamLocation {
  BeginningOfFile(offset: Int)
  CurrentLocation(offset: Int)
  EndOfFile(offset: Int)
}

Constructors

  • BeginningOfFile(offset: Int)

    A location relative to the beginning of the file, i.e. an absolute offset in the read stream. The offset should not be negative.

  • CurrentLocation(offset: Int)

    A location relative to the current position in the file. The offset can be either positive or negative.

  • EndOfFile(offset: Int)

    A location relative to the end of the file. The offset should not be positive.

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 position(
  stream: ReadStream,
  location: FileStreamLocation,
) -> Result(Int, FileError)

Sets the position of a read stream to the given location, where the location can be relative to the beginning of the file, the end of the file, or the current position in the file, On success, returns the current position in the read stream as an absolute offset in bytes.

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_remaining_bytes(
  stream: ReadStream,
) -> Result(BitArray, ReadStreamError)

Reads all remaining bytes from a read stream.

If no more data is available in the read stream then this function will return an empty bit array. It never returns an EndOfStream error.

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