pub struct Deflater { /* private fields */ }Expand description
A streaming raw DEFLATE encoder (RFC 1951).
Push input, pull output, with a Flush choosing how far each call
goes. Memory is fixed when the deflater is built (section 5.6 of the
plan): at window bits 15 and level 6, a 64 KiB window, 128 KiB of hash
chains, 64 KiB of symbols, and up to one block of pending output.
§Examples
use compress_lib::{Level, deflate::{self, Deflater, Flush}};
let mut deflater = Deflater::new(Level::DEFAULT);
let mut packed = Vec::new();
deflater.compress_vec(b"hello, hello, hello", &mut packed, Flush::Finish)?;
assert_eq!(deflate::decompress(&packed)?, b"hello, hello, hello");Implementations§
Source§impl Deflater
impl Deflater
Sourcepub fn new(level: Level) -> Self
pub fn new(level: Level) -> Self
A deflater at level with window bits 15.
§Examples
use compress_lib::{Level, deflate::Deflater};
let deflater = Deflater::new(Level::FAST);
assert_eq!(deflater.level(), Level::FAST);Sourcepub fn with_window_bits(self, bits: u8) -> Result<Self, Error>
pub fn with_window_bits(self, bits: u8) -> Result<Self, Error>
Sets the window to 2^bits bytes, 8 to 15: no match distance is ever
longer (RFC 7692 negotiates these sizes; 8 is a real 256-byte
window). Smaller windows also use smaller hash and symbol tables.
§Errors
ErrorKind::InvalidWindowBits outside 8 to 15.
§Examples
use compress_lib::{Level, deflate::Deflater};
let small = Deflater::new(Level::FAST).with_window_bits(9)?;
assert!(small.memory_in_use() < Deflater::new(Level::FAST).memory_in_use());
assert!(Deflater::new(Level::FAST).with_window_bits(7).is_err());Sourcepub fn level(&self) -> Level
pub fn level(&self) -> Level
The level in force.
§Examples
use compress_lib::{Level, deflate::Deflater};
assert_eq!(Deflater::default().level(), Level::DEFAULT);Sourcepub fn window_bits(&self) -> u8
pub fn window_bits(&self) -> u8
The window size in bits.
§Examples
assert_eq!(compress_lib::deflate::Deflater::default().window_bits(), 15);Sourcepub fn set_level(&mut self, level: Level)
pub fn set_level(&mut self, level: Level)
Changes the level for data not yet compressed. The current block is ended first when the change switches to or from level 0.
§Examples
use compress_lib::{Level, deflate::{self, Deflater, Flush}};
let mut deflater = Deflater::new(Level::BEST);
let mut out = Vec::new();
deflater.compress_vec(b"header ", &mut out, Flush::None)?;
deflater.set_level(Level::NONE);
deflater.compress_vec(b"body", &mut out, Flush::Finish)?;
assert_eq!(deflate::decompress(&out)?, b"header body");Sourcepub fn total_in(&self) -> u64
pub fn total_in(&self) -> u64
Uncompressed bytes taken since the deflater was built or reset.
§Examples
use compress_lib::deflate::{Deflater, Flush};
let mut d = Deflater::default();
d.compress_vec(b"abc", &mut Vec::new(), Flush::Finish)?;
assert_eq!(d.total_in(), 3);Sourcepub fn total_out(&self) -> u64
pub fn total_out(&self) -> u64
Compressed bytes handed out since the deflater was built or reset.
§Examples
use compress_lib::deflate::{Deflater, Flush};
let mut d = Deflater::default();
let mut out = Vec::new();
d.compress_vec(b"", &mut out, Flush::Finish)?;
assert_eq!(d.total_out(), out.len() as u64);Sourcepub fn memory_in_use(&self) -> usize
pub fn memory_in_use(&self) -> usize
Heap bytes held: window, hash chains, symbols, pending output, and block scratch.
§Examples
let d = compress_lib::deflate::Deflater::default();
assert!(d.memory_in_use() > 64 * 1024);Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Starts a new, unrelated stream, keeping the allocations.
§Examples
use compress_lib::deflate::{self, Deflater, Flush};
let mut d = Deflater::default();
let mut a = Vec::new();
d.compress_vec(b"first", &mut a, Flush::Finish)?;
d.reset();
let mut b = Vec::new();
d.compress_vec(b"second", &mut b, Flush::Finish)?;
assert_eq!(deflate::decompress(&b)?, b"second");Sourcepub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<(), Error>
pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<(), Error>
Presets the history with dictionary (its last 2^window_bits
bytes): matches may refer into it. The decoder must be given the same
dictionary. Only before any input.
§Errors
ErrorKind::InvalidState after input has been given.
§Examples
use compress_lib::deflate::{Deflater, Flush, Inflater};
let dict = b"{\"type\":\"update\",\"value\":";
let mut d = Deflater::default();
d.set_dictionary(dict)?;
let mut packed = Vec::new();
d.compress_vec(b"{\"type\":\"update\",\"value\":42}", &mut packed, Flush::Finish)?;
let mut i = Inflater::new();
i.set_dictionary(dict)?;
let mut out = Vec::new();
i.finish_vec(&packed, &mut out)?;
assert_eq!(out, b"{\"type\":\"update\",\"value\":42}");Sourcepub fn compress(
&mut self,
input: &[u8],
output: &mut [u8],
flush: Flush,
) -> Result<Progress, Error>
pub fn compress( &mut self, input: &[u8], output: &mut [u8], flush: Flush, ) -> Result<Progress, Error>
Compresses as much of input as the window takes and writes
compressed bytes to output.
With Flush::None the result is NeedsInput once all input is taken
(output may lag behind), or NeedsOutput when output is full.
With a flush, Done means all input is taken and the flush point is
completely in output; on NeedsOutput, call again with the same
flush and the input not yet consumed.
§Errors
ErrorKind::InvalidState when input is given after Finish
completed (call reset for a new stream).
§Examples
use compress_lib::deflate::{self, Deflater, Flush, Status};
let mut d = Deflater::default();
let mut out = [0u8; 256];
let p = d.compress(b"some data", &mut out, Flush::Finish)?;
assert_eq!(p.status, Status::Done);
assert_eq!(deflate::decompress(&out[..p.produced])?, b"some data");Sourcepub fn compress_vec(
&mut self,
input: &[u8],
out: &mut Vec<u8>,
flush: Flush,
) -> Result<Progress, Error>
pub fn compress_vec( &mut self, input: &[u8], out: &mut Vec<u8>, flush: Flush, ) -> Result<Progress, Error>
Compresses input and appends to out until the flush point is
written (with Flush::None, until all input is taken). Returns
Done, or NeedsInput for Flush::None.
§Errors
As compress, plus
ErrorKind::AllocationFailed when out cannot grow.
§Examples
use compress_lib::deflate::{self, Deflater, Flush};
let mut d = Deflater::default();
let mut out = Vec::new();
d.compress_vec(b"part one, ", &mut out, Flush::None)?;
d.compress_vec(b"part two", &mut out, Flush::Finish)?;
assert_eq!(deflate::decompress(&out)?, b"part one, part two");