compress into reusable buffers

This commit is contained in:
Robert Habermeier
2016-06-14 13:22:15 +02:00
parent 16e58958c9
commit dfb603dd08
2 changed files with 46 additions and 9 deletions

View File

@@ -68,9 +68,14 @@ impl fmt::Display for Error {
}
}
/// The maximum compressed length given a size.
pub fn max_compressed_len(len: usize) -> usize {
unsafe { snappy_max_compressed_length(len as size_t) as usize }
}
/// Compress a buffer using snappy.
pub fn compress(input: &[u8]) -> Vec<u8> {
let mut buf_size = unsafe { snappy_max_compressed_length(input.len() as size_t) };
let mut buf_size = max_compressed_len(input.len());
let mut output = vec![0; buf_size as usize];
buf_size = compress_into(input, &mut output).expect("snappy compression failed with large enough buffer.");
@@ -79,7 +84,7 @@ pub fn compress(input: &[u8]) -> Vec<u8> {
}
/// Compress a buffer using snappy, writing the result into
/// the given output buffer. Will error if the buffer is too small.
/// the given output buffer. Will error iff the buffer is too small.
/// Otherwise, returns the length of the compressed data.
pub fn compress_into(input: &[u8], output: &mut [u8]) -> Result<usize, Error> {
let mut len = output.len() as size_t;