Struct std::io::BufferedWriter
pub struct BufferedWriter<W> { // some fields omitted }
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
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();
Methods
impl<W: Writer> BufferedWriter<W>
fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W>
Creates a new BufferedWriter
with the specified buffer capacity
fn new(inner: W) -> BufferedWriter<W>
Creates a new BufferedWriter
with a default buffer capacity
fn get_ref<'a>(&'a self) -> &'a W
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.
fn unwrap(self) -> W
Unwraps this BufferedWriter
, returning the underlying writer.
The buffer is flushed before returning the writer.
Trait Implementations
impl<W: Writer> Writer for BufferedWriter<W>
fn write(&mut self, buf: &[u8]) -> IoResult<()>
Write the entirety of a given buffer
Errors
If an error happens during the I/O operation, the error is returned as
Err
. Note that it is considered an error if the entire buffer could
not be written, and if an error is returned then it is unknown how much
data (if any) was actually written.
fn flush(&mut self) -> IoResult<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
This is by default a no-op and implementers of the Writer
trait should
decide whether their stream needs to be buffered or not.
fn write_str(&mut self, s: &str) -> IoResult<()>
Write a rust string into this sink.
The bytes written will be the UTF-8 encoded version of the input string.
If other encodings are desired, it is recommended to compose this stream
with another performing the conversion, or to use write
with a
converted byte-array instead.
fn write_line(&mut self, s: &str) -> IoResult<()>
Writes a string into this sink, and then writes a literal newline (\n
)
byte afterwards. Note that the writing of the newline is not atomic in
the sense that the call to write
is invoked twice (once with the
string and once with a newline character).
If other encodings or line ending flavors are desired, it is recommended
that the write
method is used specifically instead.
fn write_char(&mut self, c: char) -> IoResult<()>
Write a single char, encoded as UTF-8.
fn write_int(&mut self, n: int) -> IoResult<()>
Write the result of passing n through int::to_str_bytes
.
fn write_uint(&mut self, n: uint) -> IoResult<()>
Write the result of passing n through uint::to_str_bytes
.
fn write_le_uint(&mut self, n: uint) -> IoResult<()>
Write a little-endian uint (number of bytes depends on system).
fn write_le_int(&mut self, n: int) -> IoResult<()>
Write a little-endian int (number of bytes depends on system).
fn write_be_uint(&mut self, n: uint) -> IoResult<()>
Write a big-endian uint (number of bytes depends on system).
fn write_be_int(&mut self, n: int) -> IoResult<()>
Write a big-endian int (number of bytes depends on system).
fn write_be_u64(&mut self, n: u64) -> IoResult<()>
Write a big-endian u64 (8 bytes).
fn write_be_u32(&mut self, n: u32) -> IoResult<()>
Write a big-endian u32 (4 bytes).
fn write_be_u16(&mut self, n: u16) -> IoResult<()>
Write a big-endian u16 (2 bytes).
fn write_be_i64(&mut self, n: i64) -> IoResult<()>
Write a big-endian i64 (8 bytes).
fn write_be_i32(&mut self, n: i32) -> IoResult<()>
Write a big-endian i32 (4 bytes).
fn write_be_i16(&mut self, n: i16) -> IoResult<()>
Write a big-endian i16 (2 bytes).
fn write_be_f64(&mut self, f: f64) -> IoResult<()>
Write a big-endian IEEE754 double-precision floating-point (8 bytes).
fn write_be_f32(&mut self, f: f32) -> IoResult<()>
Write a big-endian IEEE754 single-precision floating-point (4 bytes).
fn write_le_u64(&mut self, n: u64) -> IoResult<()>
Write a little-endian u64 (8 bytes).
fn write_le_u32(&mut self, n: u32) -> IoResult<()>
Write a little-endian u32 (4 bytes).
fn write_le_u16(&mut self, n: u16) -> IoResult<()>
Write a little-endian u16 (2 bytes).
fn write_le_i64(&mut self, n: i64) -> IoResult<()>
Write a little-endian i64 (8 bytes).
fn write_le_i32(&mut self, n: i32) -> IoResult<()>
Write a little-endian i32 (4 bytes).
fn write_le_i16(&mut self, n: i16) -> IoResult<()>
Write a little-endian i16 (2 bytes).
fn write_le_f64(&mut self, f: f64) -> IoResult<()>
Write a little-endian IEEE754 double-precision floating-point (8 bytes).
fn write_le_f32(&mut self, f: f32) -> IoResult<()>
Write a little-endian IEEE754 single-precision floating-point (4 bytes).
fn write_u8(&mut self, n: u8) -> IoResult<()>
Write a u8 (1 byte).
fn write_i8(&mut self, n: i8) -> IoResult<()>
Write a i8 (1 byte).
fn by_ref<'a>(&'a mut self) -> RefWriter<'a, Self>
Creates a wrapper around a mutable reference to the writer.
This is useful to allow applying wrappers while still retaining ownership of the original value.
impl<W: Writer> Drop for BufferedWriter<W>
fn drop(&mut self)
The drop
method, called when the value goes out of scope.