ZstdReader

Struct ZstdReader 

Source
pub struct ZstdReader<'dict, R> { /* private fields */ }
Expand description

A reader that decompresses a zstd stream from an underlying reader.

The underyling reader R should implement the following traits:

For async support, see crate::AsyncZstdReader.

§Construction

Create a builder using either ZstdReader::builder (recommended) or ZstdReader::builder_buffered (to use a custom buffer). See ZstdReaderBuilder for build options. Call ZstdReaderBuilder::build to build the ZstdReader instance.

let reader = zstd_framed::ZstdReader::builder(compressed_file)
    // .with_seek_table(table) // Provide a seek table if available
    .build()?;

§Buffering

The decompressed zstd output is always buffered internally. Since the reader must also implement std::io::BufRead, the compressed input must also be buffered.

ZstdReader::builder will wrap any reader implmenting std::io::Read with a recommended buffer size for the input stream. For more control over how the input gets buffered, you can instead use ZstdReader::builder_buffered.

§Seeking

ZstdReader implements std::io::Seek as long as the underlying reader implements std::io::Seek. By default, seeking within the stream will linearly decompress until reaching the target!

Seeking can do a lot better when the underlying stream is broken up into multiple frames, such as a stream that uses the zstd seekable format. You can create such a stream using ZstdWriterBuilder::with_seek_table.

There are two situations where seeking can take advantage of a seek table:

  1. When a seek table is provided up-front using ZstdReaderBuilder::with_seek_table. See crate::table::read_seek_table for reading a seek table from a reader.
  2. When rewinding to a previously-decompressed frame. Frame offsets are automatically recorded during decompression.

Even if a seek table is used, seeking will still need to rewind to the start of a frame, then decompress until reaching the target offset.

Implementations§

Source§

impl<R> ZstdReader<'_, BufReader<R>>

Source

pub fn builder(reader: R) -> ZstdReaderBuilder<BufReader<R>>
where R: Read,

Create a new zstd reader that decompresses the zstd stream from the underlying reader. The provided reader will be wrapped with an appropriately-sized buffer.

Source

pub fn builder_buffered(reader: R) -> ZstdReaderBuilder<R>

Create a new zstd reader that decompresses the zstd stream from the underlying reader. The underlying reader must implement std::io::BufRead, and its buffer will be used directly. When in doubt, use ZstdReader::builder, which uses an appropriate buffer size for decompressing a zstd stream.

Trait Implementations§

Source§

impl<R> BufRead for ZstdReader<'_, R>
where R: BufRead,

Source§

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

Returns the contents of the internal buffer, filling it with more data, via Read methods, if empty. Read more
Source§

fn consume(&mut self, amt: usize)

Marks the given amount of additional bytes from the internal buffer as having been read. Subsequent calls to read only return bytes that have not been marked as read. Read more
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> Read for ZstdReader<'_, R>
where R: BufRead,

Source§

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

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
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” adaptor 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§

impl<R> Seek for ZstdReader<'_, R>
where R: BufRead + Seek,

Source§

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

Seek to an offset, in bytes, in a stream. Read more
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<'dict, R> Freeze for ZstdReader<'dict, R>
where R: Freeze,

§

impl<'dict, R> RefUnwindSafe for ZstdReader<'dict, R>
where R: RefUnwindSafe,

§

impl<'dict, R> Send for ZstdReader<'dict, R>
where R: Send,

§

impl<'dict, R> Sync for ZstdReader<'dict, R>
where R: Sync,

§

impl<'dict, R> Unpin for ZstdReader<'dict, R>
where R: Unpin,

§

impl<'dict, R> !UnwindSafe for ZstdReader<'dict, R>

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> 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, 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.