use result::{Ok, Err};
use slice::{OwnedVector, ImmutableVector, MutableVector};
use slice;
+use vec::Vec;
/// Wraps a Reader and buffers input from it
///
-/// It can be excessively inefficient to work directly with a `Reader` or
-/// `Writer`. Every call to `read` or `write` on `TcpStream` results in a
-/// system call, for example. This module provides structures that wrap
-/// `Readers`, `Writers`, and `Streams` and buffer input and output to them.
+/// It can be excessively inefficient to work directly with a `Reader`. For
+/// example, every call to `read` on `TcpStream` results in a system call. A
+/// `BufferedReader` performs large, infrequent reads on the underlying
+/// `Reader` and maintains an in-memory buffer of the results.
///
/// # Example
///
/// ```
pub struct BufferedReader<R> {
priv inner: R,
- priv buf: ~[u8],
+ priv buf: Vec<u8>,
priv pos: uint,
priv cap: uint,
- priv eof: bool,
}
impl<R: Reader> BufferedReader<R> {
// everything up-front. This allows creation of BufferedReader instances
// to be very cheap (large mallocs are not nearly as expensive as large
// callocs).
- let mut buf = slice::with_capacity(cap);
+ let mut buf = Vec::with_capacity(cap);
unsafe { buf.set_len(cap); }
BufferedReader {
inner: inner,
buf: buf,
pos: 0,
cap: 0,
- eof: false,
}
}
/// underlying reader because that could possibly corrupt the buffer.
pub fn get_ref<'a>(&'a self) -> &'a R { &self.inner }
- /// Unwraps this buffer, returning the underlying reader.
+ /// Unwraps this `BufferedReader`, returning the underlying reader.
///
/// Note that any leftover data in the internal buffer is lost.
pub fn unwrap(self) -> R { self.inner }
impl<R: Reader> Buffer for BufferedReader<R> {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
if self.pos == self.cap {
- self.cap = try!(self.inner.read(self.buf));
+ self.cap = try!(self.inner.read(self.buf.as_mut_slice()));
self.pos = 0;
}
Ok(self.buf.slice(self.pos, self.cap))
/// Wraps a Writer and buffers output to it
///
+/// It can be excessively inefficient to work directly with a `Writer`. For
+/// example, every call to `write` on `TcpStream` results in a system call. A
+/// `BufferedWriter` keeps an in memory buffer of data and writes it to the
+/// underlying `Writer` in large, infrequent batches.
+///
/// This writer will be flushed when it is dropped.
///
/// # Example
/// ```
pub struct BufferedWriter<W> {
priv inner: Option<W>,
- priv buf: ~[u8],
+ priv buf: Vec<u8>,
priv pos: uint
}
/// Creates a new `BufferedWriter` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
// See comments in BufferedReader for why this uses unsafe code.
- let mut buf = slice::with_capacity(cap);
+ let mut buf = Vec::with_capacity(cap);
unsafe { buf.set_len(cap); }
BufferedWriter {
inner: Some(inner),
/// underlying reader because that could possibly corrupt the buffer.
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
- /// Unwraps this buffer, returning the underlying writer.
+ /// Unwraps this `BufferedWriter`, returning the underlying writer.
///
/// The buffer is flushed before returning the writer.
pub fn unwrap(mut self) -> W {
/// underlying reader because that could possibly corrupt the buffer.
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
- /// Unwraps this buffer, returning the underlying writer.
+ /// Unwraps this `LineBufferedWriter`, returning the underlying writer.
///
/// The internal buffer is flushed before returning the writer.
pub fn unwrap(self) -> W { self.inner.unwrap() }
/// 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
w.get_ref()
}
- /// Unwraps this buffer, returning the underlying stream.
+ /// 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.