std.fs.file.ReadOnlyFile
class pub ReadOnlyFile
A file that can only be used for reads.
Fields
path
let pub @path: Path
The path of the file.
Static methods
new
Show source codeHide source code
fn pub static new(path: Path) -> Result[ReadOnlyFile, Error] {
match inko_file_open(_INKO.process, path.to_string, FILE_READ_ONLY) {
case { @tag = 0, @value = v } -> {
Result.Ok(ReadOnlyFile(path: path, fd: v))
}
case { @tag = _, @value = e } -> {
Result.Error(Error.from_os_error(e as Int))
}
}
}
fn pub static new(path: Path) -> Result[ReadOnlyFile, Error]
Returns a new ReadOnlyFile
.
Examples
Opening a file in read-only mode:
import std.fs.file (ReadOnlyFile)
let handle = ReadOnlyFile.new('/dev/null'.to_path).get
Instance methods
read
Show source codeHide 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 codeHide 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 codeHide 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 codeHide 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]
Implemented traits
Drop
impl Drop for ReadOnlyFile
Read
impl Read for ReadOnlyFile
Seek
impl Seek for ReadOnlyFile
Size
impl Size for ReadOnlyFile