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]

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);

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);

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);

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);

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]

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Read all bytes until EOF in this source, placing them into buf. Read more

Read all bytes until EOF in this source, placing them into buf. Read more

Read the exact number of bytes required to fill buf. Read more

Creates a "by reference" adaptor for this instance of Read. Read more

Transforms this Read instance to an Iterator over its bytes. Read more

🔬 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 chars. Read more

Creates an adaptor which will chain this stream with another. Read more

Creates an adaptor which will read at most limit bytes from it. Read more

impl<R: Read> BufRead for AccReader<R>
[src]

Fills the internal buffer of this object, returning the buffer contents. Read more

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

Read all bytes into buf until the delimiter byte or EOF is reached. Read more

Read all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer. Read more

Returns an iterator over the contents of this reader split on the byte byte. Read more

Returns an iterator over the lines of this reader. Read more

impl<R: Read> Seek for AccReader<R>
[src]

Seek to an offset, in bytes, in a stream. Read more