pub struct BufReader<R> { /* private fields */ }Expand description
The BufReader struct adds buffering to any reader.
It can be excessively inefficient to work directly with a Read instance.
For example, every call to read on TcpStream
results in a system call. A BufReader performs large, infrequent reads on
the underlying Read and maintains an in-memory buffer of the results.
BufReader can improve the speed of programs that make small and
repeated read calls to the same file or network socket. It does not
help when reading very large amounts at once, or reading just one or a few
times. It also provides no advantage when reading from a source that is
already in memory, like a Vec<u8>.
§Examples
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
fn main() -> std::io::Result<()> {
let f = File::open("log.txt")?;
let mut reader = BufReader::new(f);
let mut line = String::new();
let len = reader.read_line(&mut line)?;
println!("First line is {} bytes long", len);
Ok(())
}Implementations§
Source§impl<R: Read> BufReader<R>
impl<R: Read> BufReader<R>
Sourcepub fn new(inner: R) -> BufReader<R>
pub fn new(inner: R) -> BufReader<R>
Creates a new BufReader with a default buffer capacity. The default is currently 8 KB,
but may change in the future.
§Examples
use std::io::BufReader;
use std::fs::File;
fn main() -> std::io::Result<()> {
let f = File::open("log.txt")?;
let reader = BufReader::new(f);
Ok(())
}Sourcepub fn with_capacity(cap: usize, inner: R) -> BufReader<R>
pub fn with_capacity(cap: usize, inner: R) -> BufReader<R>
Creates a new BufReader with the specified buffer capacity.
§Examples
Creating a buffer with ten bytes of capacity:
use std::io::BufReader;
use std::fs::File;
fn main() -> std::io::Result<()> {
let f = File::open("log.txt")?;
let reader = BufReader::with_capacity(10, f);
Ok(())
}Sourcepub fn get_ref(&self) -> &R
pub fn get_ref(&self) -> &R
Gets a reference to the underlying reader.
It is inadvisable to directly read from the underlying reader.
§Examples
use std::io::BufReader;
use std::fs::File;
fn main() -> std::io::Result<()> {
let f1 = File::open("log.txt")?;
let reader = BufReader::new(f1);
let f2 = reader.get_ref();
Ok(())
}Source§impl<R: Seek> BufReader<R>
impl<R: Seek> BufReader<R>
Sourcepub fn seek_relative(&mut self, offset: i64) -> Result<()>
pub fn seek_relative(&mut self, offset: i64) -> Result<()>
Seeks relative to the current position. If the new position lies within the buffer, the buffer will not be flushed, allowing for more efficient seeks. This method does not return the location of the underlying reader, so the caller must track this information themselves if it is required.
Trait Implementations§
Source§impl<R: Read> BufRead for BufReader<R>
impl<R: Read> BufRead for BufReader<R>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt bytes have been consumed from the buffer,
so they should no longer be returned in calls to read. Read moreSource§fn read_line(&mut self, buf: &mut String) -> Result<usize>
fn read_line(&mut self, buf: &mut String) -> Result<usize>
Source§impl<R: Read> Read for BufReader<R>
impl<R: Read> Read for BufReader<R>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Source§unsafe fn initializer(&self) -> Initializer
unsafe fn initializer(&self) -> Initializer
Reader can work with buffers of uninitialized
memory. Read morefn read_out(&mut self, out: Out<'_, [u8]>) -> Result<usize>
Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf. Read moreSource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf. Read moreSource§fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf. Read moreSource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read moreSource§impl<R: Seek> Seek for BufReader<R>
impl<R: Seek> Seek for BufReader<R>
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to an offset, in bytes, in the underlying reader.
The position used for seeking with SeekFrom::Current(_) is the
position the underlying reader would be at if the BufReader had no
internal buffer.
Seeking always discards the internal buffer, even if the seek position
would otherwise fall within it. This guarantees that calling
.into_inner() immediately after a seek yields the underlying reader
at the same position.
To seek without discarding the internal buffer, use seek_relative.
See std::io::Seek for more details.
Note: In the edge case where you’re seeking with SeekFrom::Current(n)
where n minus the internal buffer length overflows an i64, two
seeks will be performed instead of one. If the second seek returns
Err, the underlying reader will be left at the same position it would
have if you called seek with SeekFrom::Current(0).