Skip to content

Commit

Permalink
Add functions that allow (de)compress instances
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreV23 committed Jul 16, 2023
1 parent 74870ae commit 2754030
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
20 changes: 20 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),
}
}

/// 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<R> {
ZlibEncoder {
obj: r,
data: compress,
}
}
}

pub fn reset_encoder_data<R>(zlib: &mut ZlibEncoder<R>) {
Expand Down Expand Up @@ -165,6 +174,17 @@ impl<R: BufRead> ZlibDecoder<R> {
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<R> {
ZlibDecoder {
obj: r,
data: decompress,
}
}
}

pub fn reset_decoder_data<R>(zlib: &mut ZlibDecoder<R>) {
Expand Down
34 changes: 34 additions & 0 deletions 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),
}
}

/// 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<R> {
ZlibEncoder {
inner: bufread::ZlibEncoder::new_with_compress(BufReader::new(r), compress),
}
}
}

impl<R> ZlibEncoder<R> {
Expand Down Expand Up @@ -169,6 +178,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.
///
/// Also takes in a custom Decompress instance.
pub fn new_with_decompress(r: R, decompress: Decompress) -> ZlibDecoder<R> {
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<u8>,
decompress: Decompress,
) -> ZlibDecoder<R> {
ZlibDecoder {
inner: bufread::ZlibDecoder::new_with_decompress(
BufReader::with_buf(buf, r),
decompress,
),
}
}
}

impl<R> ZlibDecoder<R> {
Expand Down
15 changes: 15 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> {
}
}

/// 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<W> {
ZlibEncoder {
inner: zio::Writer::new(w, compress),
}
}

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

/// This is like `new` but with a supplied `Decompress` instance.
pub fn new_with_decompress(w: W, decomp: Decompress) -> ZlibDecoder<W> {
ZlibDecoder {
inner: zio::Writer::new(w, decomp),
}
}

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

0 comments on commit 2754030

Please sign in to comment.