Skip to main content

BufferedByteInput

Struct BufferedByteInput 

Source
pub struct BufferedByteInput<R> { /* private fields */ }
Expand description

Buffered byte input over a wrapped reader.

This type owns a wrapped input object and an internal byte buffer. It keeps unread bytes in buffer[position..limit] so callers can inspect or consume the current byte window before refilling it.

BufferedByteInput is deliberately byte-oriented. It performs no binary decoding, text decoding, or record parsing; higher-level stream adapters can build those concerns on top of Self::unread_slice, Self::unread_raw_parts, Self::ensure_available, and Self::read_into_unchecked. The type also implements BufRead for callers that want the standard buffered-read interface.

Implementations§

Source§

impl<R> BufferedByteInput<R>

Source

pub fn new(inner: R) -> Self

Creates a buffered byte input with the default capacity.

§Arguments
  • inner - The input object wrapped by this buffer.
§Returns

A new buffered byte input whose internal buffer has at least DEFAULT_BUFFER_CAPACITY bytes.

Source

pub fn with_capacity(inner: R, capacity: usize) -> Self

Creates a buffered byte input with at least the requested capacity.

The actual capacity is raised to 1 when the requested value is 0.

§Arguments
  • inner - The input object wrapped by this buffer.
  • capacity - The requested internal buffer capacity, in bytes.
§Returns

A new buffered byte input whose internal buffer capacity is capacity.max(1).

Source

pub const fn inner(&self) -> &R

Returns a shared reference to the wrapped input object.

§Returns

A shared reference to the inner input object.

Source

pub fn inner_mut(&mut self) -> &mut R

Returns an exclusive reference to the wrapped input object.

Mutating the wrapped object directly may invalidate assumptions about bytes already buffered by this value.

§Returns

An exclusive reference to the wrapped input object.

Source

pub fn into_parts(self) -> (R, Vec<u8>)

Consumes this buffered input and returns the wrapped input object plus unread bytes.

This method performs no I/O. Bytes that have already been read from the wrapped input but not consumed by this buffered input are returned as the second tuple item.

§Returns

The wrapped input object and a vector containing the unread buffered bytes in logical read order.

Source

pub fn capacity(&self) -> usize

Returns the internal buffer capacity.

§Returns

The total number of bytes that can be held by the internal buffer.

Source

pub fn available(&self) -> usize

Returns the number of unread bytes currently buffered.

§Returns

The length of buffer[position..limit], in bytes.

Source

pub fn unread_slice(&self) -> &[u8]

Returns the currently buffered unread bytes.

§Returns

The unread range buffer[position..limit].

Source

pub fn unread_raw_parts(&self) -> (&[u8], usize, usize)

Returns raw unread-buffer parts for hot-path callers.

The returned slice is the full internal backing storage. index is the start of the unread byte window, and count is the number of unread bytes. Callers that need a slice can use &buffer[index..index + count]; callers that already validated bounds can pass buffer and index directly to indexed unchecked codecs.

§Returns

The backing storage, the unread start index, and the unread byte count.

Source

pub fn consume(&mut self, count: usize)

Advances the unread cursor by count bytes.

§Parameters
  • count - Number of currently unread bytes to consume.
§Panics

Panics when count exceeds Self::available.

Source

pub unsafe fn consume_unchecked(&mut self, count: usize)

Advances the unread cursor without checking bounds.

§Parameters
  • count - Number of currently unread bytes to consume.
§Safety

The caller must guarantee that count <= self.available().

Source§

impl<R> BufferedByteInput<R>
where R: Read,

Source

pub fn fill_more(&mut self) -> Result<bool>

Refills the internal buffer after preserving unread bytes.

Consumed bytes may be discarded, and unread bytes may be moved to the front of the buffer before the wrapped reader is called.

§Returns

Ok(true) if at least one byte was appended, or Ok(false) at EOF.

§Errors

Returns any non-interrupted I/O error produced by the wrapped reader.

Source

pub fn fill_until(&mut self, count: usize) -> Result<bool>

Refills the buffer until at least count unread bytes are available.

This method may discard consumed bytes or move unread bytes to the front of the buffer before reading more data. It stops as soon as the unread window reaches count bytes or the wrapped reader reaches EOF.

§Parameters
  • count - Minimum number of unread bytes required.
§Returns

Ok(true) if at least count unread bytes are buffered. Ok(false) means EOF was reached before the requested byte count became available.

§Errors

Returns ErrorKind::InvalidInput when count exceeds the internal buffer capacity. Returns ErrorKind::InvalidData if the wrapped reader reports more bytes than the spare buffer range could hold. Returns any non-interrupted I/O error produced by the wrapped reader while refilling the buffer.

Source

pub fn ensure_available(&mut self, count: usize) -> Result<()>

Ensures that at least count unread bytes are available.

Unlike Self::fill_until, this method treats EOF before the requested byte count as ErrorKind::UnexpectedEof. Any partial bytes buffered before EOF are consumed so callers observe the same logical position as a failed exact read.

§Parameters
  • count - Minimum number of unread bytes required.
§Errors

Returns ErrorKind::UnexpectedEof if EOF is reached before count bytes are available. Returns ErrorKind::InvalidInput when count exceeds the internal buffer capacity. Returns ErrorKind::InvalidData if the wrapped reader reports more bytes than the spare buffer range could hold. Returns any non-interrupted I/O error produced by the wrapped reader while refilling the buffer.

Source

pub unsafe fn read_into_unchecked( &mut self, output: &mut [u8], output_index: usize, count: usize, ) -> Result<usize>

Reads bytes through the internal buffer into an indexed output range.

If the internal buffer is empty and count is at least as large as the internal buffer capacity, the read is delegated directly to the wrapped reader to avoid an unnecessary copy. Otherwise, bytes are served from the internal buffer.

§Arguments
  • output - Destination storage that receives bytes.
  • output_index - Start index inside output.
  • count - Maximum number of bytes to read.
§Returns

The number of bytes written into output[output_index..output_index + count]. A return value of 0 means that count was zero or EOF was reached before any bytes were read.

§Errors

Returns any I/O error produced by the wrapped reader. Returns ErrorKind::InvalidData if the wrapped reader reports more bytes than the requested destination range could hold. Interrupted reads are retried when the method refills the internal buffer through read_more; direct delegated reads follow the wrapped reader’s own Read::read behavior.

§Safety

The caller must guarantee that output_index..output_index + count is a valid range inside output and that the addition does not overflow.

Trait Implementations§

Source§

impl<R> BufRead for BufferedByteInput<R>
where R: Read,

Source§

fn fill_buf(&mut self) -> Result<&[u8]>

Returns the currently buffered unread bytes, refilling when empty.

Source§

fn consume(&mut self, amount: usize)

Consumes amount bytes from the unread byte window.

Source§

fn has_data_left(&mut self) -> Result<bool, Error>

🔬This is a nightly-only experimental API. (buf_read_has_data_left)
Checks if there is any data left to be read. Read more
1.0.0 · Source§

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
1.83.0 · Source§

fn skip_until(&mut self, byte: u8) -> Result<usize, Error>

Skips all bytes until the delimiter byte or EOF is reached. Read more
1.0.0 · Source§

fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until a newline (the 0xA byte) is reached, and append them to the provided String buffer. Read more
1.0.0 · Source§

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Returns an iterator over the contents of this reader split on the byte byte. Read more
1.0.0 · Source§

fn lines(self) -> Lines<Self>
where Self: Sized,

Returns an iterator over the lines of this reader. Read more
Source§

impl<R: Debug> Debug for BufferedByteInput<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R> Read for BufferedByteInput<R>
where R: Read,

Source§

fn read(&mut self, output: &mut [u8]) -> Result<usize>

Reads bytes through the internal buffer.

§Arguments
  • output - Destination slice that receives the bytes read.
§Returns

The number of bytes written to output.

§Errors

Returns any I/O error produced by the wrapped reader.

1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

impl<R> Seek for BufferedByteInput<R>
where R: Read + Seek,

Source§

fn seek(&mut self, position: SeekFrom) -> Result<u64>

Seeks the wrapped reader and discards buffered bytes after success.

1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more

Auto Trait Implementations§

§

impl<R> Freeze for BufferedByteInput<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for BufferedByteInput<R>
where R: RefUnwindSafe,

§

impl<R> Send for BufferedByteInput<R>
where R: Send,

§

impl<R> Sync for BufferedByteInput<R>
where R: Sync,

§

impl<R> Unpin for BufferedByteInput<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for BufferedByteInput<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for BufferedByteInput<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> BufReadExt for T
where T: BufRead + ?Sized,

Source§

fn read_until_limited( &mut self, delimiter: u8, max_len: usize, ) -> Result<Vec<u8>, Error>

Reads bytes through delimiter while enforcing max_len. Read more
Source§

fn read_until_limited_into( &mut self, delimiter: u8, output: &mut Vec<u8>, max_len: usize, ) -> Result<usize, Error>

Reads bytes through delimiter into output while enforcing max_len. Read more
Source§

fn read_line_limited(&mut self, max_len: usize) -> Result<String, Error>

Reads one UTF-8 line while enforcing max_len. Read more
Source§

fn read_line_limited_into( &mut self, output: &mut String, max_len: usize, ) -> Result<usize, Error>

Reads one UTF-8 line into output while enforcing max_len. Read more
Source§

fn discard_until_limited( &mut self, delimiter: u8, max_len: usize, ) -> Result<usize, Error>

Discards bytes through delimiter while enforcing max_len. Read more
Source§

impl<T> BufReadSeek for T
where T: BufRead + Seek + ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ReadExt for T
where T: Read + ?Sized,

Source§

unsafe fn read_unchecked( &mut self, buffer: &mut [u8], start_index: usize, count: usize, ) -> Result<usize, Error>

Reads bytes into a range of buffer without checking the range bounds in release builds. Read more
Source§

unsafe fn read_exact_or_eof_unchecked( &mut self, buffer: &mut [u8], start_index: usize, count: usize, ) -> Result<usize, Error>

Reads bytes into a range of buffer until that range is full or EOF is reached, without checking the range bounds in release builds. Read more
Source§

unsafe fn read_exact_unchecked( &mut self, buffer: &mut [u8], start_index: usize, count: usize, ) -> Result<(), Error>

Reads exactly count bytes into a range of buffer without checking the range bounds in release builds. Read more
Source§

fn read_exact_or_eof(&mut self, buffer: &mut [u8]) -> Result<usize, Error>

Reads bytes until buffer is full or EOF is reached. Read more
Source§

fn read_exact_array<const N: usize>(&mut self) -> Result<[u8; N], Error>

Reads exactly N bytes into a stack-allocated array. Read more
Source§

fn read_exact_vec_limited( &mut self, len: usize, max_len: usize, ) -> Result<Vec<u8>, Error>

Reads exactly len bytes into a new vector after checking a limit. Read more
Source§

fn read_exact_vec_limited_into( &mut self, output: &mut Vec<u8>, len: usize, max_len: usize, ) -> Result<(), Error>

Reads exactly len bytes and appends them to output. Read more
Source§

fn discard_exact_or_eof(&mut self, bytes: u64) -> Result<u64, Error>

Discards up to bytes bytes from this reader. Read more
Source§

fn copy_to(&mut self, writer: &mut dyn Write) -> Result<u64, Error>

Copies all remaining bytes from this reader into writer. Read more
Source§

fn copy_to_at_most( &mut self, writer: &mut dyn Write, max_bytes: u64, ) -> Result<u64, Error>

Copies at most max_bytes bytes from this reader into writer. Read more
Source§

fn copy_to_end_limited( &mut self, writer: &mut dyn Write, max_bytes: u64, ) -> Result<u64, Error>

Copies the remaining input if its total length is at most max_bytes. Read more
Source§

fn read_to_end_limited(&mut self, max_len: usize) -> Result<Vec<u8>, Error>

Reads the remaining bytes into a vector with a maximum accepted length. Read more
Source§

fn read_to_end_limited_into( &mut self, output: &mut Vec<u8>, max_len: usize, ) -> Result<usize, Error>

Reads the remaining bytes into output with a maximum accepted length. Read more
Source§

fn read_to_string_limited(&mut self, max_len: usize) -> Result<String, Error>

Reads the remaining bytes as UTF-8 text with a maximum accepted length. Read more
Source§

fn read_to_string_limited_into( &mut self, output: &mut String, max_len: usize, ) -> Result<usize, Error>

Reads the remaining bytes as UTF-8 text and appends to output. Read more
Source§

impl<T> ReadSeek for T
where T: Read + Seek + ?Sized,

Source§

impl<T> ReadSeekExt for T
where T: Read + Seek + ?Sized,

Source§

fn peek_exact_or_eof(&mut self, buffer: &mut [u8]) -> Result<usize, Error>

Reads from the current position and restores the original position. Read more
Source§

fn read_exact_or_eof_at( &mut self, offset: u64, buffer: &mut [u8], ) -> Result<usize, Error>

Reads from offset and restores the original position. Read more
Source§

impl<T> SeekExt for T
where T: Seek + ?Sized,

Source§

fn stream_size(&mut self) -> Result<u64, Error>

Gets the stream size without changing the final stream position. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.