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 bytes from a stream into a ByteArray.

The return value is the number of bytes read.

The size argument specifies how many bytes are to be read. The actual number of bytes read may be less than this value.

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 {
    let bytes_read = try read(into: bytes, size: read_size)

    if bytes_read == 0 { return Result.Ok(total) }

    total += bytes_read

    # To reduce the overhead of large buffer reads, we increase the buffer
    # size as more data is read.
    if read_size < MAX_READ_ALL_SIZE { read_size *= 2 }
  }
}
fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, Error]

Reads all bytes from the stream into the ByteArray.

If an error is encountered while reading, this method stops reading any more bytes and re-throws the error.

The return value is the number of bytes read.

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