pub struct BufReader<R> { /* private fields */ }
Expand description
A drop-in replacement for std::io::BufReader
with more functionality.
Original method names/signatures and implemented traits are left untouched, making replacement as simple as swapping the import of the type.
Implementations§
source§impl<R> BufReader<R>
impl<R> BufReader<R>
sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Create a new BufReader
wrapping inner
, with a buffer of a
default capacity.
sourcepub fn with_capacity(cap: usize, inner: R) -> Self
pub fn with_capacity(cap: usize, inner: R) -> Self
Create a new BufReader
wrapping inner
with a capacity
of at least cap
bytes.
The actual capacity of the buffer may vary based on implementation details of the buffer’s allocator.
sourcepub fn make_room(&mut self)
pub fn make_room(&mut self)
Move data to the start of the buffer, making room at the end for more reading.
sourcepub fn grow(&mut self, additional: usize)
pub fn grow(&mut self, additional: usize)
Grow the internal buffer by at least additional
bytes. May not be
quite exact due to implementation details of the buffer’s allocator.
##Note This should not be called frequently as each call will incur a reallocation and a zeroing of the new memory.
sourcepub fn get_buf(&self) -> &[u8] ⓘ
pub fn get_buf(&self) -> &[u8] ⓘ
Get the section of the buffer containing valid data; may be empty.
Call .consume()
to remove bytes from the beginning of this section.
sourcepub fn get_mut(&mut self) -> &mut R
pub fn get_mut(&mut self) -> &mut R
Get a mutable reference to the underlying reader.
§Note
Reading directly from the underlying reader is not recommended, as some data has likely already been moved into the buffer.
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consumes self
and returns the inner reader only.
sourcepub fn into_inner_with_buf(self) -> (R, Vec<u8>)
pub fn into_inner_with_buf(self) -> (R, Vec<u8>)
Consumes self
and returns both the underlying reader and the buffer,
with the data moved to the beginning and the length truncated to contain
only valid data.
See also: BufReader::unbuffer()
source§impl<R: Read> BufReader<R>
impl<R: Read> BufReader<R>
sourcepub fn read_into_buf(&mut self) -> Result<usize>
pub fn read_into_buf(&mut self) -> Result<usize>
Unconditionally perform a read into the buffer, calling .make_room()
if appropriate or necessary, as determined by the implementation.
If the read was successful, returns the number of bytes now available in the buffer.
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 has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read moresource§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
bufread_skip_until
)byte
or EOF is reached. Read more1.0.0 · source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moresource§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>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§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
.unwrap()
immediately after a seek yields the underlying reader at
the same position.
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 underflows 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 seeked to SeekFrom::Current(0)
.
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)