2013-09-10 00:38:43 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Buffering wrappers for I/O traits
|
|
|
|
|
2014-02-06 01:34:33 -06:00
|
|
|
use cmp;
|
2014-01-15 15:25:09 -06:00
|
|
|
use container::Container;
|
2014-01-29 18:33:57 -06:00
|
|
|
use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
|
2014-01-15 15:25:09 -06:00
|
|
|
use iter::ExactSize;
|
2014-02-28 14:46:46 -06:00
|
|
|
use ops::Drop;
|
|
|
|
use option::{Some, None, Option};
|
2014-01-29 18:33:57 -06:00
|
|
|
use result::{Ok, Err};
|
2014-03-08 17:11:52 -06:00
|
|
|
use slice::{OwnedVector, ImmutableVector, MutableVector};
|
|
|
|
use slice;
|
2014-03-22 19:18:27 -05:00
|
|
|
use vec::Vec;
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
/// Wraps a Reader and buffers input from it
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-03-22 19:18:27 -05:00
|
|
|
/// 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.
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::{BufferedReader, File};
|
|
|
|
///
|
|
|
|
/// let file = File::open(&Path::new("message.txt"));
|
|
|
|
/// let mut reader = BufferedReader::new(file);
|
|
|
|
///
|
|
|
|
/// let mut buf = [0, ..100];
|
|
|
|
/// match reader.read(buf) {
|
2014-01-30 18:55:20 -06:00
|
|
|
/// Ok(nread) => println!("Read {} bytes", nread),
|
|
|
|
/// Err(e) => println!("error reading: {}", e)
|
2014-01-15 15:25:09 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2013-09-10 00:38:43 -05:00
|
|
|
pub struct BufferedReader<R> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: R,
|
|
|
|
buf: Vec<u8>,
|
|
|
|
pos: uint,
|
|
|
|
cap: uint,
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Reader> BufferedReader<R> {
|
2014-03-06 01:35:12 -06:00
|
|
|
/// Creates a new `BufferedReader` with the specified buffer capacity
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
|
2013-11-11 12:08:03 -06:00
|
|
|
// It's *much* faster to create an uninitialized buffer than it is to
|
|
|
|
// fill everything in with 0. This buffer is entirely an implementation
|
|
|
|
// detail and is never exposed, so we're safe to not initialize
|
|
|
|
// everything up-front. This allows creation of BufferedReader instances
|
|
|
|
// to be very cheap (large mallocs are not nearly as expensive as large
|
|
|
|
// callocs).
|
2014-03-22 19:18:27 -05:00
|
|
|
let mut buf = Vec::with_capacity(cap);
|
2013-12-15 06:05:30 -06:00
|
|
|
unsafe { buf.set_len(cap); }
|
2013-09-10 00:38:43 -05:00
|
|
|
BufferedReader {
|
|
|
|
inner: inner,
|
2013-11-11 12:08:03 -06:00
|
|
|
buf: buf,
|
2013-09-10 00:38:43 -05:00
|
|
|
pos: 0,
|
2014-01-07 11:47:53 -06:00
|
|
|
cap: 0,
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `BufferedReader` with a default buffer capacity
|
|
|
|
pub fn new(inner: R) -> BufferedReader<R> {
|
2014-01-15 15:25:09 -06:00
|
|
|
BufferedReader::with_capacity(DEFAULT_BUF_SIZE, inner)
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying reader.
|
|
|
|
///
|
|
|
|
/// This type does not expose the ability to get a mutable reference to the
|
|
|
|
/// underlying reader because that could possibly corrupt the buffer.
|
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a R { &self.inner }
|
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `BufferedReader`, returning the underlying reader.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// Note that any leftover data in the internal buffer is lost.
|
|
|
|
pub fn unwrap(self) -> R { self.inner }
|
2013-11-13 13:17:58 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl<R: Reader> Buffer for BufferedReader<R> {
|
2014-03-20 20:52:00 -05:00
|
|
|
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
|
2014-01-30 16:10:53 -06:00
|
|
|
if self.pos == self.cap {
|
2014-03-22 19:18:27 -05:00
|
|
|
self.cap = try!(self.inner.read(self.buf.as_mut_slice()));
|
2014-01-29 18:33:57 -06:00
|
|
|
self.pos = 0;
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(self.buf.slice(self.pos, self.cap))
|
2013-10-06 17:39:42 -05:00
|
|
|
}
|
2013-11-13 13:17:58 -06:00
|
|
|
|
|
|
|
fn consume(&mut self, amt: uint) {
|
|
|
|
self.pos += amt;
|
|
|
|
assert!(self.pos <= self.cap);
|
|
|
|
}
|
2013-10-06 17:39:42 -05:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-10-06 17:39:42 -05:00
|
|
|
impl<R: Reader> Reader for BufferedReader<R> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2013-10-06 17:39:42 -05:00
|
|
|
let nread = {
|
2014-03-20 20:52:00 -05:00
|
|
|
let available = try!(self.fill_buf());
|
2014-02-06 01:34:33 -06:00
|
|
|
let nread = cmp::min(available.len(), buf.len());
|
2014-03-08 17:11:52 -06:00
|
|
|
slice::bytes::copy_memory(buf, available.slice_to(nread));
|
2013-10-06 17:39:42 -05:00
|
|
|
nread
|
|
|
|
};
|
2013-09-10 00:38:43 -05:00
|
|
|
self.pos += nread;
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(nread)
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps a Writer and buffers output to it
|
|
|
|
///
|
2014-03-22 19:18:27 -05:00
|
|
|
/// 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.
|
|
|
|
///
|
2014-02-28 14:46:46 -06:00
|
|
|
/// This writer will be flushed when it is dropped.
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # #[allow(unused_must_use)];
|
2014-01-15 15:25:09 -06:00
|
|
|
/// use std::io::{BufferedWriter, File};
|
|
|
|
///
|
|
|
|
/// let file = File::open(&Path::new("message.txt"));
|
|
|
|
/// let mut writer = BufferedWriter::new(file);
|
|
|
|
///
|
|
|
|
/// writer.write_str("hello, world");
|
|
|
|
/// writer.flush();
|
|
|
|
/// ```
|
2013-09-10 00:38:43 -05:00
|
|
|
pub struct BufferedWriter<W> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: Option<W>,
|
|
|
|
buf: Vec<u8>,
|
|
|
|
pos: uint
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Writer> BufferedWriter<W> {
|
2014-03-06 01:35:12 -06:00
|
|
|
/// Creates a new `BufferedWriter` with the specified buffer capacity
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
|
2013-11-11 12:08:03 -06:00
|
|
|
// See comments in BufferedReader for why this uses unsafe code.
|
2014-03-22 19:18:27 -05:00
|
|
|
let mut buf = Vec::with_capacity(cap);
|
2013-12-15 06:05:30 -06:00
|
|
|
unsafe { buf.set_len(cap); }
|
2013-09-10 00:38:43 -05:00
|
|
|
BufferedWriter {
|
2014-02-28 14:46:46 -06:00
|
|
|
inner: Some(inner),
|
2013-11-11 12:08:03 -06:00
|
|
|
buf: buf,
|
2013-09-10 00:38:43 -05:00
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `BufferedWriter` with a default buffer capacity
|
|
|
|
pub fn new(inner: W) -> BufferedWriter<W> {
|
2014-01-15 15:25:09 -06:00
|
|
|
BufferedWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2013-11-30 10:27:39 -06:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
fn flush_buf(&mut self) -> IoResult<()> {
|
2013-11-30 10:27:39 -06:00
|
|
|
if self.pos != 0 {
|
2014-02-28 14:46:46 -06:00
|
|
|
let ret = self.inner.get_mut_ref().write(self.buf.slice_to(self.pos));
|
2013-11-30 10:27:39 -06:00
|
|
|
self.pos = 0;
|
2014-01-29 18:33:57 -06:00
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2013-11-30 10:27:39 -06:00
|
|
|
}
|
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying writer.
|
|
|
|
///
|
|
|
|
/// This type does not expose the ability to get a mutable reference to the
|
|
|
|
/// underlying reader because that could possibly corrupt the buffer.
|
2014-02-28 14:46:46 -06:00
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
|
2014-01-07 22:05:33 -06:00
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `BufferedWriter`, returning the underlying writer.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// The buffer is flushed before returning the writer.
|
|
|
|
pub fn unwrap(mut self) -> W {
|
2014-02-28 14:46:46 -06:00
|
|
|
// FIXME(#12628): is failing the right thing to do if flushing fails?
|
2014-01-29 18:33:57 -06:00
|
|
|
self.flush_buf().unwrap();
|
2014-02-28 14:46:46 -06:00
|
|
|
self.inner.take_unwrap()
|
2014-01-07 22:05:33 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Writer> Writer for BufferedWriter<W> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2013-09-10 00:38:43 -05:00
|
|
|
if self.pos + buf.len() > self.buf.len() {
|
2014-02-19 12:07:49 -06:00
|
|
|
try!(self.flush_buf());
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if buf.len() > self.buf.len() {
|
2014-02-28 14:46:46 -06:00
|
|
|
self.inner.get_mut_ref().write(buf)
|
2013-09-10 00:38:43 -05:00
|
|
|
} else {
|
|
|
|
let dst = self.buf.mut_slice_from(self.pos);
|
2014-03-08 17:11:52 -06:00
|
|
|
slice::bytes::copy_memory(dst, buf);
|
2013-09-10 00:38:43 -05:00
|
|
|
self.pos += buf.len();
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
fn flush(&mut self) -> IoResult<()> {
|
2014-02-28 14:46:46 -06:00
|
|
|
self.flush_buf().and_then(|()| self.inner.get_mut_ref().flush())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl<W: Writer> Drop for BufferedWriter<W> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.inner.is_some() {
|
|
|
|
// FIXME(#12628): should this error be ignored?
|
|
|
|
let _ = self.flush_buf();
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 23:26:09 -06:00
|
|
|
/// Wraps a Writer and buffers output to it, flushing whenever a newline (`0x0a`,
|
|
|
|
/// `'\n'`) is detected.
|
2013-10-24 19:30:36 -05:00
|
|
|
///
|
2014-02-28 14:46:46 -06:00
|
|
|
/// This writer will be flushed when it is dropped.
|
2013-10-24 19:30:36 -05:00
|
|
|
pub struct LineBufferedWriter<W> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: BufferedWriter<W>,
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Writer> LineBufferedWriter<W> {
|
|
|
|
/// Creates a new `LineBufferedWriter`
|
|
|
|
pub fn new(inner: W) -> LineBufferedWriter<W> {
|
|
|
|
// Lines typically aren't that long, don't use a giant buffer
|
|
|
|
LineBufferedWriter {
|
|
|
|
inner: BufferedWriter::with_capacity(1024, inner)
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying writer.
|
|
|
|
///
|
|
|
|
/// This type does not expose the ability to get a mutable reference to the
|
|
|
|
/// underlying reader because that could possibly corrupt the buffer.
|
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
|
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `LineBufferedWriter`, returning the underlying writer.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// The internal buffer is flushed before returning the writer.
|
2014-01-27 06:18:36 -06:00
|
|
|
pub fn unwrap(self) -> W { self.inner.unwrap() }
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-10-24 19:30:36 -05:00
|
|
|
impl<W: Writer> Writer for LineBufferedWriter<W> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2013-11-30 10:27:39 -06:00
|
|
|
match buf.iter().rposition(|&b| b == '\n' as u8) {
|
2013-10-24 19:30:36 -05:00
|
|
|
Some(i) => {
|
2014-02-19 12:07:49 -06:00
|
|
|
try!(self.inner.write(buf.slice_to(i + 1)));
|
|
|
|
try!(self.inner.flush());
|
|
|
|
try!(self.inner.write(buf.slice_from(i + 1)));
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
|
|
|
None => self.inner.write(buf),
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2013-10-24 19:30:36 -05:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
fn flush(&mut self) -> IoResult<()> { self.inner.flush() }
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
|
|
|
|
2013-10-03 02:15:54 -05:00
|
|
|
struct InternalBufferedWriter<W>(BufferedWriter<W>);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-11-01 20:06:31 -05:00
|
|
|
impl<W> InternalBufferedWriter<W> {
|
|
|
|
fn get_mut_ref<'a>(&'a mut self) -> &'a mut BufferedWriter<W> {
|
|
|
|
let InternalBufferedWriter(ref mut w) = *self;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
impl<W: Reader> Reader for InternalBufferedWriter<W> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-02-28 14:46:46 -06:00
|
|
|
self.get_mut_ref().inner.get_mut_ref().read(buf)
|
2014-01-29 18:33:57 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2014-02-28 14:46:46 -06:00
|
|
|
/// Wraps a Stream and buffers input and output to and from it.
|
2013-09-10 00:38:43 -05:00
|
|
|
///
|
2014-03-22 19:18:27 -05:00
|
|
|
/// 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`.
|
|
|
|
///
|
2014-02-28 14:46:46 -06:00
|
|
|
/// The output half will be flushed when this stream is dropped.
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # #[allow(unused_must_use)];
|
2014-01-15 15:25:09 -06:00
|
|
|
/// 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) {
|
2014-01-30 18:55:20 -06:00
|
|
|
/// Ok(nread) => println!("Read {} bytes", nread),
|
|
|
|
/// Err(e) => println!("error reading: {}", e)
|
2014-01-15 15:25:09 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2013-09-12 23:48:47 -05:00
|
|
|
pub struct BufferedStream<S> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: BufferedReader<InternalBufferedWriter<S>>
|
2013-09-12 23:48:47 -05:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
impl<S: Stream> BufferedStream<S> {
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Creates a new buffered stream with explicitly listed capacities for the
|
|
|
|
/// reader/writer buffer.
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn with_capacities(reader_cap: uint, writer_cap: uint, inner: S)
|
|
|
|
-> BufferedStream<S> {
|
|
|
|
let writer = BufferedWriter::with_capacity(writer_cap, inner);
|
2013-10-03 02:15:54 -05:00
|
|
|
let internal_writer = InternalBufferedWriter(writer);
|
2013-09-10 00:38:43 -05:00
|
|
|
let reader = BufferedReader::with_capacity(reader_cap,
|
|
|
|
internal_writer);
|
2013-09-12 23:48:47 -05:00
|
|
|
BufferedStream { inner: reader }
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Creates a new buffered stream with the default reader/writer buffer
|
|
|
|
/// capacities.
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn new(inner: S) -> BufferedStream<S> {
|
2014-01-15 15:25:09 -06:00
|
|
|
BufferedStream::with_capacities(DEFAULT_BUF_SIZE, DEFAULT_BUF_SIZE,
|
2013-09-10 00:38:43 -05:00
|
|
|
inner)
|
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// 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.
|
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a S {
|
|
|
|
let InternalBufferedWriter(ref w) = self.inner.inner;
|
|
|
|
w.get_ref()
|
|
|
|
}
|
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `BufferedStream`, returning the underlying stream.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// The internal buffer is flushed before returning the stream. Any leftover
|
|
|
|
/// data in the read buffer is lost.
|
|
|
|
pub fn unwrap(self) -> S {
|
|
|
|
let InternalBufferedWriter(w) = self.inner.inner;
|
|
|
|
w.unwrap()
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl<S: Stream> Buffer for BufferedStream<S> {
|
2014-03-20 20:52:00 -05:00
|
|
|
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> { self.inner.fill_buf() }
|
2013-11-13 13:17:58 -06:00
|
|
|
fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl<S: Stream> Reader for BufferedStream<S> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Stream> Writer for BufferedStream<S> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
|
|
|
self.inner.inner.get_mut_ref().write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> IoResult<()> {
|
|
|
|
self.inner.inner.get_mut_ref().flush()
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-02-13 19:49:11 -06:00
|
|
|
extern crate test;
|
2014-01-07 00:33:37 -06:00
|
|
|
use io;
|
2013-09-10 00:38:43 -05:00
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
2014-01-07 10:48:44 -06:00
|
|
|
use super::super::mem::{MemReader, MemWriter, BufReader};
|
2014-02-13 19:49:11 -06:00
|
|
|
use Harness = self::test::BenchHarness;
|
2013-11-12 11:38:54 -06:00
|
|
|
|
2014-01-15 15:25:09 -06:00
|
|
|
/// A type, free to create, primarily intended for benchmarking creation of
|
|
|
|
/// wrappers that, just for construction, don't need a Reader/Writer that
|
|
|
|
/// does anything useful. Is equivalent to `/dev/null` in semantics.
|
2013-11-12 11:38:54 -06:00
|
|
|
#[deriving(Clone,Eq,Ord)]
|
|
|
|
pub struct NullStream;
|
|
|
|
|
|
|
|
impl Reader for NullStream {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::IoResult<uint> {
|
|
|
|
Err(io::standard_error(io::EndOfFile))
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for NullStream {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) }
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-12-22 12:07:08 -06:00
|
|
|
/// A dummy reader intended at testing short-reads propagation.
|
|
|
|
pub struct ShortReader {
|
2014-03-27 17:09:47 -05:00
|
|
|
lengths: ~[uint],
|
2013-12-22 12:07:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for ShortReader {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::IoResult<uint> {
|
|
|
|
match self.lengths.shift() {
|
|
|
|
Some(i) => Ok(i),
|
|
|
|
None => Err(io::standard_error(io::EndOfFile))
|
|
|
|
}
|
2013-12-22 12:07:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_buffered_reader() {
|
|
|
|
let inner = MemReader::new(~[0, 1, 2, 3, 4]);
|
|
|
|
let mut reader = BufferedReader::with_capacity(2, inner);
|
|
|
|
|
|
|
|
let mut buf = [0, 0, 0];
|
|
|
|
let nread = reader.read(buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(2), nread);
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(buf.as_slice(), &[0, 1, 0]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
let mut buf = [0];
|
|
|
|
let nread = reader.read(buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(1), nread);
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(buf.as_slice(), &[2]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
let mut buf = [0, 0, 0];
|
|
|
|
let nread = reader.read(buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(1), nread);
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(buf.as_slice(), &[3, 0, 0]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
let nread = reader.read(buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(1), nread);
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(buf.as_slice(), &[4, 0, 0]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
assert!(reader.read(buf).is_err());
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buffered_writer() {
|
|
|
|
let inner = MemWriter::new();
|
|
|
|
let mut writer = BufferedWriter::with_capacity(2, inner);
|
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([0, 1]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(), &[]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([2]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(), &[0, 1]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([3]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(), &[0, 1]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[0, 1, 2, 3], writer.get_ref().get_ref());
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([4]).unwrap();
|
|
|
|
writer.write([5]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[0, 1, 2, 3], writer.get_ref().get_ref());
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([6]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[0, 1, 2, 3, 4, 5],
|
2014-01-07 22:05:33 -06:00
|
|
|
writer.get_ref().get_ref());
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([7, 8]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[0, 1, 2, 3, 4, 5, 6],
|
2014-01-07 22:05:33 -06:00
|
|
|
writer.get_ref().get_ref());
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([9, 10, 11]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
|
2014-01-07 22:05:33 -06:00
|
|
|
writer.get_ref().get_ref());
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
|
2014-01-07 22:05:33 -06:00
|
|
|
writer.get_ref().get_ref());
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-30 10:27:39 -06:00
|
|
|
#[test]
|
|
|
|
fn test_buffered_writer_inner_flushes() {
|
|
|
|
let mut w = BufferedWriter::with_capacity(3, MemWriter::new());
|
2014-01-30 16:10:53 -06:00
|
|
|
w.write([0, 1]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[], w.get_ref().get_ref());
|
2014-01-07 22:05:33 -06:00
|
|
|
let w = w.unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(&[0, 1], w.get_ref());
|
2013-11-30 10:27:39 -06:00
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
// This is just here to make sure that we don't infinite loop in the
|
|
|
|
// newtype struct autoderef weirdness
|
|
|
|
#[test]
|
|
|
|
fn test_buffered_stream() {
|
|
|
|
struct S;
|
|
|
|
|
2013-11-11 00:46:32 -06:00
|
|
|
impl io::Writer for S {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) }
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:46:32 -06:00
|
|
|
impl io::Reader for S {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::IoResult<uint> {
|
|
|
|
Err(io::standard_error(io::EndOfFile))
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut stream = BufferedStream::new(S);
|
|
|
|
let mut buf = [];
|
2014-01-30 16:10:53 -06:00
|
|
|
assert!(stream.read(buf).is_err());
|
|
|
|
stream.write(buf).unwrap();
|
|
|
|
stream.flush().unwrap();
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2013-10-06 17:39:42 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_until() {
|
|
|
|
let inner = MemReader::new(~[0, 1, 2, 1, 0]);
|
|
|
|
let mut reader = BufferedReader::with_capacity(2, inner);
|
2014-03-26 11:24:16 -05:00
|
|
|
assert_eq!(reader.read_until(0), Ok(vec!(0)));
|
|
|
|
assert_eq!(reader.read_until(2), Ok(vec!(1, 2)));
|
|
|
|
assert_eq!(reader.read_until(1), Ok(vec!(1)));
|
|
|
|
assert_eq!(reader.read_until(8), Ok(vec!(0)));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert!(reader.read_until(9).is_err());
|
2013-10-06 17:39:42 -05:00
|
|
|
}
|
2013-10-24 19:30:36 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_line_buffer() {
|
|
|
|
let mut writer = LineBufferedWriter::new(MemWriter::new());
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([0]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(), &[]);
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([1]).unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(), &[]);
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2014-02-28 03:23:06 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(), &[0, 1]);
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([0, '\n' as u8, 1, '\n' as u8, 2]).unwrap();
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(),
|
2014-02-28 03:23:06 -06:00
|
|
|
&[0, 1, 0, '\n' as u8, 1, '\n' as u8]);
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(),
|
2014-02-28 03:23:06 -06:00
|
|
|
&[0, 1, 0, '\n' as u8, 1, '\n' as u8, 2]);
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write([3, '\n' as u8]).unwrap();
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref().get_ref(),
|
2014-02-28 03:23:06 -06:00
|
|
|
&[0, 1, 0, '\n' as u8, 1, '\n' as u8, 2, 3, '\n' as u8]);
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
2013-11-12 11:38:54 -06:00
|
|
|
|
2013-12-08 01:12:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_read_line() {
|
|
|
|
let in_buf = MemReader::new(bytes!("a\nb\nc").to_owned());
|
|
|
|
let mut reader = BufferedReader::with_capacity(2, in_buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.read_line(), Ok(~"a\n"));
|
|
|
|
assert_eq!(reader.read_line(), Ok(~"b\n"));
|
|
|
|
assert_eq!(reader.read_line(), Ok(~"c"));
|
|
|
|
assert!(reader.read_line().is_err());
|
2013-12-08 01:12:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lines() {
|
|
|
|
let in_buf = MemReader::new(bytes!("a\nb\nc").to_owned());
|
|
|
|
let mut reader = BufferedReader::with_capacity(2, in_buf);
|
|
|
|
let mut it = reader.lines();
|
2014-02-19 20:53:46 -06:00
|
|
|
assert_eq!(it.next(), Some(Ok(~"a\n")));
|
|
|
|
assert_eq!(it.next(), Some(Ok(~"b\n")));
|
|
|
|
assert_eq!(it.next(), Some(Ok(~"c")));
|
2013-12-08 01:12:07 -06:00
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:07:08 -06:00
|
|
|
#[test]
|
|
|
|
fn test_short_reads() {
|
|
|
|
let inner = ShortReader{lengths: ~[0, 1, 2, 0, 1, 0]};
|
|
|
|
let mut reader = BufferedReader::new(inner);
|
|
|
|
let mut buf = [0, 0];
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.read(buf), Ok(0));
|
|
|
|
assert_eq!(reader.read(buf), Ok(1));
|
|
|
|
assert_eq!(reader.read(buf), Ok(2));
|
|
|
|
assert_eq!(reader.read(buf), Ok(0));
|
|
|
|
assert_eq!(reader.read(buf), Ok(1));
|
|
|
|
assert_eq!(reader.read(buf), Ok(0));
|
|
|
|
assert!(reader.read(buf).is_err());
|
2013-12-22 12:07:08 -06:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:48:44 -06:00
|
|
|
#[test]
|
|
|
|
fn read_char_buffered() {
|
|
|
|
let buf = [195u8, 159u8];
|
|
|
|
let mut reader = BufferedReader::with_capacity(1, BufReader::new(buf));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.read_char(), Ok('ß'));
|
2014-01-07 10:48:44 -06:00
|
|
|
}
|
2013-12-08 01:12:07 -06:00
|
|
|
|
2014-02-08 23:40:38 -06:00
|
|
|
#[test]
|
|
|
|
fn test_chars() {
|
|
|
|
let buf = [195u8, 159u8, 'a' as u8];
|
|
|
|
let mut reader = BufferedReader::with_capacity(1, BufReader::new(buf));
|
|
|
|
let mut it = reader.chars();
|
2014-02-19 20:53:46 -06:00
|
|
|
assert_eq!(it.next(), Some(Ok('ß')));
|
|
|
|
assert_eq!(it.next(), Some(Ok('a')));
|
2014-02-08 23:40:38 -06:00
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2013-11-12 11:38:54 -06:00
|
|
|
#[bench]
|
|
|
|
fn bench_buffered_reader(bh: &mut Harness) {
|
2013-11-21 19:23:21 -06:00
|
|
|
bh.iter(|| {
|
2014-02-12 09:39:21 -06:00
|
|
|
BufferedReader::new(NullStream)
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_buffered_writer(bh: &mut Harness) {
|
2013-11-21 19:23:21 -06:00
|
|
|
bh.iter(|| {
|
2014-02-12 09:39:21 -06:00
|
|
|
BufferedWriter::new(NullStream)
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_buffered_stream(bh: &mut Harness) {
|
2013-11-21 19:23:21 -06:00
|
|
|
bh.iter(|| {
|
2013-11-12 11:38:54 -06:00
|
|
|
BufferedStream::new(NullStream);
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|