Skip to content

Commit

Permalink
Merge pull request #361 from PierreV23/main
Browse files Browse the repository at this point in the history
Add functionality for custom (de)compress instances
  • Loading branch information
Byron authored Jul 31, 2023
2 parents 956397a + c9fe661 commit 5d462b3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/zlib/bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ impl<R: BufRead> ZlibEncoder<R> {
data: Compress::new(level, true),
}
}

/// Creates a new encoder with the given `compression` settings which will
/// read uncompressed data from the given stream `r` and emit the compressed stream.
pub fn new_with_compress(r: R, compression: Compress) -> ZlibEncoder<R> {
ZlibEncoder {
obj: r,
data: compression,
}
}
}

pub fn reset_encoder_data<R>(zlib: &mut ZlibEncoder<R>) {
Expand Down Expand Up @@ -165,6 +174,15 @@ impl<R: BufRead> ZlibDecoder<R> {
data: Decompress::new(true),
}
}

/// Creates a new decoder which will decompress data read from the given
/// stream, using the given `decompression` settings.
pub fn new_with_decompress(r: R, decompression: Decompress) -> ZlibDecoder<R> {
ZlibDecoder {
obj: r,
data: decompression,
}
}
}

pub fn reset_decoder_data<R>(zlib: &mut ZlibDecoder<R>) {
Expand Down
37 changes: 36 additions & 1 deletion src/zlib/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::prelude::*;

use super::bufread;
use crate::bufreader::BufReader;
use crate::Decompress;

/// A ZLIB encoder, or compressor.
///
Expand Down Expand Up @@ -42,6 +43,14 @@ impl<R: Read> ZlibEncoder<R> {
inner: bufread::ZlibEncoder::new(BufReader::new(r), level),
}
}

/// Creates a new encoder with the given `compression` settings which will
/// read uncompressed data from the given stream `r` and emit the compressed stream.
pub fn new_with_compress(r: R, compression: crate::Compress) -> ZlibEncoder<R> {
ZlibEncoder {
inner: bufread::ZlibEncoder::new_with_compress(BufReader::new(r), compression),
}
}
}

impl<R> ZlibEncoder<R> {
Expand Down Expand Up @@ -160,7 +169,8 @@ impl<R: Read> ZlibDecoder<R> {
ZlibDecoder::new_with_buf(r, vec![0; 32 * 1024])
}

/// Same as `new`, but the intermediate buffer for data is specified.
/// Creates a new decoder which will decompress data read from the given
/// stream `r`, using `buf` as backing to speed up reading.
///
/// Note that the specified buffer will only be used up to its current
/// length. The buffer's capacity will also not grow over time.
Expand All @@ -169,6 +179,31 @@ impl<R: Read> ZlibDecoder<R> {
inner: bufread::ZlibDecoder::new(BufReader::with_buf(buf, r)),
}
}

/// Creates a new decoder which will decompress data read from the given
/// stream `r`, along with `decompression` settings.
pub fn new_with_decompress(r: R, decompression: Decompress) -> ZlibDecoder<R> {
ZlibDecoder::new_with_decompress_and_buf(r, vec![0; 32 * 1024], decompression)
}

/// Creates a new decoder which will decompress data read from the given
/// stream `r`, using `buf` as backing to speed up reading,
/// along with `decompression` settings to configure decoder.
///
/// 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<u8>,
decompression: Decompress,
) -> ZlibDecoder<R> {
ZlibDecoder {
inner: bufread::ZlibDecoder::new_with_decompress(
BufReader::with_buf(buf, r),
decompression,
),
}
}
}

impl<R> ZlibDecoder<R> {
Expand Down
19 changes: 19 additions & 0 deletions src/zlib/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ impl<W: Write> ZlibEncoder<W> {
}
}

/// Creates a new encoder which will write compressed data to the stream
/// `w` with the given `compression` settings.
pub fn new_with_compress(w: W, compression: Compress) -> ZlibEncoder<W> {
ZlibEncoder {
inner: zio::Writer::new(w, compression),
}
}

/// Acquires a reference to the underlying writer.
pub fn get_ref(&self) -> &W {
self.inner.get_ref()
Expand Down Expand Up @@ -218,6 +226,17 @@ impl<W: Write> ZlibDecoder<W> {
}
}

/// Creates a new decoder which will write uncompressed data to the stream `w`
/// using the given `decompression` settings.
///
/// When this decoder is dropped or unwrapped the final pieces of data will
/// be flushed.
pub fn new_with_decompress(w: W, decompression: Decompress) -> ZlibDecoder<W> {
ZlibDecoder {
inner: zio::Writer::new(w, decompression),
}
}

/// Acquires a reference to the underlying writer.
pub fn get_ref(&self) -> &W {
self.inner.get_ref()
Expand Down

0 comments on commit 5d462b3

Please sign in to comment.