pub struct StreamEncoder { /* private fields */ }Expand description
A streaming compressor (libzstd’s ZSTD_CStream with
ZSTD_compressStream2): feed it input in any pieces, get output in any
pieces, frame after frame. The output is byte-identical to libzstd
1.6.0’s for the same level, input, operations and output buffer sizes
(see compress). No std needed;
Encoder wraps it as a std::io::Write.
use zstd_native::{EndOp, StreamEncoder};
let data = b"hello hello hello hello hello hello".repeat(100);
let mut enc = StreamEncoder::new(3);
let mut out = Vec::new();
let mut buf = [0u8; 64];
let mut input = &data[..];
// Feed the input in pieces of 100 bytes, then end the frame.
while !input.is_empty() {
let n = input.len().min(100);
let mut piece = &input[..n];
while !piece.is_empty() {
let p = enc.compress(piece, &mut buf, EndOp::Continue)?;
piece = &piece[p.read..];
out.extend_from_slice(&buf[..p.written]);
}
input = &input[n..];
}
loop {
let p = enc.compress(&[], &mut buf, EndOp::End)?;
out.extend_from_slice(&buf[..p.written]);
if p.remaining == 0 {
break;
}
}
assert_eq!(zstd_native::decompress(&out)?, data);At level 22 with a window of 128 MiB, i.e. for unknown sizes and sizes over 64 MiB, libzstd enables long distance matching by itself, and so does this encoder, with the same output (and libzstd’s memory use: over 800 MiB for the tables and the input buffer).
Implementations§
Source§impl StreamEncoder
impl StreamEncoder
Sourcepub fn set_workers(&mut self, n: u32) -> Result<(), Error>
Available on crate feature std only.
pub fn set_workers(&mut self, n: u32) -> Result<(), Error>
std only.Compresses the next frames with n worker threads (libzstd’s
ZSTD_c_nbWorkers; at most 256, 64 on 32-bit targets); 0, the
default, compresses in the calling thread. The output is
byte-identical to libzstd’s with the same worker count, see
Compressor::set_workers for how
frames are cut into jobs, threads and memory.
A frame whose pledged size (or, when the first call ends it, input
size) is at most 512 KiB is still compressed in the calling thread.
Otherwise, input is buffered until a job is full (or a flush or the
end comes), then compressed by a worker while more input is taken:
compress with EndOp::Continue returns once
it has taken some input or written some output (waiting for the
oldest job when it could do neither), and the output does not
depend on the output buffer sizes.
§Errors
Error::StageWrong in the middle of a frame.
Sourcepub fn workers(&self) -> u32
Available on crate feature std only.
pub fn workers(&self) -> u32
std only.The worker thread count (see set_workers).
Sourcepub fn with_dictionary(level: i32, dict: &[u8]) -> Result<Self, Error>
pub fn with_dictionary(level: i32, dict: &[u8]) -> Result<Self, Error>
A streaming compressor for level with the dictionary dict (see
set_dictionary).
§Errors
Error::InvalidDictionary if a zstd-format dictionary is corrupt.
Sourcepub fn with_prepared_dictionary(dict: &EncoderDictionary) -> Self
pub fn with_prepared_dictionary(dict: &EncoderDictionary) -> Self
A streaming compressor with a prepared dictionary, at its level (see
set_prepared_dictionary).
Sourcepub fn set_level(&mut self, level: i32)
pub fn set_level(&mut self, level: i32)
Changes the level, from the next frame on. With a prepared
dictionary, frames take the dictionary’s level instead (see
Compressor::set_level).
Sourcepub fn set_dictionary(&mut self, dict: &[u8]) -> Result<(), Error>
pub fn set_dictionary(&mut self, dict: &[u8]) -> Result<(), Error>
Uses dict for the next frames (libzstd’s
ZSTD_CCtx_loadDictionary; see
Compressor::set_dictionary),
replacing any dictionary; an empty dict removes it.
§Errors
Error::StageWrong in the middle of a frame,
Error::InvalidDictionary if a zstd-format dictionary is corrupt
(the encoder then has no dictionary).
Sourcepub fn set_prepared_dictionary(
&mut self,
dict: &EncoderDictionary,
) -> Result<(), Error>
pub fn set_prepared_dictionary( &mut self, dict: &EncoderDictionary, ) -> Result<(), Error>
Uses the prepared dict for the next frames, at its level (libzstd’s
ZSTD_CCtx_refCDict), replacing any dictionary.
§Errors
Error::StageWrong in the middle of a frame.
Sourcepub fn set_pledged_src_size(&mut self, size: Option<u64>) -> Result<(), Error>
pub fn set_pledged_src_size(&mut self, size: Option<u64>) -> Result<(), Error>
Declares the size of the next frame’s content (libzstd’s
ZSTD_CCtx_setPledgedSrcSize; None: unknown, the default). The
frame header then holds it, and the parameters are chosen for it
(a smaller window for small sizes). The frame must then get exactly
that many bytes. It applies to one frame only.
§Errors
Error::StageWrong in the middle of a frame.
Sourcepub fn set_checksum(&mut self, on: bool) -> Result<(), Error>
pub fn set_checksum(&mut self, on: bool) -> Result<(), Error>
Ends the next frames with a content checksum (libzstd’s
ZSTD_c_checksumFlag; off by default; see
Compressor::set_checksum).
§Errors
Error::StageWrong in the middle of a frame.
Sourcepub fn set_content_size_flag(&mut self, on: bool) -> Result<(), Error>
pub fn set_content_size_flag(&mut self, on: bool) -> Result<(), Error>
Writes the content size into the next frames’ headers when it is
known (libzstd’s ZSTD_c_contentSizeFlag; on by default; see
Compressor::set_content_size_flag).
The pledged size still chooses the parameters and is still checked.
§Errors
Error::StageWrong in the middle of a frame.
Sourcepub fn set_dict_id_flag(&mut self, on: bool) -> Result<(), Error>
pub fn set_dict_id_flag(&mut self, on: bool) -> Result<(), Error>
Writes the dictionary’s ID into the next frames’ headers (libzstd’s
ZSTD_c_dictIDFlag; on by default; see
Compressor::set_dict_id_flag).
§Errors
Error::StageWrong in the middle of a frame.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Abandons the current frame, if any (libzstd’s ZSTD_CCtx_reset
with ZSTD_reset_session_only): the next call starts a new one. The
pledged source size is forgotten; the level is kept.
Sourcepub fn at_frame_boundary(&self) -> bool
pub fn at_frame_boundary(&self) -> bool
Whether no frame is in progress: the last frame was ended and all its output was written (or nothing was compressed yet).
Sourcepub fn compress(
&mut self,
input: &[u8],
output: &mut [u8],
op: EndOp,
) -> Result<CompressProgress, Error>
pub fn compress( &mut self, input: &[u8], output: &mut [u8], op: EndOp, ) -> Result<CompressProgress, Error>
Compresses as much of input as possible into output, then does
what op asks (libzstd’s ZSTD_compressStream2). Starts a frame if
none is in progress.
With EndOp::Continue, it consumes input until the output is full
(and may consume all of it while writing nothing). With
EndOp::Flush and EndOp::End, call again (with the rest of
the input, if not all was read) until
remaining is 0.
The output matches libzstd’s for the same calls with the same
input pieces and the same output buffer sizes. Without flushes, it
doesn’t depend on how the input is split across EndOp::Continue
calls, nor on the output sizes as long as the frame ends with an
EndOp::End call that has no input (see the module docs for why
an End call with input may differ).
§Errors
Error::SrcSizeWrong if a frame gets more or less input than its
pledged size. After an error the frame is abandoned (as with
reset).