Search results

There are no results.

std.fs.file.ReadWriteFile

class pub ReadWriteFile

A file that can be used for both reads and writes.

Fields

path

let pub @path: Path

The path of the file.

Static methods

append

Show source code
Hide source code
fn pub static append(path: Path) -> Result[ReadWriteFile, Error] {
  match inko_file_open(_INKO.process, path.to_string, FILE_READ_APPEND) {
    case { @tag = 0, @value = v } -> {
      Result.Ok(ReadWriteFile(path: path, fd: v))
    }
    case { @tag = _, @value = e } -> {
      Result.Error(Error.from_os_error(e as Int))
    }
  }
}
fn pub static append(path: Path) -> Result[ReadWriteFile, Error]

Opens a file for both reading and appending:

Examples

import std.fs.file (ReadWriteFile)

let handle = ReadWriteFile.append('/dev/null'.to_path).get

new

Show source code
Hide source code
fn pub static new(path: Path) -> Result[ReadWriteFile, Error] {
  match inko_file_open(_INKO.process, path.to_string, FILE_READ_WRITE) {
    case { @tag = 0, @value = v } -> {
      Result.Ok(ReadWriteFile(path: path, fd: v))
    }
    case { @tag = _, @value = e } -> {
      Result.Error(Error.from_os_error(e as Int))
    }
  }
}
fn pub static new(path: Path) -> Result[ReadWriteFile, Error]

Opens a file for both reading and writing:

Examples

import std.fs.file (ReadWriteFile)

let handle = ReadWriteFile.new('/dev/null'.to_path).get

Instance methods

flush

Show source code
Hide source code
fn pub mut flush -> Result[Nil, Error] {
  match inko_file_flush(_INKO.process, @fd) {
    case { @tag = 1, @value = _ } -> Result.Ok(nil)
    case { @tag = _, @value = e } -> {
      Result.Error(Error.from_os_error(e as Int))
    }
  }
}
fn pub mut flush -> Result[Nil, Error]

Flushes any pending writes.

print

Show source code
Hide source code
fn pub mut print(string: String) -> Result[Nil, Error] {
  write_string(string).then(fn (_) { write_string('\n') })
}
fn pub mut print(string: String) -> Result[Nil, Error]

Writes the entirety of string to the underlying stream, followed by writing a Unix newline to the stream.

read

Show source code
Hide source code
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] {
  match inko_file_read(_INKO.process, @fd, into, size) {
    case { @tag = 0, @value = v } -> Result.Ok(v)
    case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e))
  }
}
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).

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.

seek

Show source code
Hide source code
fn pub mut seek(position: Int) -> Result[Int, Error] {
  match inko_file_seek(_INKO.process, @fd, position) {
    case { @tag = 0, @value = v } -> Result.Ok(v)
    case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e))
  }
}
fn pub mut seek(position: Int) -> Result[Int, Error]

Seeks to the given byte offset, returning the new offset.

If position is negative, seeking is performed in reverse order relative to the end.

size

Show source code
Hide source code
fn pub size -> Result[Int, Error] {
  match inko_file_size(_INKO.process, @path.to_string) {
    case { @tag = 0, @value = v } -> Result.Ok(v)
    case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e))
  }
}
fn pub size -> Result[Int, Error]

write_bytes

Show source code
Hide source code
fn pub mut write_bytes(bytes: ref ByteArray) -> Result[Nil, Error] {
  write_all_internal(bytes.to_pointer, bytes.size)
}
fn pub mut write_bytes(bytes: ref ByteArray) -> Result[Nil, Error]

Writes the entirety of bytes to the underlying stream.

Types implementing this method must guarantee that upon returning from this method, either all of the data is written and a Ok(Nil) is returned, or an Error(Error) is returned.

write_string

Show source code
Hide source code
fn pub mut write_string(string: String) -> Result[Nil, Error] {
  write_all_internal(string.to_pointer, string.size)
}
fn pub mut write_string(string: String) -> Result[Nil, Error]

Writes the entirety of string to the underlying stream.

See Write.write_bytes for more details.

Implemented traits

std.drop.

Drop

impl Drop for ReadWriteFile
std.io.

Read

impl Read for ReadWriteFile
std.io.

Seek

impl Seek for ReadWriteFile
std.io.

Size

impl Size for ReadWriteFile
std.io.

Write

impl Write for ReadWriteFile