Search results

There are no results.

std.io.Read

Read

A type data can be read from (e.g. a File).

Required methods

read

Show source code
Hide source code
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error]
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error]

Reads up to size bytes from self into the given ByteArray, returning the number of bytes read.

The into argument is the ByteArray to read the bytes into. The capacity of this ByteArray is increased automatically if necessary.

The size argument specifies how many bytes are to be read.

The return value is the number of bytes read.

The number of bytes read may be less than size. This can happen for different reasons, such as when all input is consumed or not enough data is available (yet).

Default methods

read_all

Show source code
Hide source code
fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, Error] {
  let mut total = 0
  let mut read_size = INITIAL_READ_ALL_SIZE

  loop {
    match read(into: bytes, size: read_size) {
      case Ok(0) -> return Result.Ok(total)
      case Ok(n) -> {
        total += n

        # To reduce the number of calls to `Reader.read` when there's lots of
        # input to consume, we increase the read size if deemed beneficial.
        if read_size < MAX_READ_ALL_SIZE and n == read_size { read_size *= 2 }
      }
      case Error(e) -> throw e
    }
  }
}
fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, Error]

Reads from self into the given ByteArray, returning when all input is consumed.

The return value is the number of bytes read.

Errors

This method returns an Error if the underlying call to Read.read returns an Error.

read_exact

Show source code
Hide source code
fn pub mut read_exact(into: mut ByteArray, size: Int) -> Result[Nil, Error] {
  let mut pending = size

  while pending > 0 {
    match read(into, pending) {
      case Ok(0) if pending > 0 -> throw Error.EndOfInput
      case Ok(n) -> pending -= n
      case Error(e) -> throw e
    }
  }

  Result.Ok(nil)
}
fn pub mut read_exact(into: mut ByteArray, size: Int) -> Result[Nil, Error]

Reads exactly size bytes into into.

Whereas Read.read might return early if fewer bytes are available in the input stream, Read.read_exact continues reading until the desired amount of bytes is read.

Errors

If the end of the input stream is encountered before filling the buffer, an Error.EndOfInput error is returned.

If an error is returned, no assumption can be made about the state of the into buffer, i.e. there's no guarantee data read so far is in the buffer in the event of an error.

Implementations

std.fs.file.

ReadOnlyFile

impl Read for ReadOnlyFile
std.fs.file.

ReadWriteFile

impl Read for ReadWriteFile
std.io.

Buffer

impl Read for Buffer
std.io.

BufferedReader

impl Read for BufferedReader
std.net.socket.

Socket

impl Read for Socket
std.net.socket.

TcpClient

impl Read for TcpClient
std.net.socket.

UdpSocket

impl Read for UdpSocket
std.net.socket.

UnixClient

impl Read for UnixClient
std.net.socket.

UnixDatagram

impl Read for UnixDatagram
std.net.socket.

UnixSocket

impl Read for UnixSocket
std.net.tls.

Client

impl Read for Client
std.net.tls.

Server

impl Read for Server
std.stdio.

Stdin

impl Read for Stdin
std.sys.

Stderr

impl Read for Stderr
std.sys.

Stdout

impl Read for Stdout