Struct bzip2_rs::decoder::Decoder [−][src]
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]
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Decoder
[src]
impl Send for Decoder
[src]
impl Sync for Decoder
[src]
impl Unpin for Decoder
[src]
impl UnwindSafe for Decoder
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,