Struct acc_reader::AccReader [] [src]

pub struct AccReader<R: Read> {
    // some 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>

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>

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>

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

fn by_ref(&mut self) -> &mut Self

fn bytes(self) -> Bytes<Self>

fn chars(self) -> Chars<Self>

fn chain<R>(self, next: R) -> Chain<Self, R> where R: Read

fn take(self, limit: u64) -> Take<Self>

fn tee<W>(self, out: W) -> Tee<Self, W> where W: Write

impl<R: Read> BufRead for AccReader<R>

fn fill_buf(&mut self) -> Result<&[u8]>

fn consume(&mut self, amt: usize)

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>

fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>

fn split(self, byte: u8) -> Split<Self>

fn lines(self) -> Lines<Self>

impl<R: Read> Seek for AccReader<R>

fn seek(&mut self, pos: SeekFrom) -> Result<u64>