Struct Decoder

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

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§

Source§

impl Decoder

Source

pub fn new() -> Self

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

Source

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

Write more compressed data into this Decoder

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

Source

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

Read more decompressed data from this Decoder

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

Trait Implementations§

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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.