std.fs.file.ReadOnlyFile
type pub inline ReadOnlyFile
A file that can only be used for reads.
Static methods
new
Show source codeHide source code
fn pub static new(path: ref Path) -> Result[ReadOnlyFile, Error] {
match
sys.open_file(
path.to_string,
read: true,
write: false,
append: false,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadOnlyFile(fd))
case Error(e) -> Result.Error(e)
}
}
fn pub static new(path: ref Path) -> Result[ReadOnlyFile, Error]
Returns a new ReadOnlyFile
.
Errors
This method returns a std.io.Error
if opening of the file fails.
Examples
import std.fs.file (ReadOnlyFile)
ReadOnlyFile.new('/dev/null'.to_path)
Instance methods
metadata
Show source codeHide source code
fn pub metadata -> Result[Metadata, Error] {
sys.file_metadata(@fd)
}
fn pub metadata -> Result[Metadata, Error]
Returns a metadata about the current file, such as its size and creation time.
Errors
This method returns an Error
if the underlying system call fails, such as
when the file no longer exists.
Examples
import std.fs.file (ReadOnlyFile)
ReadOnlyFile
.new('/test.txt')
.or_panic('failed to open the file')
.metadata
.or_panic('failed to get the metadata')
.size # => 1234
read
Show source codeHide source code
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] {
sys.read_file(@fd, into, size)
}
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 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 {
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 codeHide 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 codeHide source code
fn pub mut seek(position: Int) -> Result[Int, Error] {
sys.seek_to(@fd, position)
}
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.
Implemented traits
Drop
impl Drop for ReadOnlyFile
Read
impl Read for ReadOnlyFile
Seek
impl Seek for ReadOnlyFile