Search results

There are no results.

std.io.BufferedRead

BufferedRead

A Read type using an internal buffer, allowing more efficient reading and additional operations.

Required methods

peek

Show source code
Hide source code
fn pub mut peek -> Result[Option[Int], Error]
fn pub mut peek -> Result[Option[Int], Error]

Reads the current byte from the buffer, without consuming it from the underlying buffer.

If a byte is read, Ok(Some(n)) is returned where n is the byte. A Ok(None) indicates the end of the input.

Examples

import std.io (Buffer, BufferedReader)

let buffer = Buffer.new('hello')
let reader = BufferedReader.new(buffer)

reader.peek # => Result.Ok(Option.Some(104))
reader.peek # => Result.Ok(Option.Some(104))

read_byte

Show source code
Hide source code
fn pub mut read_byte -> Result[Option[Int], Error]
fn pub mut read_byte -> Result[Option[Int], Error]

Read and return a single byte.

If a byte is read, Ok(Some(n)) is returned where n is the byte. A Ok(None) indicates the end of the input.

Examples

import std.io (Buffer, BufferedReader)

let buffer = Buffer.new('hello')
let reader = BufferedReader.new(buffer)

reader.read_byte # => Result.Ok(Option.Some(104))
reader.read_byte # => Result.Ok(Option.Some(101))

Default methods

bytes

Show source code
Hide source code
fn pub mut bytes -> Stream[Result[Int, Error]] {
  Stream.new(fn move {
    match read_byte {
      case Ok(Some(num)) -> Option.Some(Result.Ok(num))
      case Ok(None) -> Option.None
      case Error(err) -> Option.Some(Result.Error(err))
    }
  })
}
fn pub mut bytes -> Stream[Result[Int, Error]]

Returns an iterator that yields the bytes in self.

Each byte is wrapped in a Result, as reading may fail.

Examples

import std.fs.file (ReadOnlyFile)
import std.io (BufferedReader)

let file = ReadOnlyFile.new('README.md').get
let reader = BufferedReader.new(file)

reader.bytes.next # => Option.Some(Result.Ok(35))

read_line

Show source code
Hide source code
fn pub mut read_line(
  into: mut ByteArray,
  inclusive: Bool,
) -> Result[Int, Error] {
  read_until(byte: 0xA, into: into, inclusive: inclusive)
}
fn pub mut read_line(into: mut ByteArray, inclusive: Bool) -> Result[Int, Error]

Read bytes into into up to and including the newline byte (0xA aka "\n").

The inclusive argument specifies if the newline should also be read into the ByteArray, or if it should be discarded.

Upon success, the return value is Ok(n) where n is the number of bytes read from the input stream. If inclusive is set to false, n still accounts for the newline. That is, if the input is ab\n, then the returned size is 3 bytes.

Examples

Reading until and including the end of a line:

import std.io (Buffer, BufferedReader)

let reader = BufferedReader.new(Buffer.new('hello\nworld'))
let bytes = ByteArray.new

reader.read_line(into: bytes, inclusive: true) # => Result.Ok(6)
bytes.to_string # => 'hello\n'

Excluding the newline from the buffer:

import std.io (Buffer, BufferedReader)

let reader = BufferedReader.new(Buffer.new('hello\nworld'))
let bytes = ByteArray.new

reader.read_line(into: bytes, inclusive: false) # => Result.Ok(6)
bytes.to_string # => 'hello'

read_until

Show source code
Hide source code
fn pub mut read_until(
  byte: Int,
  into: mut ByteArray,
  inclusive: Bool,
) -> Result[Int, Error] {
  let mut total = 0

  loop {
    match try read_byte {
      case Some(val) if byte == val -> {
        if inclusive { into.push(val) }

        total += 1
        break
      }
      case Some(val) -> {
        total += 1
        into.push(val)
      }
      case _ -> break
    }
  }

  Result.Ok(total)
}
fn pub mut read_until(byte: Int, into: mut ByteArray, inclusive: Bool) -> Result[Int, Error]

Read bytes into into up to and including the byte specified in the byte argument.

The inclusive argument specifies if the byte value should also be read into the ByteArray, or if it should be discarded.

Upon success, the return value is Ok(n) where n is the number of bytes read from the input stream. If inclusive is set to false, n still accounts for the terminal byte. That is, if the input is abc and the terminal byte is c, then the returned size is 3 bytes.

Examples

Reading until and including a given byte:

import std.io (Buffer, BufferedReader)

let reader = BufferedReader.new(Buffer.new('hello\nworld'))
let bytes = ByteArray.new

reader.read_until(byte: 0xA, into: bytes, inclusive: true) # => Result.Ok(6)
bytes.to_string # => 'hello\n'

Excluding the byte from the buffer:

import std.io (Buffer, BufferedReader)

let reader = BufferedReader.new(Buffer.new('hello\nworld'))
let bytes = ByteArray.new

reader.read_until(byte: 0xA, into: bytes, inclusive: false) # => Result.Ok(6)
bytes.to_string # => 'hello'

Implementations

std.io.

Buffer

impl BufferedRead for Buffer
std.io.

BufferedReader

impl BufferedRead for BufferedReader