Struct std::io::BufferedStream[src]
pub struct BufferedStream<S> {
// some fields omitted
}Wraps a Stream and buffers input and output to and from it.
It can be excessively inefficient to work directly with a Stream. For
example, every call to read or write on TcpStream results in a system
call. A BufferedStream keeps in memory buffers of data, making large,
infrequent calls to read and write on the underlying Stream.
The output half will be flushed when this stream is dropped.
Example
fn main() { #![allow(unused_must_use)] use std::io::{BufferedStream, File}; let file = File::open(&Path::new("message.txt")); let mut stream = BufferedStream::new(file); stream.write("hello, world".as_bytes()); stream.flush(); let mut buf = [0, ..100]; match stream.read(buf) { Ok(nread) => println!("Read {} bytes", nread), Err(e) => println!("error reading: {}", e) } }use std::io::{BufferedStream, File}; let file = File::open(&Path::new("message.txt")); let mut stream = BufferedStream::new(file); stream.write("hello, world".as_bytes()); stream.flush(); let mut buf = [0, ..100]; match stream.read(buf) { Ok(nread) => println!("Read {} bytes", nread), Err(e) => println!("error reading: {}", e) }
Methods
impl<S: Stream> BufferedStream<S>
fn with_capacities(reader_cap: uint, writer_cap: uint, inner: S) -> BufferedStream<S>
Creates a new buffered stream with explicitly listed capacities for the reader/writer buffer.
fn new(inner: S) -> BufferedStream<S>
Creates a new buffered stream with the default reader/writer buffer capacities.
fn get_ref<'a>(&'a self) -> &'a S
Gets a reference to the underlying stream.
This type does not expose the ability to get a mutable reference to the underlying reader because that could possibly corrupt the buffer.
fn unwrap(self) -> S
Unwraps this BufferedStream, returning the underlying stream.
The internal buffer is flushed before returning the stream. Any leftover data in the read buffer is lost.