Struct bzip2_rs::decoder::Decoder[][src]

pub struct Decoder { /* fields omitted */ }

A low-level decoder implementation

This decoder does no IO by itself, instead enough data has to be written to it in order for it to be able to decode the next block. After that the decompressed content for the block can be read until all of the data from the block has been exhausted. Repeating this process for every block in sequence will result into the entire file being decompressed.

use bzip2_rs::decoder::{Decoder, ReadState, WriteState};

let mut compressed_file: &[u8] = include_bytes!("../../tests/samplefiles/sample1.bz2").as_ref();
let mut output = Vec::new();

let mut decoder = Decoder::new();

assert!(
    !compressed_file.is_empty(),
    "empty files will cause the following loop to spin forever"
);

let mut buf = [0; 1024];
loop {
    match decoder.read(&mut buf)? {
        ReadState::NeedsWrite(space) => {
            // `Decoder` needs more data to be written to it before it
            // can decode the next block.
            // If we reached the end of the file `compressed_file.len()` will be 0,
            // signaling to the `Decoder` that the last block is smaller and it can
            // proceed with reading.
            match decoder.write(&compressed_file)? {
                WriteState::NeedsRead => unreachable!(),
                WriteState::Written(written) => compressed_file = &compressed_file[written..],
            };
        }
        ReadState::Read(n) => {
            // `n` uncompressed bytes have been read into `buf`
            output.extend_from_slice(&buf[..n]);
        }
        ReadState::Eof => {
            // we reached the end of the file
            break;
        }
    }
}

// `output` contains the decompressed file
let decompressed_file: &[u8] = include_bytes!("../../tests/samplefiles/sample1.ref").as_ref();
assert_eq!(output, decompressed_file);

Implementations

impl Decoder[src]

pub fn new() -> Self[src]

Construct a new Decoder, ready to decompress a new bzip2 file

pub fn write(&mut self, buf: &[u8]) -> Result<WriteState, DecoderError>[src]

Write more compressed data into this Decoder

See the documentation for WriteState to decide what to do next.

pub fn read(&mut self, buf: &mut [u8]) -> Result<ReadState, DecoderError>[src]

Read more decompressed data from this Decoder

See the documentation for ReadState to decide what to do next.

Trait Implementations

impl Default for Decoder[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.