1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-lexer-test FIXME #15883
13 //! Buffering wrappers for I/O traits
19 use error::{self, FromError};
21 use io::{self, Cursor, DEFAULT_BUF_SIZE, Error, ErrorKind};
24 /// Wraps a `Read` and buffers input from it
26 /// It can be excessively inefficient to work directly with a `Read` instance.
27 /// For example, every call to `read` on `TcpStream` results in a system call.
28 /// A `BufReader` performs large, infrequent reads on the underlying `Read`
29 /// and maintains an in-memory buffer of the results.
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub struct BufReader<R> {
36 impl<R: Read> BufReader<R> {
37 /// Creates a new `BufReader` with a default buffer capacity
38 #[stable(feature = "rust1", since = "1.0.0")]
39 pub fn new(inner: R) -> BufReader<R> {
40 BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
43 /// Creates a new `BufReader` with the specified buffer capacity
44 #[stable(feature = "rust1", since = "1.0.0")]
45 pub fn with_capacity(cap: usize, inner: R) -> BufReader<R> {
48 buf: Cursor::new(Vec::with_capacity(cap)),
52 /// Gets a reference to the underlying reader.
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub fn get_ref(&self) -> &R { &self.inner }
56 /// Gets a mutable reference to the underlying reader.
60 /// It is inadvisable to directly read from the underlying reader.
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
64 /// Unwraps this `BufReader`, returning the underlying reader.
66 /// Note that any leftover data in the internal buffer is lost.
67 #[stable(feature = "rust1", since = "1.0.0")]
68 pub fn into_inner(self) -> R { self.inner }
71 #[stable(feature = "rust1", since = "1.0.0")]
72 impl<R: Read> Read for BufReader<R> {
73 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
74 // If we don't have any buffered data and we're doing a massive read
75 // (larger than our internal buffer), bypass our internal buffer
77 if self.buf.get_ref().len() == self.buf.position() as usize &&
78 buf.len() >= self.buf.get_ref().capacity() {
79 return self.inner.read(buf);
81 try!(self.fill_buf());
86 #[stable(feature = "rust1", since = "1.0.0")]
87 impl<R: Read> BufRead for BufReader<R> {
88 fn fill_buf(&mut self) -> io::Result<&[u8]> {
89 // If we've reached the end of our internal buffer then we need to fetch
90 // some more data from the underlying reader.
91 if self.buf.position() as usize == self.buf.get_ref().len() {
92 self.buf.set_position(0);
93 let v = self.buf.get_mut();
95 let inner = &mut self.inner;
96 try!(super::with_end_to_cap(v, |b| inner.read(b)));
101 fn consume(&mut self, amt: uint) {
102 self.buf.consume(amt)
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<R> fmt::Debug for BufReader<R> where R: fmt::Debug {
108 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
109 write!(fmt, "BufReader {{ reader: {:?}, buffer: {}/{} }}",
110 self.inner, self.buf.position(), self.buf.get_ref().len())
114 /// Wraps a Writer and buffers output to it
116 /// It can be excessively inefficient to work directly with a `Write`. For
117 /// example, every call to `write` on `TcpStream` results in a system call. A
118 /// `BufWriter` keeps an in memory buffer of data and writes it to the
119 /// underlying `Write` in large, infrequent batches.
121 /// This writer will be flushed when it is dropped.
122 #[stable(feature = "rust1", since = "1.0.0")]
123 pub struct BufWriter<W> {
128 /// An error returned by `into_inner` which indicates whether a flush error
131 #[stable(feature = "rust1", since = "1.0.0")]
132 pub struct IntoInnerError<W>(W, Error);
134 impl<W: Write> BufWriter<W> {
135 /// Creates a new `BufWriter` with a default buffer capacity
136 #[stable(feature = "rust1", since = "1.0.0")]
137 pub fn new(inner: W) -> BufWriter<W> {
138 BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
141 /// Creates a new `BufWriter` with the specified buffer capacity
142 #[stable(feature = "rust1", since = "1.0.0")]
143 pub fn with_capacity(cap: usize, inner: W) -> BufWriter<W> {
146 buf: Vec::with_capacity(cap),
150 fn flush_buf(&mut self) -> io::Result<()> {
152 let len = self.buf.len();
153 let mut ret = Ok(());
154 while written < len {
155 match self.inner.as_mut().unwrap().write(&self.buf[written..]) {
157 ret = Err(Error::new(ErrorKind::WriteZero,
158 "failed to flush", None));
161 Ok(n) => written += n,
162 Err(e) => { ret = Err(e); break }
167 // NB: would be better expressed as .remove(0..n) if it existed
169 ptr::copy(self.buf.as_mut_ptr(),
170 self.buf.as_ptr().offset(written as isize),
174 self.buf.truncate(len - written);
178 /// Gets a reference to the underlying writer.
179 #[stable(feature = "rust1", since = "1.0.0")]
180 pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
182 /// Gets a mutable reference to the underlying write.
186 /// It is inadvisable to directly read from the underlying writer.
187 #[stable(feature = "rust1", since = "1.0.0")]
188 pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
190 /// Unwraps this `BufWriter`, returning the underlying writer.
192 /// The buffer is flushed before returning the writer.
193 #[stable(feature = "rust1", since = "1.0.0")]
194 pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
195 match self.flush_buf() {
196 Err(e) => Err(IntoInnerError(self, e)),
197 Ok(()) => Ok(self.inner.take().unwrap())
202 #[stable(feature = "rust1", since = "1.0.0")]
203 impl<W: Write> Write for BufWriter<W> {
204 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
205 if self.buf.len() + buf.len() > self.buf.capacity() {
206 try!(self.flush_buf());
208 if buf.len() >= self.buf.capacity() {
209 self.inner.as_mut().unwrap().write(buf)
211 let amt = cmp::min(buf.len(), self.buf.capacity());
212 Write::write(&mut self.buf, &buf[..amt])
215 fn flush(&mut self) -> io::Result<()> {
216 self.flush_buf().and_then(|()| self.get_mut().flush())
220 #[stable(feature = "rust1", since = "1.0.0")]
221 impl<W> fmt::Debug for BufWriter<W> where W: fmt::Debug {
222 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
223 write!(fmt, "BufWriter {{ writer: {:?}, buffer: {}/{} }}",
224 self.inner.as_ref().unwrap(), self.buf.len(), self.buf.capacity())
229 impl<W: Write> Drop for BufWriter<W> {
231 if self.inner.is_some() {
232 // dtors should not panic, so we ignore a failed flush
233 let _r = self.flush_buf();
238 impl<W> IntoInnerError<W> {
239 /// Returns the error which caused the call to `into_inner` to fail.
241 /// This error was returned when attempting to flush the internal buffer.
242 #[stable(feature = "rust1", since = "1.0.0")]
243 pub fn error(&self) -> &Error { &self.1 }
245 /// Returns the underlying `BufWriter` instance which generated the error.
247 /// The returned object can be used to retry a flush or re-inspect the
249 #[stable(feature = "rust1", since = "1.0.0")]
250 pub fn into_inner(self) -> W { self.0 }
253 #[stable(feature = "rust1", since = "1.0.0")]
254 impl<W> FromError<IntoInnerError<W>> for Error {
255 fn from_error(iie: IntoInnerError<W>) -> Error { iie.1 }
258 #[stable(feature = "rust1", since = "1.0.0")]
259 impl<W> error::Error for IntoInnerError<W> {
260 fn description(&self) -> &str {
261 error::Error::description(self.error())
265 #[stable(feature = "rust1", since = "1.0.0")]
266 impl<W> fmt::Display for IntoInnerError<W> {
267 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
272 /// Wraps a Writer and buffers output to it, flushing whenever a newline
273 /// (`0x0a`, `'\n'`) is detected.
275 /// This writer will be flushed when it is dropped.
276 #[stable(feature = "rust1", since = "1.0.0")]
277 pub struct LineWriter<W> {
281 impl<W: Write> LineWriter<W> {
282 /// Creates a new `LineWriter`
283 #[stable(feature = "rust1", since = "1.0.0")]
284 pub fn new(inner: W) -> LineWriter<W> {
285 // Lines typically aren't that long, don't use a giant buffer
286 LineWriter::with_capacity(1024, inner)
289 /// Creates a new `LineWriter` with a specified capacity for the internal
291 #[stable(feature = "rust1", since = "1.0.0")]
292 pub fn with_capacity(cap: usize, inner: W) -> LineWriter<W> {
293 LineWriter { inner: BufWriter::with_capacity(cap, inner) }
296 /// Gets a reference to the underlying writer.
297 #[stable(feature = "rust1", since = "1.0.0")]
298 pub fn get_ref(&self) -> &W { self.inner.get_ref() }
300 /// Gets a mutable reference to the underlying writer.
302 /// Caution must be taken when calling methods on the mutable reference
303 /// returned as extra writes could corrupt the output stream.
304 #[stable(feature = "rust1", since = "1.0.0")]
305 pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
307 /// Unwraps this `LineWriter`, returning the underlying writer.
309 /// The internal buffer is flushed before returning the writer.
310 #[stable(feature = "rust1", since = "1.0.0")]
311 pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
312 self.inner.into_inner().map_err(|IntoInnerError(buf, e)| {
313 IntoInnerError(LineWriter { inner: buf }, e)
318 #[stable(feature = "rust1", since = "1.0.0")]
319 impl<W: Write> Write for LineWriter<W> {
320 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
321 match buf.rposition_elem(&b'\n') {
323 let n = try!(self.inner.write(&buf[..i + 1]));
324 if n != i + 1 { return Ok(n) }
325 try!(self.inner.flush());
326 self.inner.write(&buf[i + 1..]).map(|i| n + i)
328 None => self.inner.write(buf),
332 fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
335 #[stable(feature = "rust1", since = "1.0.0")]
336 impl<W> fmt::Debug for LineWriter<W> where W: fmt::Debug {
337 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
338 write!(fmt, "LineWriter {{ writer: {:?}, buffer: {}/{} }}",
339 self.inner.inner, self.inner.buf.len(),
340 self.inner.buf.capacity())
344 struct InternalBufWriter<W>(BufWriter<W>);
346 impl<W> InternalBufWriter<W> {
347 fn get_mut(&mut self) -> &mut BufWriter<W> {
348 let InternalBufWriter(ref mut w) = *self;
353 impl<W: Read> Read for InternalBufWriter<W> {
354 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
355 self.get_mut().inner.as_mut().unwrap().read(buf)
359 /// Wraps a Stream and buffers input and output to and from it.
361 /// It can be excessively inefficient to work directly with a `Read+Write`. For
362 /// example, every call to `read` or `write` on `TcpStream` results in a system
363 /// call. A `BufStream` keeps in memory buffers of data, making large,
364 /// infrequent calls to `read` and `write` on the underlying `Read+Write`.
366 /// The output half will be flushed when this stream is dropped.
367 #[stable(feature = "rust1", since = "1.0.0")]
368 pub struct BufStream<S> {
369 inner: BufReader<InternalBufWriter<S>>
372 impl<S: Read + Write> BufStream<S> {
373 /// Creates a new buffered stream with explicitly listed capacities for the
374 /// reader/writer buffer.
375 #[stable(feature = "rust1", since = "1.0.0")]
376 pub fn with_capacities(reader_cap: usize, writer_cap: usize, inner: S)
378 let writer = BufWriter::with_capacity(writer_cap, inner);
379 let internal_writer = InternalBufWriter(writer);
380 let reader = BufReader::with_capacity(reader_cap, internal_writer);
381 BufStream { inner: reader }
384 /// Creates a new buffered stream with the default reader/writer buffer
386 #[stable(feature = "rust1", since = "1.0.0")]
387 pub fn new(inner: S) -> BufStream<S> {
388 BufStream::with_capacities(DEFAULT_BUF_SIZE, DEFAULT_BUF_SIZE, inner)
391 /// Gets a reference to the underlying stream.
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub fn get_ref(&self) -> &S {
394 let InternalBufWriter(ref w) = self.inner.inner;
398 /// Gets a mutable reference to the underlying stream.
402 /// It is inadvisable to read directly from or write directly to the
403 /// underlying stream.
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub fn get_mut(&mut self) -> &mut S {
406 let InternalBufWriter(ref mut w) = self.inner.inner;
410 /// Unwraps this `BufStream`, returning the underlying stream.
412 /// The internal buffer is flushed before returning the stream. Any leftover
413 /// data in the read buffer is lost.
414 #[stable(feature = "rust1", since = "1.0.0")]
415 pub fn into_inner(self) -> Result<S, IntoInnerError<BufStream<S>>> {
416 let BufReader { inner: InternalBufWriter(w), buf } = self.inner;
417 w.into_inner().map_err(|IntoInnerError(w, e)| {
418 IntoInnerError(BufStream {
419 inner: BufReader { inner: InternalBufWriter(w), buf: buf },
425 #[stable(feature = "rust1", since = "1.0.0")]
426 impl<S: Read + Write> BufRead for BufStream<S> {
427 fn fill_buf(&mut self) -> io::Result<&[u8]> { self.inner.fill_buf() }
428 fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
431 #[stable(feature = "rust1", since = "1.0.0")]
432 impl<S: Read + Write> Read for BufStream<S> {
433 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
438 #[stable(feature = "rust1", since = "1.0.0")]
439 impl<S: Read + Write> Write for BufStream<S> {
440 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
441 self.inner.inner.get_mut().write(buf)
443 fn flush(&mut self) -> io::Result<()> {
444 self.inner.inner.get_mut().flush()
448 #[stable(feature = "rust1", since = "1.0.0")]
449 impl<S> fmt::Debug for BufStream<S> where S: fmt::Debug {
450 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
451 let reader = &self.inner;
452 let writer = &self.inner.inner.0;
453 write!(fmt, "BufStream {{ stream: {:?}, write_buffer: {}/{}, read_buffer: {}/{} }}",
455 writer.buf.len(), writer.buf.capacity(),
456 reader.buf.position(), reader.buf.get_ref().len())
464 use io::{self, BufReader, BufWriter, BufStream, Cursor, LineWriter};
467 /// A dummy reader intended at testing short-reads propagation.
468 pub struct ShortReader {
472 impl Read for ShortReader {
473 fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
474 if self.lengths.is_empty() {
477 Ok(self.lengths.remove(0))
483 fn test_buffered_reader() {
484 let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
485 let mut reader = BufReader::with_capacity(2, inner);
487 let mut buf = [0, 0, 0];
488 let nread = reader.read(&mut buf);
489 assert_eq!(Ok(3), nread);
490 let b: &[_] = &[5, 6, 7];
493 let mut buf = [0, 0];
494 let nread = reader.read(&mut buf);
495 assert_eq!(Ok(2), nread);
496 let b: &[_] = &[0, 1];
500 let nread = reader.read(&mut buf);
501 assert_eq!(Ok(1), nread);
505 let mut buf = [0, 0, 0];
506 let nread = reader.read(&mut buf);
507 assert_eq!(Ok(1), nread);
508 let b: &[_] = &[3, 0, 0];
511 let nread = reader.read(&mut buf);
512 assert_eq!(Ok(1), nread);
513 let b: &[_] = &[4, 0, 0];
516 assert_eq!(reader.read(&mut buf), Ok(0));
520 fn test_buffered_writer() {
521 let inner = Vec::new();
522 let mut writer = BufWriter::with_capacity(2, inner);
524 writer.write(&[0, 1]).unwrap();
525 assert_eq!(*writer.get_ref(), [0, 1]);
527 writer.write(&[2]).unwrap();
528 assert_eq!(*writer.get_ref(), [0, 1]);
530 writer.write(&[3]).unwrap();
531 assert_eq!(*writer.get_ref(), [0, 1]);
533 writer.flush().unwrap();
534 assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
536 writer.write(&[4]).unwrap();
537 writer.write(&[5]).unwrap();
538 assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
540 writer.write(&[6]).unwrap();
541 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]);
543 writer.write(&[7, 8]).unwrap();
544 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]);
546 writer.write(&[9, 10, 11]).unwrap();
547 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
549 writer.flush().unwrap();
550 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
554 fn test_buffered_writer_inner_flushes() {
555 let mut w = BufWriter::with_capacity(3, Vec::new());
556 w.write(&[0, 1]).unwrap();
557 assert_eq!(*w.get_ref(), []);
558 let w = w.into_inner().unwrap();
559 assert_eq!(w, [0, 1]);
562 // This is just here to make sure that we don't infinite loop in the
563 // newtype struct autoderef weirdness
565 fn test_buffered_stream() {
569 fn write(&mut self, b: &[u8]) -> io::Result<usize> { Ok(b.len()) }
570 fn flush(&mut self) -> io::Result<()> { Ok(()) }
574 fn read(&mut self, _: &mut [u8]) -> io::Result<usize> { Ok(0) }
577 let mut stream = BufStream::new(S);
578 assert_eq!(stream.read(&mut [0; 10]), Ok(0));
579 stream.write(&[0; 10]).unwrap();
580 stream.flush().unwrap();
584 fn test_read_until() {
585 let inner: &[u8] = &[0, 1, 2, 1, 0];
586 let mut reader = BufReader::with_capacity(2, inner);
587 let mut v = Vec::new();
588 reader.read_until(0, &mut v).unwrap();
591 reader.read_until(2, &mut v).unwrap();
592 assert_eq!(v, [1, 2]);
594 reader.read_until(1, &mut v).unwrap();
597 reader.read_until(8, &mut v).unwrap();
600 reader.read_until(9, &mut v).unwrap();
605 fn test_line_buffer() {
606 let mut writer = LineWriter::new(Vec::new());
607 writer.write(&[0]).unwrap();
608 assert_eq!(*writer.get_ref(), []);
609 writer.write(&[1]).unwrap();
610 assert_eq!(*writer.get_ref(), []);
611 writer.flush().unwrap();
612 assert_eq!(*writer.get_ref(), [0, 1]);
613 writer.write(&[0, b'\n', 1, b'\n', 2]).unwrap();
614 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);
615 writer.flush().unwrap();
616 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);
617 writer.write(&[3, b'\n']).unwrap();
618 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
622 fn test_read_line() {
623 let in_buf = b"a\nb\nc";
624 let mut reader = BufReader::with_capacity(2, in_buf);
625 let mut s = String::new();
626 reader.read_line(&mut s).unwrap();
627 assert_eq!(s, "a\n");
629 reader.read_line(&mut s).unwrap();
630 assert_eq!(s, "b\n");
632 reader.read_line(&mut s).unwrap();
635 reader.read_line(&mut s).unwrap();
641 let in_buf = b"a\nb\nc";
642 let reader = BufReader::with_capacity(2, in_buf);
643 let mut it = reader.lines();
644 assert_eq!(it.next(), Some(Ok("a".to_string())));
645 assert_eq!(it.next(), Some(Ok("b".to_string())));
646 assert_eq!(it.next(), Some(Ok("c".to_string())));
647 assert_eq!(it.next(), None);
651 fn test_short_reads() {
652 let inner = ShortReader{lengths: vec![0, 1, 2, 0, 1, 0]};
653 let mut reader = BufReader::new(inner);
654 let mut buf = [0, 0];
655 assert_eq!(reader.read(&mut buf), Ok(0));
656 assert_eq!(reader.read(&mut buf), Ok(1));
657 assert_eq!(reader.read(&mut buf), Ok(2));
658 assert_eq!(reader.read(&mut buf), Ok(0));
659 assert_eq!(reader.read(&mut buf), Ok(1));
660 assert_eq!(reader.read(&mut buf), Ok(0));
661 assert_eq!(reader.read(&mut buf), Ok(0));
665 fn read_char_buffered() {
666 let buf = [195, 159];
667 let reader = BufReader::with_capacity(1, &buf[..]);
668 assert_eq!(reader.chars().next(), Some(Ok('ß')));
673 let buf = [195, 159, b'a'];
674 let reader = BufReader::with_capacity(1, &buf[..]);
675 let mut it = reader.chars();
676 assert_eq!(it.next(), Some(Ok('ß')));
677 assert_eq!(it.next(), Some(Ok('a')));
678 assert_eq!(it.next(), None);
683 fn dont_panic_in_drop_on_panicked_flush() {
684 struct FailFlushWriter;
686 impl Write for FailFlushWriter {
687 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
688 fn flush(&mut self) -> io::Result<()> {
689 Err(io::Error::last_os_error())
693 let writer = FailFlushWriter;
694 let _writer = BufWriter::new(writer);
696 // If writer panics *again* due to the flush error then the process will
702 fn bench_buffered_reader(b: &mut test::Bencher) {
704 BufReader::new(io::empty())
709 fn bench_buffered_writer(b: &mut test::Bencher) {
711 BufWriter::new(io::sink())
716 fn bench_buffered_stream(b: &mut test::Bencher) {
717 let mut buf = Cursor::new(Vec::new());
719 BufStream::new(&mut buf);