Struct brotli2::raw::Compress[][src]

pub struct Compress { /* fields omitted */ }

In-memory state for compressing/encoding data with brotli

This stream is at the heart of the I/O encoders and is used to compress data.

Methods

impl Compress
[src]

Creates a new compressor ready to encode data into brotli

Pass some input data to the compressor and write it to a buffer of output data, compressing or otherwise handling it as instructed by the specified operation.

This function will handle the data in input and place the output in output, returning the Result. Possible statuses are that the operation is complete or incomplete.

The input slice is updated to point to the remaining data that was not consumed, and the output slice is updated to point to the portion of the output slice that still needs to be filled in.

If the result of a compress operation is Unfinished (which it may be for any operation except Process), you must call the operation again with the same operation and input buffer and more space to output to. Process will never return Unfinished, but it is a logic error to end a buffer without calling either Flush or Finish as some output data may not have been written.

Errors

Returns an error if brotli encountered an error while processing the stream.

Examples

use brotli2::raw::{Error, Compress, CompressOp, CoStatus, decompress_buf};

// An example of compressing `input` into the destination vector
// `output`, expanding as necessary
fn compress_vec(mut input: &[u8],
                output: &mut Vec<u8>) -> Result<(), Error> {
    let mut compress = Compress::new();
    let nilbuf = &mut &mut [][..];
    loop {
        // Compressing to a buffer is easiest when the slice is already
        // available - since we need to grow, extend from compressor
        // internal buffer.
        let status = try!(compress.compress(CompressOp::Finish, &mut input, nilbuf));
        while let Some(buf) = compress.take_output(None) {
            output.extend_from_slice(buf)
        }
        match status {
            CoStatus::Finished => break,
            CoStatus::Unfinished => (),
        }
    }
    Ok(())
}

fn assert_roundtrip(data: &[u8]) {
    let mut compressed = Vec::new();
    compress_vec(data, &mut compressed).unwrap();

    let mut decompressed = [0; 2048];
    let mut decompressed = &mut decompressed[..];
    decompress_buf(&compressed, &mut decompressed).unwrap();
    assert_eq!(decompressed, data);
}

assert_roundtrip(b"Hello, World!");
assert_roundtrip(b"");
assert_roundtrip(&[6; 1024]);

Retrieve a slice of the internal compressor buffer up to size_limit in length (unlimited length if None), consuming it. As the internal buffer may not be contiguous, consecutive calls may return more output until this function returns None.

Configure the parameters of this compression session.

Note that this is likely to only successful if called before compression starts.

Trait Implementations

impl Send for Compress
[src]

impl Sync for Compress
[src]

impl Drop for Compress
[src]

Executes the destructor for this type. Read more