Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder { /* private fields */ }
Expand description

A streaming gzip decoder.

Multi-member streams (RFC 1952 section 2.2) are decoded as one by default, up to Limits::max_members; the output cap applies to all members together. With more input possible after a member, decompress returns NeedsInput; finish decides that the stream ended.

§Examples

use compress_lib::{deflate::Status, gzip};

let packed = gzip::compress(b"streamed");
let mut dec = gzip::Decoder::new();
let mut out = [0u8; 32];
let p = dec.finish(&packed, &mut out)?;
assert_eq!(p.status, Status::Done);
assert_eq!(&out[..p.produced], b"streamed");

Implementations§

Source§

impl Decoder

Source

pub fn new() -> Self

A decoder with window bits 15 and the default limits, multi-member, trailing data refused, header fields not kept.

§Examples
assert!(!compress_lib::gzip::Decoder::new().is_done());
Source

pub fn with_limits(self, limits: Limits) -> Self

Sets the limits: the output cap (across members), the ratio cap, the header field cap, and the member cap.

§Examples
use compress_lib::{Limits, gzip};

let dec = gzip::Decoder::new().with_limits(Limits::new().with_max_members(1));
Source

pub fn with_single_member(self) -> Self

Decodes one member only; Done at its end, and what follows is left to the caller.

§Examples
use compress_lib::{deflate::Status, gzip};

let mut two = gzip::compress(b"first");
let first_len = two.len();
two.extend_from_slice(&gzip::compress(b"second"));
let mut dec = gzip::Decoder::new().with_single_member();
let mut out = Vec::new();
let p = dec.decompress_vec(&two, &mut out)?;
assert_eq!((p.status, p.consumed), (Status::Done, first_len));
Source

pub fn with_trailing_data(self, allow: bool) -> Self

Allows bytes that are not a gzip member after the last member (some tools pad files with zeros); consumed stops before them.

§Examples
use compress_lib::gzip;

let mut padded = gzip::compress(b"data");
padded.extend_from_slice(&[0; 8]);
let mut dec = gzip::Decoder::new().with_trailing_data(true);
let mut out = Vec::new();
dec.finish_vec(&padded, &mut out)?;
assert_eq!(out, b"data");
Source

pub fn with_keep_header(self, keep: bool) -> Self

Keeps the header fields (name, comment, extra) for header (bounded by Limits::max_header); off by default, so no memory is spent on them.

§Examples
let dec = compress_lib::gzip::Decoder::new().with_keep_header(true);
assert!(dec.header().is_none());
Source

pub fn header(&self) -> Option<&GzipHeader>

The first member’s header, once read, when headers are kept.

§Examples
use compress_lib::gzip;

let mut dec = gzip::Decoder::new().with_keep_header(true);
dec.finish_vec(&gzip::compress(b"x"), &mut Vec::new())?;
assert_eq!(dec.header().map(|h| h.os()), Some(255));
Source

pub fn is_done(&self) -> bool

true once the stream ended.

§Examples
use compress_lib::gzip;

let mut dec = gzip::Decoder::new();
dec.finish_vec(&gzip::compress(b""), &mut Vec::new())?;
assert!(dec.is_done());
Source

pub fn members(&self) -> u32

Members completed so far.

§Examples
use compress_lib::gzip;

let mut two = gzip::compress(b"a");
two.extend_from_slice(&gzip::compress(b"b"));
let mut dec = gzip::Decoder::new();
dec.finish_vec(&two, &mut Vec::new())?;
assert_eq!(dec.members(), 2);
Source

pub fn total_in(&self) -> u64

Compressed bytes consumed.

§Examples
use compress_lib::gzip;

let packed = gzip::compress(b"x");
let mut dec = gzip::Decoder::new();
dec.finish_vec(&packed, &mut Vec::new())?;
assert_eq!(dec.total_in(), packed.len() as u64);
Source

pub fn total_out(&self) -> u64

Decompressed bytes produced, all members.

§Examples
use compress_lib::gzip;

let mut dec = gzip::Decoder::new();
dec.finish_vec(&gzip::compress(b"four"), &mut Vec::new())?;
assert_eq!(dec.total_out(), 4);
Source

pub fn memory_in_use(&self) -> usize

Heap bytes held.

§Examples
assert!(compress_lib::gzip::Decoder::new().memory_in_use() > 32 * 1024);
Source

pub fn reset(&mut self)

Starts over for a new stream, keeping allocations.

§Examples
let mut dec = compress_lib::gzip::Decoder::new();
dec.reset();
Source

pub fn decompress( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>

Decompresses as much of input into output as fits; more input may follow (and after a member, perhaps another member).

§Errors

Decoding errors, the header and trailer checks, and the limits.

§Examples
use compress_lib::{deflate::Status, gzip};

let packed = gzip::compress(b"split");
let mut dec = gzip::Decoder::new();
let mut out = [0u8; 16];
let a = dec.decompress(&packed[..5], &mut out)?;
assert_eq!(a.status, Status::NeedsInput);
let b = dec.finish(&packed[5..], &mut out[a.produced..])?;
assert_eq!(b.status, Status::Done);
Source

pub fn finish( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>

As decompress, with input the last of the stream.

§Errors

As decompress, plus UnexpectedEnd.

§Examples
use compress_lib::{ErrorKind, gzip};

let packed = gzip::compress(b"data");
let err = gzip::Decoder::new().finish(&packed[..packed.len() - 1], &mut [0u8; 8]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::UnexpectedEnd);
Source

pub fn decompress_vec( &mut self, input: &[u8], out: &mut Vec<u8>, ) -> Result<Progress, Error>

Decompresses input, appending to out.

§Errors

As decompress, plus AllocationFailed.

§Examples
use compress_lib::gzip;

let mut out = Vec::new();
gzip::Decoder::new().decompress_vec(&gzip::compress(b"vec"), &mut out)?;
assert_eq!(out, b"vec");
Source

pub fn finish_vec( &mut self, input: &[u8], out: &mut Vec<u8>, ) -> Result<Progress, Error>

As decompress_vec, with input the last of the stream.

§Errors

As decompress_vec, plus UnexpectedEnd.

§Examples
use compress_lib::gzip;

let mut out = Vec::new();
gzip::Decoder::new().finish_vec(&gzip::compress(b"vec"), &mut out)?;
assert_eq!(out, b"vec");

Trait Implementations§

Source§

impl Debug for Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Decoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.