io_stream

Types

The type that represents io streams.

capability to read, write and seek and binary/text format is marked using phantom types.

pub opaque type Stream(read_cap, write_cap, enc_cap, seek_cap)

Values

pub fn close(
  stream: Stream(read_cap, write_cap, enc_cap, seek_cap),
) -> Result(Nil, error.StreamError)

Closes given stream.

pub fn next_byte(
  stream: Stream(mode.Read, write_cap, mode.Binary, seek_cap),
) -> Result(Int, error.StreamError)

Get the next byte in the stream.

The given stream must have binary format and read capability.

pub fn next_char(
  stream: Stream(mode.Read, write_cap, mode.Text, seek_cap),
) -> Result(String, error.StreamError)

Get the next unicode character in the stream. This uses UTF-8 encoding.

The given stream must have textual format and read capability.

pub fn open_append(
  path: String,
  exclusive: Bool,
) -> Result(
  Stream(mode.NoRead, mode.Write, mode.Text, mode.NoSeek),
  error.SystemError,
)

Open file in textual append mode

exclusive: whether to fail if the file already exists

pub fn open_append_bin(
  path: String,
  exclusive: Bool,
) -> Result(
  Stream(mode.NoRead, mode.Write, mode.Binary, mode.NoSeek),
  error.SystemError,
)

Open file in binary append mode

exclusive: whether to fail if the file already exists

pub fn open_read(
  path: String,
) -> Result(
  Stream(mode.Read, mode.NoWrite, mode.Text, mode.Seek),
  error.SystemError,
)

Open file in textual read mode

pub fn open_read_bin(
  path: String,
) -> Result(
  Stream(mode.Read, mode.NoWrite, mode.Binary, mode.Seek),
  error.SystemError,
)

Open file in binary read mode

pub fn open_rw(
  path: String,
  truncate: Bool,
) -> Result(
  Stream(mode.Read, mode.Write, mode.Text, mode.Seek),
  error.SystemError,
)

Open file in textual read/write mode

truncate: whether to empty the file while opening it

Note: In NodeJS a file is created if it doesn’t exist on “w+” mode(with truncate). This doesn’t happen in Erlang.

pub fn open_rw_bin(
  path: String,
  truncate: Bool,
) -> Result(
  Stream(mode.Read, mode.Write, mode.Binary, mode.Seek),
  error.SystemError,
)

Open file in binary read/write mode

truncate: whether to empty the file while opening it

Note: In NodeJS a file is created if it doesn’t exist on “w+” mode(with truncate). This doesn’t happen in Erlang.

pub fn open_write(
  path: String,
  exclusive: Bool,
) -> Result(
  Stream(mode.NoRead, mode.Write, mode.Text, mode.Seek),
  error.SystemError,
)

Open file in textual write mode

exclusive: whether to fail if the file already exists

pub fn open_write_bin(
  path: String,
  exclusive: Bool,
) -> Result(
  Stream(mode.NoRead, mode.Write, mode.Binary, mode.Seek),
  error.SystemError,
)

Open file in binary write mode

exclusive: whether to fail if the file already exists

pub fn read_all(
  stream: Stream(mode.Read, write_cap, mode.Text, seek_cap),
) -> Result(String, error.StreamError)

Returns all the remaining text in the stream. This uses UTF-8 encoding.

The given stream must have textual format and read capability.

pub fn read_all_bytes(
  stream: Stream(mode.Read, write_cap, mode.Binary, seek_cap),
) -> Result(BitArray, error.StreamError)

Returns all the remaining bytes in the stream. This uses UTF-8 encoding.

The given stream must have binary format and read capability.

pub fn read_bytes(
  stream: Stream(mode.Read, write_cap, mode.Binary, seek_cap),
  count: Int,
) -> Result(BitArray, error.StreamError)

Get the next bytes in the stream. The given stream must have binary format.

The given stream must have binary format and read capability.

count: number of bytes to reads

pub fn read_line(
  stream: Stream(mode.Read, write_cap, mode.Text, seek_cap),
) -> Result(String, error.StreamError)

Get the next unicode characters until a break line appears in the stream. This uses UTF-8 encoding.

The given stream must have textual format and read capability.

Note: The line break character is also included in the returned string.

Note: If the stream ends before a break line is found, this function returns characters up to end of file.

pub fn seek(
  stream: Stream(mode.Read, mode.Write, enc_cap, mode.Seek),
  position: Int,
) -> Result(Nil, error.StreamError)

Seeks given stream to a specific position.

The given stream must have both read and write capability.

Note: Seek behavior for files opened in append mode is harder to get right in both Erlang ans Js and thus isn’t implemented.

pub fn stderr() -> Stream(
  mode.NoRead,
  mode.Write,
  mode.Text,
  mode.NoSeek,
)

Returns error output stream in text format.

pub fn stderr_bin() -> Stream(
  mode.NoRead,
  mode.Write,
  mode.Binary,
  mode.NoSeek,
)

Returns error output stream in binary format.

pub fn stdin() -> Stream(
  mode.Read,
  mode.NoWrite,
  mode.Text,
  mode.Seek,
)

Returns standard input stream in text format.

pub fn stdin_bin() -> Stream(
  mode.Read,
  mode.NoWrite,
  mode.Binary,
  mode.Seek,
)

Returns standard input stream in binary format.

pub fn stdout() -> Stream(
  mode.NoRead,
  mode.Write,
  mode.Text,
  mode.NoSeek,
)

Returns standard output stream in text format.

pub fn stdout_bin() -> Stream(
  mode.NoRead,
  mode.Write,
  mode.Binary,
  mode.NoSeek,
)

Returns standard output stream in binary format.

pub fn sync(
  stream: Stream(read_cap, mode.Write, enc_cap, seek_cap),
) -> Result(Nil, error.StreamError)

Synchronies write calls for given.

Note: stdout/stderr actually can’t be synced.

pub fn write_bytes(
  stream: Stream(read_cap, mode.Write, mode.Binary, seek_cap),
  bytes: BitArray,
) -> Result(Nil, error.StreamError)

Writes given bytes into the stream.

The given stream must have binary format and write capability.

pub fn write_line(
  stream: Stream(read_cap, mode.Write, mode.Text, seek_cap),
  string: String,
) -> Result(Nil, error.StreamError)

Writes given string into the stream and then adds a line break.

The given stream must have textual format and write capability.

pub fn write_string(
  stream: Stream(read_cap, mode.Write, mode.Text, seek_cap),
  string: String,
) -> Result(Nil, error.StreamError)

Writes given string into the stream.

The given stream must have textual format and write capability.

Search Document