Struct acc_reader::AccReader
[−]
[src]
pub struct AccReader<R: Read> { /* fields omitted */ }
An accumulating reader which provides Seek
for any Read
.
An accumulating reader wraps an instance of std::io::Read
trait and provides
implementations of std::io::Read
, std::io::BufRead
and std::io::Seek
which use
the wrapped Read
as a source.
This struct keeps an internal buffer which contains everything read so far from the
wrapped stream and allows "revisiting" the previously read data through the Seek
interface.
When the user needs to seek beyond what was read from the stream, the accumulating reader
will automatically read the necessary number of bytes from the wrapped stream to fulfill
the request, if possible.
Seeking to beyond the end of the underlying stream is not possible and will result in
an error. Seeking using SeekFrom::End
, naturally, involves buffering the whole underlying
stream, therefore it will either hang with blocking infinite streams like sockets or will fill
up all of the available memory with truly infinite streams.
This struct will buffer all of the underlying stream in order to provide seeking, therefore you should discard it as soon as you don't need it if you are working with large streams of data.
AccReader
is parameterized by two values, initial capacity and increment. Initial capacity
defines the initial size of the internal buffer. This buffer automatically grows with each
successful read operation, if necessary, by the number of bytes read. If BufRead
interface
is used, however, increment value is used to expand the internal buffer capacity when it is
filled.
Examples
use std::io::{self, Read, Seek, SeekFrom}; use acc_reader::AccReader; let mut ar = AccReader::new(io::stdin()); // read everything starting from the 12th byte // will panic if the input contains less than 12 bytes ar.seek(SeekFrom::Start(12)).unwrap(); let mut input = Vec::new(); ar.read_to_end(&mut input).unwrap();
It is also possible to seek starting from the end of stream, but this requires reading the whole stream to the end:
use std::io::{self, Read, Seek, SeekFrom}; use acc_reader::AccReader; let mut ar = AccReader::new(io::stdin()); // read last 12 bytes // will panic if the input contains less than 12 bytes ar.seek(SeekFrom::End(-12)).unwrap(); let mut input = Vec::new(); ar.read_to_end(&mut input).unwrap();
Methods
impl<R: Read> AccReader<R>
[src]
fn new(source: R) -> AccReader<R>
Creates a new accumulating reader from the provided Read
instance.
Default values for the initial buffer capacity and increment are used.
Examples
use std::io; use acc_reader::AccReader; let input = io::stdin(); let mut ar = AccReader::new(input);
fn with_initial_capacity(cap: usize, source: R) -> AccReader<R>
Creates a new accumulating reader from the provided Read
instance with the specified
initial capacity for the internal buffer.
The default value for the buffer increment is used.
Examples
use std::io; use acc_reader::AccReader; let input = io::stdin(); let mut ar = AccReader::with_initial_capacity(512, input);
fn with_increment(inc: usize, source: R) -> AccReader<R>
Creates a new accumulating reader from the provided Read
instance with the specified
increment for the internal buffer.
The default value for the initial capacity is used.
Examples
use std::io; use acc_reader::AccReader; let input = io::stdin(); let mut ar = AccReader::with_increment(128, input);
fn with_initial_capacity_and_increment(
cap: usize,
inc: usize,
source: R
) -> AccReader<R>
cap: usize,
inc: usize,
source: R
) -> AccReader<R>
Creates a new accumulating reader from the provided Read
instance with the specified
increment and initial capacity for the internal buffer.
Initial capacity determines the initial size of the internal buffer. The increment
is only needed if BufRead
interface is used, and it defined the buffer expansion
size when fill_buf()
is called and no more space in the buffer is available.
Examples
use std::io; use acc_reader::AccReader; let input = io::stdin(); let mut ar = AccReader::with_initial_capacity_and_increment(512, 128, input);
fn into_inner(self) -> R
Unwraps this accumulating reader, returning the underlying BufRead
instance.
Note that any accumulated data will be lost.
Examples
use std::io; use acc_reader::AccReader; let input = io::stdin(); let mut ar = AccReader::new(input); let input2 = ar.into_inner();
Trait Implementations
impl<R: Read> Read for AccReader<R>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
1.0.0
Transforms this Read
instance to an Iterator
over its bytes. Read more
fn chars(self) -> Chars<Self>
🔬 This is a nightly-only experimental API. (io
)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an Iterator
over char
s. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
1.0.0
Creates an adaptor which will read at most limit
bytes from it. Read more
impl<R: Read> BufRead for AccReader<R>
[src]
fn fill_buf(&mut self) -> Result<&[u8]>
Fills the internal buffer of this object, returning the buffer contents. Read more
fn consume(&mut self, amt: usize)
Tells this buffer that amt
bytes have been consumed from the buffer, so they should no longer be returned in calls to read
. Read more
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0
Read all bytes into buf
until the delimiter byte
or EOF is reached. Read more
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0
Read all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer. Read more
fn split(self, byte: u8) -> Split<Self>
1.0.0
Returns an iterator over the contents of this reader split on the byte byte
. Read more
fn lines(self) -> Lines<Self>
1.0.0
Returns an iterator over the lines of this reader. Read more