9.1 KiB
module io
Contents
- Constants
- cp
- make_readerwriter
- new_buffered_reader
- new_buffered_writer
- new_multi_writer
- read_all
- read_any
- RandomReader
- RandomWriter
- Reader
- ReaderWriter
- Writer
- ReaderWriterImpl
- BufferedReadLineConfig
- BufferedReader
- BufferedReaderConfig
- BufferedWriter
- BufferedWriterConfig
- CopySettings
- Eof
- MultiWriter
- NotExpected
- ReadAllConfig
Constants
const read_all_len = 10 * 1024
const read_all_grow_len = 1024
cp
fn cp(mut src Reader, mut dst Writer, params CopySettings) !
cp copies from src to dst by allocating a maximum of 1024 bytes buffer for reading until either EOF is reached on src or an error occurs. An error is returned if an error is encountered during write.
make_readerwriter
fn make_readerwriter(r Reader, w Writer) ReaderWriterImpl
make_readerwriter takes a rstream and a wstream and makes an rwstream with them.
new_buffered_reader
fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader
new_buffered_reader creates a new BufferedReader.
new_buffered_writer
fn new_buffered_writer(o BufferedWriterConfig) !&BufferedWriter
new_buffered_writer creates a new BufferedWriter with the specified BufferedWriterConfig. Returns an error when cap is 0 or negative.
new_multi_writer
fn new_multi_writer(writers ...Writer) Writer
new_multi_writer returns a Writer that writes to all writers. The write function of the returned Writer writes to all writers of the MultiWriter, returns the length of bytes written, and if any writer fails to write the full length an error is returned and writing to other writers stops, and if any writer returns an error the error is returned immediately and writing to other writers stops.
read_all
fn read_all(config ReadAllConfig) ![]u8
read_all reads all bytes from a reader until either a 0 length read or if read_to_end_of_stream is true then the end of the stream (none).
read_any
fn read_any(mut r Reader) ![]u8
read_any reads any available bytes from a reader (until the reader returns a read of 0 length).
RandomReader
interface RandomReader {
read_from(pos u64, mut buf []u8) !int
}
RandomReader represents a stream of readable data from at a random location.
RandomWriter
interface RandomWriter {
write_to(pos u64, buf []u8) !int
}
RandomWriter is the interface that wraps the write_to method, which writes buf.len bytes to the underlying data stream at a random pos.
Reader
interface Reader {
// read reads up to buf.len bytes and places
// them into buf.
// A type that implements this should return
// `io.Eof` on end of stream (EOF) instead of just returning 0
mut:
read(mut buf []u8) !int
}
Reader represents a stream of data that can be read.
ReaderWriter
interface ReaderWriter {
Reader
Writer
}
ReaderWriter represents a stream that can be read and written.
Writer
interface Writer {
mut:
write(buf []u8) !int
}
Writer is the interface that wraps the write method, which writes buf.len bytes to the underlying data stream.
ReaderWriterImpl
read
fn (mut r ReaderWriterImpl) read(mut buf []u8) !int
read reads up to buf.len bytes into buf. It returns the number of bytes read or any error encountered.
write
fn (mut r ReaderWriterImpl) write(buf []u8) !int
write writes buf.len bytes from buf to the underlying data stream. It returns the number of bytes written or any error encountered.
BufferedReadLineConfig
struct BufferedReadLineConfig {
pub:
delim u8 = `\n` // line delimiter
}
BufferedReadLineConfig are options that can be given to the read_line() function.
BufferedReader
struct BufferedReader {
mut:
reader Reader
buf []u8
offset int // current offset in the buffer
len int
fails int // how many times fill_buffer has read 0 bytes in a row
mfails int // maximum fails, after which we can assume that the stream has ended
pub mut:
end_of_stream bool // whether we reached the end of the upstream reader
total_read int // total number of bytes read
}
BufferedReader provides a buffered interface for a reader.
read
fn (mut r BufferedReader) read(mut buf []u8) !int
read fufills the Reader interface.
free
fn (mut r BufferedReader) free()
free deallocates the memory for a buffered reader's internal buffer.
end_of_stream
fn (r BufferedReader) end_of_stream() bool
end_of_stream returns whether the end of the stream was reached.
read_line
fn (mut r BufferedReader) read_line(config BufferedReadLineConfig) !string
read_line attempts to read a line from the buffered reader. It will read until it finds the specified line delimiter such as (\n, the default or \0) or the end of stream.
BufferedReaderConfig
struct BufferedReaderConfig {
pub:
reader Reader
cap int = 128 * 1024 // large for fast reading of big(ish) files
retries int = 2 // how many times to retry before assuming the stream ended
}
BufferedReaderConfig are options that can be given to a buffered reader.
BufferedWriter
struct BufferedWriter {
mut:
n int
wr Writer
pub mut:
buf []u8
}
reset
fn (mut b BufferedWriter) reset()
reset resets the buffer to its initial state.
buffered
fn (b BufferedWriter) buffered() int
buffered returns the number of bytes currently stored in the buffer.
flush
fn (mut b BufferedWriter) flush() !
flush writes the buffered data to the underlying writer and clears the buffer, ensures all data is written. Returns an error if the writer fails to write all buffered data.
available
fn (b BufferedWriter) available() int
available returns the amount of available space left in the buffer.
write
fn (mut b BufferedWriter) write(src []u8) !int
write writes src in the buffer, flushing it to the underlying writer as needed, and returns the number of bytes written.
BufferedWriterConfig
struct BufferedWriterConfig {
pub:
writer Writer
cap int = 128 * 1024
}
CopySettings
struct CopySettings {
pub mut:
buffer_size int = 64 * 1024 // The buffer size used during the copying. A larger buffer is more performant, but uses more RAM.
}
CopySettings provides additional options to io.cp
Eof
struct Eof {
Error
}
Eof error means that we reach the end of the stream.
MultiWriter
struct MultiWriter {
pub mut:
writers []Writer
}
MultiWriter writes to all its writers.
write
fn (mut m MultiWriter) write(buf []u8) !int
write writes to all writers of the MultiWriter. Returns the length of bytes written. If any writer fails to write the full length an error is returned and writing to other writers stops. If any writer returns an error the error is returned immediately and writing to other writers stops.
NotExpected
struct NotExpected {
cause string
code int
}
NotExpected is a generic error that means that we receave a not expected error.
ReadAllConfig
struct ReadAllConfig {
pub:
read_to_end_of_stream bool
reader Reader
}
ReadAllConfig allows options to be passed for the behaviour of read_all.