pub struct StreamDecoder { /* private fields */ }Expand description
A streaming decoder: feed it input in any pieces, get output in any
pieces. Handles concatenated and skippable frames. No std needed;
Decoder wraps it as a std::io::Read.
Output is decoded straight into the buffer passed to
decode, without going through the decoder’s window
buffer, from the start of a frame for as long as the buffer has room
for the next block (like libzstd’s single-pass shortcut in
ZSTD_decompressStream, but needing neither the whole frame in the
input nor its size in the header). An output buffer that holds a whole
frame, given at its start, gets it with no copy and no window buffer.
let mut d = zstd_native::StreamDecoder::new();
let mut out = [0u8; 2];
let mut got = Vec::new();
let mut input = compressed;
loop {
let p = d.decode(input, &mut out)?;
input = &input[p.read..];
got.extend_from_slice(&out[..p.written]);
if input.is_empty() && p.at_frame_boundary {
break;
}
}
assert_eq!(got, b"hello");Implementations§
Source§impl StreamDecoder
impl StreamDecoder
Sourcepub fn with_dictionary(dict: Dictionary) -> Self
pub fn with_dictionary(dict: Dictionary) -> Self
A decoder that uses dict for every frame.
Sourcepub fn set_window_log_max(&mut self, log: u32)
pub fn set_window_log_max(&mut self, log: u32)
Sets the largest window accepted (frames needing more fail with
Error::WindowTooLarge), as 1 << log, log 10 to 31 (30 on
32-bit targets, as libzstd); values outside are clamped. Frames
with a window log over 31 (30 on 32-bit) are rejected in every
mode, one-shot decoding included.
The default is 27 (128 MiB), libzstd’s.
Sourcepub fn at_frame_boundary(&self) -> bool
pub fn at_frame_boundary(&self) -> bool
True between frames with no output pending.
Sourcepub fn decode(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<Progress, Error>
pub fn decode( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>
Consumes input and writes output until the input is used up or the output is full.
§Errors
Any Error for corrupt or unsupported input; the decoder must
then be reset before reuse. Output written by an
erroring call is lost (as libzstd’s), except when the window buffer
can’t be allocated (Error::OutputTooLarge) after output already
went straight into output: that call returns its progress (not at
a frame boundary) and the next call returns the error.