Pre-defined types
BufReader
BufIO.BufReader
— TypeBufReader{T <: IO} <: AbstractBufReader
BufReader(io::IO, [buffer_size::Int])::BufReader
Wrap an IO
in a struct with a new buffer, giving it the AbstractBufReader
interface.
The BufReader
has an infinitely growable buffer, and will only grow the buffer if fill_buffer
is called while its internal buffer is full.
Throw an ArgumentError
if buffer_size
is less than 1.
julia> rdr = BufReader(IOBuffer("Hello, world!\nabc\r\ndef"));
julia> get_buffer(rdr)
0-element MemoryViews.ImmutableMemoryView{UInt8}
julia> peek(rdr)
0x48
julia> readline(rdr)
"Hello, world!"
julia> String(readavailable(rdr))
"abc\r\ndef"
BufWriter
BufIO.BufWriter
— TypeBufWriter{T <: IO} <: AbstractBufWriter
BufWriter(io::IO, [buffer_size::Int])::BufWriter
Wrap an IO
in a struct with a new buffer, giving it the AbstractBufWriter
interface.
The BufWriter
has an infinitely growable buffer, and will only expand the buffer if grow_buffer
is called on it while it does not contain any data (as shown by get_unflushed
).
Throw an ArgumentError
if buffer_size
is < 1.
julia> io = IOBuffer(); wtr = BufWriter(io);
julia> print(wtr, "Hello!")
julia> write(wtr, [0x1234, 0x5678])
4
julia> read(io) # wtr not flushed
UInt8[]
julia> flush(wtr); seekstart(io); String(read(io))
"Hello!4\x12xV"
julia> get_unflushed(wtr)
0-element MemoryViews.MutableMemoryView{UInt8}
IOReader
BufIO.IOReader
— TypeIOReader{T <: AbstractBufReader} <: IO
Wrapper type to convert an AbstractBufReader
to an IO
.
IOReader
s implement the same part of the IO
interface as AbstractBufReader
, so this type is only used to satisfy type constraints.
Examples
julia> io = CursorReader("hello");
julia> f(x::IO) = String(read(x));
julia> f(io)
ERROR: MethodError: no method matching f(::CursorReader)
[...]
julia> f(IOReader(io))
"hello"
CursorReader
BufIO.CursorReader
— TypeCursorReader(x) <: AbstractBufReader
A seekable, stateful reader of the content of any object x
which implements MemoryView(x)::MemoryView{UInt8}
.
Closing it does nothing.
julia> rdr = CursorReader("some\ncontent\nhere");
julia> readline(rdr)
"some"
julia> read(rdr, String)
"content\nhere"
julia> seek(rdr, 8);
julia> read(rdr, String)
"tent\nhere"
VecWriter
BufIO.VecWriter
— TypeVecWriter([vec::Vector{UInt8}]) <: AbstractBufWriter
Create an AbstractBufWriter
backed by a Vector{UInt8}
. Read the (public) property .vec
to get the vector back.
This type is useful as an efficient string builder through String(io.vec)
or takestring!(io)
(the latter in Julia 1.13+).
Functions flush
and close
do not affect the writer.
Mutating io
will mutate vec
and vice versa. Neither vec
nor io
will be invalidated by mutating the other, but doing so may affect the implicit (non-semantic) behaviour (e.g. memory reallocations or efficiency) of the other. For example, repeated and interleaved push!(vec)
and write(io, x)
may be less efficient, if one operation has memory allocation patterns that is suboptimal for the other operation.
julia> vw = VecWriter();
julia> write(vw, "Hello, world!", 0xe1fa)
15
julia> append!(vw.vec, b"More data");
julia> String(vw.vec)
"Hello, world!\xfa\xe1More data"