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>
impl<R: Read> Decompressor<R>
Sourcepub fn new(src: R, algo: Algorithm) -> Result<Self, Error>
pub fn new(src: R, algo: Algorithm) -> Result<Self, Error>
Create a decompressor reading a stream produced by algo from src.
Sourcepub fn single_stream(src: R, algo: Algorithm) -> Result<Self, Error>
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.
Sourcepub fn set_max_output(&mut self, bytes: usize)
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.
Sourcepub fn is_eof(&self) -> bool
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.
Sourcepub fn unused(&self) -> &[u8] ⓘ
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.
Sourcepub fn into_parts(self) -> Result<(R, Vec<u8>), IntoPartsError<R>>
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>
impl<R: Read> Drop for Decompressor<R>
Source§impl<R: Read> Read for Decompressor<R>
impl<R: Read> Read for Decompressor<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<'_, u8>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(
&mut self,
cursor: BorrowedCursor<'_, u8>,
) -> Result<(), Error>
fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> 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 more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
1.0.0 · Source§fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
limit bytes from it. Read moreSource§fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
read_array)