file_streams/file_stream

Use Erlang file streams in Gleam.

Types

A file stream that data can be read from and/or written to depending on the mode specified when it was opened.

pub opaque type FileStream

A file stream location defined relative to the beginning of the file, the end of the file, or the current position in the file stream. This is used with the position() function.

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 file stream. The offset should not be negative.

  • CurrentLocation(offset: Int)

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

  • EndOfFile(offset: Int)

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

Functions

pub fn close(stream: FileStream) -> Result(Nil, FileStreamError)

Closes a file stream that was opened with open().

pub fn open(
  filename: String,
  mode: List(FileOpenMode),
) -> Result(FileStream, FileStreamError)

Opens a new file stream that can read and/or write data from the specified file. See FileOpenMode for all of the available file modes.

For simple cases of opening a file stream use one of the open_read(), open_read_text(), open_write(), or open_write_text() helper functions.

Once the file stream is no longer needed it should be closed with close().

pub fn open_read(
  filename: String,
) -> Result(FileStream, FileStreamError)

Opens a new file stream for reading binary data from the specified file.

pub fn open_read_text(
  filename: String,
) -> Result(FileStream, FileStreamError)

Opens a new file stream for reading UTF-8 text from a file.

pub fn open_write(
  filename: String,
) -> Result(FileStream, FileStreamError)

Opens a new file stream for writing binary data to the specified file.

pub fn open_write_text(
  filename: String,
) -> Result(FileStream, FileStreamError)

Opens a new file stream for writing UTF-8 text to a file.

pub fn position(
  stream: FileStream,
  location: FileStreamLocation,
) -> Result(Int, FileStreamError)

Sets the position of a file 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 file stream as an absolute offset in bytes.

pub fn read_bytes(
  stream: FileStream,
  byte_count: Int,
) -> Result(BitArray, FileStreamError)

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

If the end of the file stream is encountered before any bytes can be read then Error(Eof) is returned.

pub fn read_bytes_exact(
  stream: FileStream,
  byte_count: Int,
) -> Result(BitArray, FileStreamError)

Reads the requested number of bytes from a binary file stream. If the requested number of bytes can’t be read prior to reaching the end of the file stream then Error(Eof) is returned.

pub fn read_chars(
  stream: FileStream,
  count: Int,
) -> Result(String, FileStreamError)

Reads the next count characters from a text file stream. The returned number of characters may be fewer than the number that was requested if the end of the stream is reached.

pub fn read_float32_be(
  stream: FileStream,
) -> Result(Float, FileStreamError)

Reads a big-endian 32-bit float from a binary file stream.

pub fn read_float32_le(
  stream: FileStream,
) -> Result(Float, FileStreamError)

Reads a little-endian 32-bit float from a binary file stream.

pub fn read_float64_be(
  stream: FileStream,
) -> Result(Float, FileStreamError)

Reads a big-endian 64-bit float from a binary file stream.

pub fn read_float64_le(
  stream: FileStream,
) -> Result(Float, FileStreamError)

Reads a little-endian 64-bit float from a binary file stream.

pub fn read_int16_be(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_int16_le(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_int32_be(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_int32_le(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_int64_be(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_int64_le(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_int8(
  stream: FileStream,
) -> Result(Int, FileStreamError)

Reads an 8-bit signed integer from a binary file stream.

pub fn read_line(
  stream: FileStream,
) -> Result(String, FileStreamError)

Reads the next line of text from a text file stream. The returned string will include the newline \n character. If the stream contains a Windows newline \r\n then only the \n will be returned.

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

Reads the specified type the requested number of times from a binary file stream, e.g. two little-endian 32-bit integers, or four big-endian 64-bit floats, 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: FileStream,
) -> Result(BitArray, FileStreamError)

Reads all remaining bytes from a binary file stream.

If no more data is available in the file stream then this function will return an empty bit array. It never returns Error(Eof).

pub fn read_uint16_be(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_uint16_le(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_uint32_be(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_uint32_le(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_uint64_be(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_uint64_le(
  stream: FileStream,
) -> Result(Int, FileStreamError)

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

pub fn read_uint8(
  stream: FileStream,
) -> Result(Int, FileStreamError)

Reads an 8-bit unsigned integer from a binary file stream.

pub fn sync(stream: FileStream) -> Result(Nil, FileStreamError)

Syncs a file stream that was opened for writing. This ensures that any write buffers kept by the operating system (not by the Erlang runtime system) are written to disk.

When a file stream is opened with delayed writes enabled to improve performance, syncing can return an error related to flushing recently written data to the underlying device.

pub fn write_bytes(
  stream: FileStream,
  bytes: BitArray,
) -> Result(Nil, FileStreamError)

Writes bytes to a binary file stream.

pub fn write_chars(
  stream: FileStream,
  chars: String,
) -> Result(Nil, FileStreamError)

Writes characters to a text file stream. This will convert the characters to the text encoding in use for the file stream.

Search Document