]> git.proxmox.com Git - rustc.git/blob - src/libstd/io/buffered.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / libstd / io / buffered.rs
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.
4 //
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.
10 //
11 // ignore-lexer-test FIXME #15883
12
13 //! Buffering wrappers for I/O traits
14
15 use prelude::v1::*;
16 use io::prelude::*;
17
18 use cmp;
19 use error;
20 use fmt;
21 use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind};
22 use ptr;
23 use iter;
24
25 /// Wraps a `Read` and buffers input from it
26 ///
27 /// It can be excessively inefficient to work directly with a `Read` instance.
28 /// For example, every call to `read` on `TcpStream` results in a system call.
29 /// A `BufReader` performs large, infrequent reads on the underlying `Read`
30 /// and maintains an in-memory buffer of the results.
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub struct BufReader<R> {
33 inner: R,
34 buf: Vec<u8>,
35 pos: usize,
36 cap: usize,
37 }
38
39 impl<R: Read> BufReader<R> {
40 /// Creates a new `BufReader` with a default buffer capacity
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub fn new(inner: R) -> BufReader<R> {
43 BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
44 }
45
46 /// Creates a new `BufReader` with the specified buffer capacity
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub fn with_capacity(cap: usize, inner: R) -> BufReader<R> {
49 let mut buf = Vec::with_capacity(cap);
50 buf.extend(iter::repeat(0).take(cap));
51 BufReader {
52 inner: inner,
53 buf: buf,
54 pos: 0,
55 cap: 0,
56 }
57 }
58
59 /// Gets a reference to the underlying reader.
60 #[stable(feature = "rust1", since = "1.0.0")]
61 pub fn get_ref(&self) -> &R { &self.inner }
62
63 /// Gets a mutable reference to the underlying reader.
64 ///
65 /// # Warning
66 ///
67 /// It is inadvisable to directly read from the underlying reader.
68 #[stable(feature = "rust1", since = "1.0.0")]
69 pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
70
71 /// Unwraps this `BufReader`, returning the underlying reader.
72 ///
73 /// Note that any leftover data in the internal buffer is lost.
74 #[stable(feature = "rust1", since = "1.0.0")]
75 pub fn into_inner(self) -> R { self.inner }
76 }
77
78 #[stable(feature = "rust1", since = "1.0.0")]
79 impl<R: Read> Read for BufReader<R> {
80 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
81 // If we don't have any buffered data and we're doing a massive read
82 // (larger than our internal buffer), bypass our internal buffer
83 // entirely.
84 if self.pos == self.cap && buf.len() >= self.buf.len() {
85 return self.inner.read(buf);
86 }
87 let nread = {
88 let mut rem = try!(self.fill_buf());
89 try!(rem.read(buf))
90 };
91 self.consume(nread);
92 Ok(nread)
93 }
94 }
95
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl<R: Read> BufRead for BufReader<R> {
98 fn fill_buf(&mut self) -> io::Result<&[u8]> {
99 // If we've reached the end of our internal buffer then we need to fetch
100 // some more data from the underlying reader.
101 if self.pos == self.cap {
102 self.cap = try!(self.inner.read(&mut self.buf));
103 self.pos = 0;
104 }
105 Ok(&self.buf[self.pos..self.cap])
106 }
107
108 fn consume(&mut self, amt: usize) {
109 self.pos = cmp::min(self.pos + amt, self.cap);
110 }
111 }
112
113 #[stable(feature = "rust1", since = "1.0.0")]
114 impl<R> fmt::Debug for BufReader<R> where R: fmt::Debug {
115 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
116 fmt.debug_struct("BufReader")
117 .field("reader", &self.inner)
118 .field("buffer", &format_args!("{}/{}", self.cap - self.pos, self.buf.len()))
119 .finish()
120 }
121 }
122
123 /// Wraps a Writer and buffers output to it
124 ///
125 /// It can be excessively inefficient to work directly with a `Write`. For
126 /// example, every call to `write` on `TcpStream` results in a system call. A
127 /// `BufWriter` keeps an in memory buffer of data and writes it to the
128 /// underlying `Write` in large, infrequent batches.
129 ///
130 /// The buffer will be written out when the writer is dropped.
131 #[stable(feature = "rust1", since = "1.0.0")]
132 pub struct BufWriter<W: Write> {
133 inner: Option<W>,
134 buf: Vec<u8>,
135 }
136
137 /// An error returned by `into_inner` which combines an error that
138 /// happened while writing out the buffer, and the buffered writer object
139 /// which may be used to recover from the condition.
140 #[derive(Debug)]
141 #[stable(feature = "rust1", since = "1.0.0")]
142 pub struct IntoInnerError<W>(W, Error);
143
144 impl<W: Write> BufWriter<W> {
145 /// Creates a new `BufWriter` with a default buffer capacity
146 #[stable(feature = "rust1", since = "1.0.0")]
147 pub fn new(inner: W) -> BufWriter<W> {
148 BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
149 }
150
151 /// Creates a new `BufWriter` with the specified buffer capacity
152 #[stable(feature = "rust1", since = "1.0.0")]
153 pub fn with_capacity(cap: usize, inner: W) -> BufWriter<W> {
154 BufWriter {
155 inner: Some(inner),
156 buf: Vec::with_capacity(cap),
157 }
158 }
159
160 fn flush_buf(&mut self) -> io::Result<()> {
161 let mut written = 0;
162 let len = self.buf.len();
163 let mut ret = Ok(());
164 while written < len {
165 match self.inner.as_mut().unwrap().write(&self.buf[written..]) {
166 Ok(0) => {
167 ret = Err(Error::new(ErrorKind::WriteZero,
168 "failed to write the buffered data"));
169 break;
170 }
171 Ok(n) => written += n,
172 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
173 Err(e) => { ret = Err(e); break }
174
175 }
176 }
177 if written > 0 {
178 // NB: would be better expressed as .remove(0..n) if it existed
179 unsafe {
180 ptr::copy(self.buf.as_ptr().offset(written as isize),
181 self.buf.as_mut_ptr(),
182 len - written);
183 }
184 }
185 self.buf.truncate(len - written);
186 ret
187 }
188
189 /// Gets a reference to the underlying writer.
190 #[stable(feature = "rust1", since = "1.0.0")]
191 pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
192
193 /// Gets a mutable reference to the underlying write.
194 ///
195 /// # Warning
196 ///
197 /// It is inadvisable to directly read from the underlying writer.
198 #[stable(feature = "rust1", since = "1.0.0")]
199 pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
200
201 /// Unwraps this `BufWriter`, returning the underlying writer.
202 ///
203 /// The buffer is written out before returning the writer.
204 #[stable(feature = "rust1", since = "1.0.0")]
205 pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
206 match self.flush_buf() {
207 Err(e) => Err(IntoInnerError(self, e)),
208 Ok(()) => Ok(self.inner.take().unwrap())
209 }
210 }
211 }
212
213 #[stable(feature = "rust1", since = "1.0.0")]
214 impl<W: Write> Write for BufWriter<W> {
215 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
216 if self.buf.len() + buf.len() > self.buf.capacity() {
217 try!(self.flush_buf());
218 }
219 if buf.len() >= self.buf.capacity() {
220 self.inner.as_mut().unwrap().write(buf)
221 } else {
222 let amt = cmp::min(buf.len(), self.buf.capacity());
223 Write::write(&mut self.buf, &buf[..amt])
224 }
225 }
226 fn flush(&mut self) -> io::Result<()> {
227 self.flush_buf().and_then(|()| self.get_mut().flush())
228 }
229 }
230
231 #[stable(feature = "rust1", since = "1.0.0")]
232 impl<W: Write> fmt::Debug for BufWriter<W> where W: fmt::Debug {
233 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
234 fmt.debug_struct("BufWriter")
235 .field("writer", &self.inner.as_ref().unwrap())
236 .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
237 .finish()
238 }
239 }
240
241 #[unsafe_destructor]
242 impl<W: Write> Drop for BufWriter<W> {
243 fn drop(&mut self) {
244 if self.inner.is_some() {
245 // dtors should not panic, so we ignore a failed flush
246 let _r = self.flush_buf();
247 }
248 }
249 }
250
251 impl<W> IntoInnerError<W> {
252 /// Returns the error which caused the call to `into_inner` to fail.
253 ///
254 /// This error was returned when attempting to write the internal buffer.
255 #[stable(feature = "rust1", since = "1.0.0")]
256 pub fn error(&self) -> &Error { &self.1 }
257
258 /// Returns the buffered writer instance which generated the error.
259 ///
260 /// The returned object can be used for error recovery, such as
261 /// re-inspecting the buffer.
262 #[stable(feature = "rust1", since = "1.0.0")]
263 pub fn into_inner(self) -> W { self.0 }
264 }
265
266 #[stable(feature = "rust1", since = "1.0.0")]
267 impl<W> From<IntoInnerError<W>> for Error {
268 fn from(iie: IntoInnerError<W>) -> Error { iie.1 }
269 }
270
271 #[stable(feature = "rust1", since = "1.0.0")]
272 impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
273 fn description(&self) -> &str {
274 error::Error::description(self.error())
275 }
276 }
277
278 #[stable(feature = "rust1", since = "1.0.0")]
279 impl<W> fmt::Display for IntoInnerError<W> {
280 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
281 self.error().fmt(f)
282 }
283 }
284
285 /// Wraps a Writer and buffers output to it, flushing whenever a newline
286 /// (`0x0a`, `'\n'`) is detected.
287 ///
288 /// The buffer will be written out when the writer is dropped.
289 #[stable(feature = "rust1", since = "1.0.0")]
290 pub struct LineWriter<W: Write> {
291 inner: BufWriter<W>,
292 }
293
294 impl<W: Write> LineWriter<W> {
295 /// Creates a new `LineWriter`
296 #[stable(feature = "rust1", since = "1.0.0")]
297 pub fn new(inner: W) -> LineWriter<W> {
298 // Lines typically aren't that long, don't use a giant buffer
299 LineWriter::with_capacity(1024, inner)
300 }
301
302 /// Creates a new `LineWriter` with a specified capacity for the internal
303 /// buffer.
304 #[stable(feature = "rust1", since = "1.0.0")]
305 pub fn with_capacity(cap: usize, inner: W) -> LineWriter<W> {
306 LineWriter { inner: BufWriter::with_capacity(cap, inner) }
307 }
308
309 /// Gets a reference to the underlying writer.
310 #[stable(feature = "rust1", since = "1.0.0")]
311 pub fn get_ref(&self) -> &W { self.inner.get_ref() }
312
313 /// Gets a mutable reference to the underlying writer.
314 ///
315 /// Caution must be taken when calling methods on the mutable reference
316 /// returned as extra writes could corrupt the output stream.
317 #[stable(feature = "rust1", since = "1.0.0")]
318 pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
319
320 /// Unwraps this `LineWriter`, returning the underlying writer.
321 ///
322 /// The internal buffer is written out before returning the writer.
323 #[stable(feature = "rust1", since = "1.0.0")]
324 pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
325 self.inner.into_inner().map_err(|IntoInnerError(buf, e)| {
326 IntoInnerError(LineWriter { inner: buf }, e)
327 })
328 }
329 }
330
331 #[stable(feature = "rust1", since = "1.0.0")]
332 impl<W: Write> Write for LineWriter<W> {
333 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
334 match buf.rposition_elem(&b'\n') {
335 Some(i) => {
336 let n = try!(self.inner.write(&buf[..i + 1]));
337 if n != i + 1 { return Ok(n) }
338 try!(self.inner.flush());
339 self.inner.write(&buf[i + 1..]).map(|i| n + i)
340 }
341 None => self.inner.write(buf),
342 }
343 }
344
345 fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
346 }
347
348 #[stable(feature = "rust1", since = "1.0.0")]
349 impl<W: Write> fmt::Debug for LineWriter<W> where W: fmt::Debug {
350 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
351 fmt.debug_struct("LineWriter")
352 .field("writer", &self.inner.inner)
353 .field("buffer",
354 &format_args!("{}/{}", self.inner.buf.len(), self.inner.buf.capacity()))
355 .finish()
356 }
357 }
358
359 struct InternalBufWriter<W: Write>(BufWriter<W>);
360
361 impl<W: Read + Write> InternalBufWriter<W> {
362 fn get_mut(&mut self) -> &mut BufWriter<W> {
363 let InternalBufWriter(ref mut w) = *self;
364 return w;
365 }
366 }
367
368 impl<W: Read + Write> Read for InternalBufWriter<W> {
369 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
370 self.get_mut().inner.as_mut().unwrap().read(buf)
371 }
372 }
373
374 /// Wraps a Stream and buffers input and output to and from it.
375 ///
376 /// It can be excessively inefficient to work directly with a `Read+Write`. For
377 /// example, every call to `read` or `write` on `TcpStream` results in a system
378 /// call. A `BufStream` keeps in memory buffers of data, making large,
379 /// infrequent calls to `read` and `write` on the underlying `Read+Write`.
380 ///
381 /// The output buffer will be written out when this stream is dropped.
382 #[stable(feature = "rust1", since = "1.0.0")]
383 pub struct BufStream<S: Write> {
384 inner: BufReader<InternalBufWriter<S>>
385 }
386
387 impl<S: Read + Write> BufStream<S> {
388 /// Creates a new buffered stream with explicitly listed capacities for the
389 /// reader/writer buffer.
390 #[stable(feature = "rust1", since = "1.0.0")]
391 pub fn with_capacities(reader_cap: usize, writer_cap: usize, inner: S)
392 -> BufStream<S> {
393 let writer = BufWriter::with_capacity(writer_cap, inner);
394 let internal_writer = InternalBufWriter(writer);
395 let reader = BufReader::with_capacity(reader_cap, internal_writer);
396 BufStream { inner: reader }
397 }
398
399 /// Creates a new buffered stream with the default reader/writer buffer
400 /// capacities.
401 #[stable(feature = "rust1", since = "1.0.0")]
402 pub fn new(inner: S) -> BufStream<S> {
403 BufStream::with_capacities(DEFAULT_BUF_SIZE, DEFAULT_BUF_SIZE, inner)
404 }
405
406 /// Gets a reference to the underlying stream.
407 #[stable(feature = "rust1", since = "1.0.0")]
408 pub fn get_ref(&self) -> &S {
409 let InternalBufWriter(ref w) = self.inner.inner;
410 w.get_ref()
411 }
412
413 /// Gets a mutable reference to the underlying stream.
414 ///
415 /// # Warning
416 ///
417 /// It is inadvisable to read directly from or write directly to the
418 /// underlying stream.
419 #[stable(feature = "rust1", since = "1.0.0")]
420 pub fn get_mut(&mut self) -> &mut S {
421 let InternalBufWriter(ref mut w) = self.inner.inner;
422 w.get_mut()
423 }
424
425 /// Unwraps this `BufStream`, returning the underlying stream.
426 ///
427 /// The internal write buffer is written out before returning the stream.
428 /// Any leftover data in the read buffer is lost.
429 #[stable(feature = "rust1", since = "1.0.0")]
430 pub fn into_inner(self) -> Result<S, IntoInnerError<BufStream<S>>> {
431 let BufReader { inner: InternalBufWriter(w), buf, pos, cap } = self.inner;
432 w.into_inner().map_err(|IntoInnerError(w, e)| {
433 IntoInnerError(BufStream {
434 inner: BufReader { inner: InternalBufWriter(w), buf: buf, pos: pos, cap: cap },
435 }, e)
436 })
437 }
438 }
439
440 #[stable(feature = "rust1", since = "1.0.0")]
441 impl<S: Read + Write> BufRead for BufStream<S> {
442 fn fill_buf(&mut self) -> io::Result<&[u8]> { self.inner.fill_buf() }
443 fn consume(&mut self, amt: usize) { self.inner.consume(amt) }
444 }
445
446 #[stable(feature = "rust1", since = "1.0.0")]
447 impl<S: Read + Write> Read for BufStream<S> {
448 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
449 self.inner.read(buf)
450 }
451 }
452
453 #[stable(feature = "rust1", since = "1.0.0")]
454 impl<S: Read + Write> Write for BufStream<S> {
455 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
456 self.inner.inner.get_mut().write(buf)
457 }
458 fn flush(&mut self) -> io::Result<()> {
459 self.inner.inner.get_mut().flush()
460 }
461 }
462
463 #[stable(feature = "rust1", since = "1.0.0")]
464 impl<S: Write> fmt::Debug for BufStream<S> where S: fmt::Debug {
465 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
466 let reader = &self.inner;
467 let writer = &self.inner.inner.0;
468 fmt.debug_struct("BufStream")
469 .field("stream", &writer.inner)
470 .field("write_buffer", &format_args!("{}/{}", writer.buf.len(), writer.buf.capacity()))
471 .field("read_buffer",
472 &format_args!("{}/{}", reader.cap - reader.pos, reader.buf.len()))
473 .finish()
474 }
475 }
476
477 #[cfg(test)]
478 mod tests {
479 use prelude::v1::*;
480 use io::prelude::*;
481 use io::{self, BufReader, BufWriter, BufStream, Cursor, LineWriter};
482 use test;
483
484 /// A dummy reader intended at testing short-reads propagation.
485 pub struct ShortReader {
486 lengths: Vec<usize>,
487 }
488
489 impl Read for ShortReader {
490 fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
491 if self.lengths.is_empty() {
492 Ok(0)
493 } else {
494 Ok(self.lengths.remove(0))
495 }
496 }
497 }
498
499 #[test]
500 fn test_buffered_reader() {
501 let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
502 let mut reader = BufReader::with_capacity(2, inner);
503
504 let mut buf = [0, 0, 0];
505 let nread = reader.read(&mut buf);
506 assert_eq!(nread.unwrap(), 3);
507 let b: &[_] = &[5, 6, 7];
508 assert_eq!(buf, b);
509
510 let mut buf = [0, 0];
511 let nread = reader.read(&mut buf);
512 assert_eq!(nread.unwrap(), 2);
513 let b: &[_] = &[0, 1];
514 assert_eq!(buf, b);
515
516 let mut buf = [0];
517 let nread = reader.read(&mut buf);
518 assert_eq!(nread.unwrap(), 1);
519 let b: &[_] = &[2];
520 assert_eq!(buf, b);
521
522 let mut buf = [0, 0, 0];
523 let nread = reader.read(&mut buf);
524 assert_eq!(nread.unwrap(), 1);
525 let b: &[_] = &[3, 0, 0];
526 assert_eq!(buf, b);
527
528 let nread = reader.read(&mut buf);
529 assert_eq!(nread.unwrap(), 1);
530 let b: &[_] = &[4, 0, 0];
531 assert_eq!(buf, b);
532
533 assert_eq!(reader.read(&mut buf).unwrap(), 0);
534 }
535
536 #[test]
537 fn test_buffered_writer() {
538 let inner = Vec::new();
539 let mut writer = BufWriter::with_capacity(2, inner);
540
541 writer.write(&[0, 1]).unwrap();
542 assert_eq!(*writer.get_ref(), [0, 1]);
543
544 writer.write(&[2]).unwrap();
545 assert_eq!(*writer.get_ref(), [0, 1]);
546
547 writer.write(&[3]).unwrap();
548 assert_eq!(*writer.get_ref(), [0, 1]);
549
550 writer.flush().unwrap();
551 assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
552
553 writer.write(&[4]).unwrap();
554 writer.write(&[5]).unwrap();
555 assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
556
557 writer.write(&[6]).unwrap();
558 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]);
559
560 writer.write(&[7, 8]).unwrap();
561 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]);
562
563 writer.write(&[9, 10, 11]).unwrap();
564 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
565
566 writer.flush().unwrap();
567 assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
568 }
569
570 #[test]
571 fn test_buffered_writer_inner_flushes() {
572 let mut w = BufWriter::with_capacity(3, Vec::new());
573 w.write(&[0, 1]).unwrap();
574 assert_eq!(*w.get_ref(), []);
575 let w = w.into_inner().unwrap();
576 assert_eq!(w, [0, 1]);
577 }
578
579 // This is just here to make sure that we don't infinite loop in the
580 // newtype struct autoderef weirdness
581 #[test]
582 fn test_buffered_stream() {
583 struct S;
584
585 impl Write for S {
586 fn write(&mut self, b: &[u8]) -> io::Result<usize> { Ok(b.len()) }
587 fn flush(&mut self) -> io::Result<()> { Ok(()) }
588 }
589
590 impl Read for S {
591 fn read(&mut self, _: &mut [u8]) -> io::Result<usize> { Ok(0) }
592 }
593
594 let mut stream = BufStream::new(S);
595 assert_eq!(stream.read(&mut [0; 10]).unwrap(), 0);
596 stream.write(&[0; 10]).unwrap();
597 stream.flush().unwrap();
598 }
599
600 #[test]
601 fn test_read_until() {
602 let inner: &[u8] = &[0, 1, 2, 1, 0];
603 let mut reader = BufReader::with_capacity(2, inner);
604 let mut v = Vec::new();
605 reader.read_until(0, &mut v).unwrap();
606 assert_eq!(v, [0]);
607 v.truncate(0);
608 reader.read_until(2, &mut v).unwrap();
609 assert_eq!(v, [1, 2]);
610 v.truncate(0);
611 reader.read_until(1, &mut v).unwrap();
612 assert_eq!(v, [1]);
613 v.truncate(0);
614 reader.read_until(8, &mut v).unwrap();
615 assert_eq!(v, [0]);
616 v.truncate(0);
617 reader.read_until(9, &mut v).unwrap();
618 assert_eq!(v, []);
619 }
620
621 #[test]
622 fn test_line_buffer() {
623 let mut writer = LineWriter::new(Vec::new());
624 writer.write(&[0]).unwrap();
625 assert_eq!(*writer.get_ref(), []);
626 writer.write(&[1]).unwrap();
627 assert_eq!(*writer.get_ref(), []);
628 writer.flush().unwrap();
629 assert_eq!(*writer.get_ref(), [0, 1]);
630 writer.write(&[0, b'\n', 1, b'\n', 2]).unwrap();
631 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);
632 writer.flush().unwrap();
633 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);
634 writer.write(&[3, b'\n']).unwrap();
635 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
636 }
637
638 #[test]
639 fn test_read_line() {
640 let in_buf: &[u8] = b"a\nb\nc";
641 let mut reader = BufReader::with_capacity(2, in_buf);
642 let mut s = String::new();
643 reader.read_line(&mut s).unwrap();
644 assert_eq!(s, "a\n");
645 s.truncate(0);
646 reader.read_line(&mut s).unwrap();
647 assert_eq!(s, "b\n");
648 s.truncate(0);
649 reader.read_line(&mut s).unwrap();
650 assert_eq!(s, "c");
651 s.truncate(0);
652 reader.read_line(&mut s).unwrap();
653 assert_eq!(s, "");
654 }
655
656 #[test]
657 fn test_lines() {
658 let in_buf: &[u8] = b"a\nb\nc";
659 let reader = BufReader::with_capacity(2, in_buf);
660 let mut it = reader.lines();
661 assert_eq!(it.next().unwrap().unwrap(), "a".to_string());
662 assert_eq!(it.next().unwrap().unwrap(), "b".to_string());
663 assert_eq!(it.next().unwrap().unwrap(), "c".to_string());
664 assert!(it.next().is_none());
665 }
666
667 #[test]
668 fn test_short_reads() {
669 let inner = ShortReader{lengths: vec![0, 1, 2, 0, 1, 0]};
670 let mut reader = BufReader::new(inner);
671 let mut buf = [0, 0];
672 assert_eq!(reader.read(&mut buf).unwrap(), 0);
673 assert_eq!(reader.read(&mut buf).unwrap(), 1);
674 assert_eq!(reader.read(&mut buf).unwrap(), 2);
675 assert_eq!(reader.read(&mut buf).unwrap(), 0);
676 assert_eq!(reader.read(&mut buf).unwrap(), 1);
677 assert_eq!(reader.read(&mut buf).unwrap(), 0);
678 assert_eq!(reader.read(&mut buf).unwrap(), 0);
679 }
680
681 #[test]
682 fn read_char_buffered() {
683 let buf = [195, 159];
684 let reader = BufReader::with_capacity(1, &buf[..]);
685 assert_eq!(reader.chars().next().unwrap().unwrap(), 'ß');
686 }
687
688 #[test]
689 fn test_chars() {
690 let buf = [195, 159, b'a'];
691 let reader = BufReader::with_capacity(1, &buf[..]);
692 let mut it = reader.chars();
693 assert_eq!(it.next().unwrap().unwrap(), 'ß');
694 assert_eq!(it.next().unwrap().unwrap(), 'a');
695 assert!(it.next().is_none());
696 }
697
698 #[test]
699 #[should_panic]
700 fn dont_panic_in_drop_on_panicked_flush() {
701 struct FailFlushWriter;
702
703 impl Write for FailFlushWriter {
704 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
705 fn flush(&mut self) -> io::Result<()> {
706 Err(io::Error::last_os_error())
707 }
708 }
709
710 let writer = FailFlushWriter;
711 let _writer = BufWriter::new(writer);
712
713 // If writer panics *again* due to the flush error then the process will
714 // abort.
715 panic!();
716 }
717
718 #[bench]
719 fn bench_buffered_reader(b: &mut test::Bencher) {
720 b.iter(|| {
721 BufReader::new(io::empty())
722 });
723 }
724
725 #[bench]
726 fn bench_buffered_writer(b: &mut test::Bencher) {
727 b.iter(|| {
728 BufWriter::new(io::sink())
729 });
730 }
731
732 #[bench]
733 fn bench_buffered_stream(b: &mut test::Bencher) {
734 let mut buf = Cursor::new(Vec::new());
735 b.iter(|| {
736 BufStream::new(&mut buf);
737 });
738 }
739 }