diff --git a/src/zlib/bufread.rs b/src/zlib/bufread.rs index 61d12525..729264ff 100644 --- a/src/zlib/bufread.rs +++ b/src/zlib/bufread.rs @@ -47,6 +47,15 @@ impl ZlibEncoder { data: Compress::new(level, true), } } + + /// Same as `new` but instead of passing a `Compression` instance, + /// a `Compress` instance is passed. + pub fn new_with_compress(r: R, compress: crate::Compress) -> ZlibEncoder { + ZlibEncoder { + obj: r, + data: compress, + } + } } pub fn reset_encoder_data(zlib: &mut ZlibEncoder) { @@ -165,6 +174,17 @@ impl ZlibDecoder { data: Decompress::new(true), } } + + /// Creates a new decoder which will decompress data read from the given + /// stream. + /// + /// Also takes in a Decompress instance. + pub fn new_with_decompress(r: R, decompress: Decompress) -> ZlibDecoder { + ZlibDecoder { + obj: r, + data: decompress, + } + } } pub fn reset_decoder_data(zlib: &mut ZlibDecoder) { diff --git a/src/zlib/read.rs b/src/zlib/read.rs index 33021304..41d5d9f8 100644 --- a/src/zlib/read.rs +++ b/src/zlib/read.rs @@ -3,6 +3,7 @@ use std::io::prelude::*; use super::bufread; use crate::bufreader::BufReader; +use crate::Decompress; /// A ZLIB encoder, or compressor. /// @@ -42,6 +43,14 @@ impl ZlibEncoder { inner: bufread::ZlibEncoder::new(BufReader::new(r), level), } } + + /// Same as `new` but with the ability to add a `Compress` instance rather + /// than a `Compression` instance. + pub fn new_with_compress(r: R, compress: crate::Compress) -> ZlibEncoder { + ZlibEncoder { + inner: bufread::ZlibEncoder::new_with_compress(BufReader::new(r), compress), + } + } } impl ZlibEncoder { @@ -169,6 +178,31 @@ impl ZlibDecoder { inner: bufread::ZlibDecoder::new(BufReader::with_buf(buf, r)), } } + + /// Creates a new decoder which will decompress data read from the given + /// stream. + /// + /// Also takes in a custom Decompress instance. + pub fn new_with_decompress(r: R, decompress: Decompress) -> ZlibDecoder { + ZlibDecoder::new_with_decompress_and_buf(r, vec![0; 32 * 1024], decompress) + } + + /// Same as `new_with_decompress`, but the intermediate buffer for data is specified. + /// + /// Note that the specified buffer will only be used up to its current + /// length. The buffer's capacity will also not grow over time. + pub fn new_with_decompress_and_buf( + r: R, + buf: Vec, + decompress: Decompress, + ) -> ZlibDecoder { + ZlibDecoder { + inner: bufread::ZlibDecoder::new_with_decompress( + BufReader::with_buf(buf, r), + decompress, + ), + } + } } impl ZlibDecoder { diff --git a/src/zlib/write.rs b/src/zlib/write.rs index c6718140..9cc27419 100644 --- a/src/zlib/write.rs +++ b/src/zlib/write.rs @@ -44,6 +44,14 @@ impl ZlibEncoder { } } + /// Same as `new` but with the ability to add a `Compress` instance rather + /// than a `Compression` instance. + pub fn new_with_compress(w: W, compress: crate::Compress) -> ZlibEncoder { + ZlibEncoder { + inner: zio::Writer::new(w, compress), + } + } + /// Acquires a reference to the underlying writer. pub fn get_ref(&self) -> &W { self.inner.get_ref() @@ -218,6 +226,13 @@ impl ZlibDecoder { } } + /// This is like `new` but with a supplied `Decompress` instance. + pub fn new_with_decompress(w: W, decomp: Decompress) -> ZlibDecoder { + ZlibDecoder { + inner: zio::Writer::new(w, decomp), + } + } + /// Acquires a reference to the underlying writer. pub fn get_ref(&self) -> &W { self.inner.get_ref()