Skip to main content

Decompressor

Struct Decompressor 

Source
pub struct Decompressor<R: Read> { /* private fields */ }
Expand description

A Read that decompresses from an inner source.

By default concatenated zstd/LZ4 frames, gzip members and xz streams decode as one stream, and the source must end on a frame boundary. A decompressor made with Decompressor::single_stream instead stops at the end of the first frame and hands back the bytes after it.

Implementations§

Source§

impl<R: Read> Decompressor<R>

Source

pub fn new(src: R, algo: Algorithm) -> Result<Self, Error>

Create a decompressor reading a stream produced by algo from src.

Source

pub fn single_stream(src: R, algo: Algorithm) -> Result<Self, Error>

Create a decompressor that stops at the end of the first frame (zstd or LZ4 frame, gzip member, zlib/DEFLATE/xz/brotli/bzip2 stream, or Snappy block) instead of continuing into whatever follows it, such as another message on the same connection.

read returns Ok(0) once that frame is fully decoded. It reads src in chunks, so it usually reads past the end: those bytes come back from unused or into_parts, so the caller can carry on reading the source:

use std::io::{Cursor, Read};
use compress_utils::{compress, Algorithm, Decompressor};

let mut wire = compress(Algorithm::Zstd, b"first", 5).unwrap();
wire.extend_from_slice(b"rest of the source");

let mut d = Decompressor::single_stream(Cursor::new(wire), Algorithm::Zstd).unwrap();
let mut first = Vec::new();
d.read_to_end(&mut first).unwrap();
assert_eq!(first, b"first");

let (src, unused) = d.into_parts().unwrap();
let mut rest = Vec::new();
Cursor::new(unused).chain(src).read_to_end(&mut rest).unwrap();
assert_eq!(rest, b"rest of the source");

Raw LZ4 and LZO have no end marker to stop at and return Status::UnsupportedAlgo. Single-mode Snappy produces no output until its whole block has arrived, and rejects a block declaring more than crate::set_max_decompressed_size (as set when the decompressor is created) with Status::SizeLimit.

Source

pub fn set_max_output(&mut self, bytes: usize)

Limit the total decompressed output to bytes (0, the default, means no limit). The read that would go past it returns the bytes up to the limit, then an error with Status::SizeLimit; the decompressor is done after that. Use it when decompressing untrusted input, which can expand to far more than its own size.

Source

pub fn is_eof(&self) -> bool

Single-stream mode: whether the end of the stream has been decoded. Output decoded before it may still be waiting to be read; read returns Ok(0) once it has been. Always false for decompressors made with Decompressor::new.

Source

pub fn unused(&self) -> &[u8] ⓘ

Single-stream mode: the bytes read from the source after the end of the stream, in order. Empty until is_eof, and always for decompressors made with Decompressor::new.

Source

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

Returns the source and the bytes already read from it after the end of the stream (unused). Reading the unused bytes, then the source, continues exactly where the compressed stream ended.

Only once the stream has ended and all of its output has been read (read has returned Ok(0)): before that, part of the stream and the bytes after it may still be held inside the decompressor, and taking the source would lose them. Otherwise this returns an IntoPartsError that hands the decompressor back, so you can keep reading. A caller that stops at the end of its own data (as pickle stops at its STOP opcode) should read to Ok(0) first.

Trait Implementations§

Source§

impl<R: Read> Drop for Decompressor<R>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<R: Read> Read for Decompressor<R>

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<'_, u8>) -> 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<'_, u8>, ) -> 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§

fn read_le<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in little-endian order. Read more
Source§

fn read_be<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in big-endian order. Read more

Auto Trait Implementations§

§

impl<R> !RefUnwindSafe for Decompressor<R>

§

impl<R> !Send for Decompressor<R>

§

impl<R> !Sync for Decompressor<R>

§

impl<R> !UnwindSafe for Decompressor<R>

§

impl<R> Freeze for Decompressor<R>
where Option<R>: Freeze,

§

impl<R> Unpin for Decompressor<R>
where Option<R>: Unpin,

§

impl<R> UnsafeUnpin for Decompressor<R>
where Option<R>: UnsafeUnpin,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.