[−][src]Function rscache::codec::encode
pub fn encode(
compression: Compression,
data: &[u8],
revision: Option<i16>
) -> Result<Vec<u8>>
Encodes a buffer, with the selected Compression
format. Revision is an optional argument
that encodes the version of this buffer into it, if no revision should be encoded
pass None.
The following process takes place when encoding:
- Compress the buffer with the selected compression format.
- Allocate a new buffer.
- Push the compression type as a byte into the new buffer.
- Push the length (u32) into the buffer of the compressed data from step 1.
- If a compression type was selected (and not
Compression::None
) insert the uncompressed length as u32. - Extend the buffer with the compressed data.
- Add the
revision
as i16 if present. - Encode complete.
Supported compression types:
- Gzip
- Bzip2
NOTE: When compressing with gzip the header is removed before the compressed data is returned. The encoded buffer will not contain the gzip header.
Errors
Returns an error if the data couldn't be compressed or is invalid.
Examples
use rscache::codec::Compression; let encoded_buffer = rscache::codec::encode(Compression::Bzip2, &buffer, None)?; assert_eq!(Compression::Bzip2 as u8, encoded_buffer[0]);