Skip to content

Commit

Permalink
Make the buffer size configurable (#307)
Browse files Browse the repository at this point in the history
* Add new_with_capacity to allow users to create Writers with custom capacity.

* Add test for buffer_with_capacity
  • Loading branch information
ksolana authored Jan 9, 2025
1 parent 1ff414d commit 03e955d
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/stream/zio/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ where
W: Write,
D: Operation,
{
/// Creates a new `Writer`.
/// Creates a new `Writer` with a fixed buffer capacity of 32KB
///
/// All output from the given operation will be forwarded to `writer`.
pub fn new(writer: W, operation: D) -> Self {
// 32KB buffer? That's what flate2 uses
new_with_capacity(W, D, 32 * 1024)
}

/// Creates a new `Writer` with user defined capacity.
///
/// All output from the given operation will be forwarded to `writer`.
pub fn new_with_capacity(writer: W, operation: D, capacity: usize) -> Self {
Self::with_output_buffer(
Vec::with_capacity(32 * 1024),
Vec::with_capacity(capacity),
writer,
operation,
)
Expand Down Expand Up @@ -314,6 +321,25 @@ mod tests {
assert_eq!(&decoded, input);
}

#[test]
fn test_compress_with_capacity() {
use crate::stream::raw::Encoder;

let input = b"AbcdefghAbcdefgh.";

// Test writer
let mut output = Vec::new();
{
let mut writer =
Writer::new_with_capacity(&mut output, Encoder::new(1).unwrap(), 64);
assert_eq!(writer.buffer().capacity() == 64);
writer.write_all(input).unwrap();
writer.finish().unwrap();
}
let decoded = crate::decode_all(&output[..]).unwrap();
assert_eq!(&decoded, input);
}

#[test]
fn test_decompress() {
use crate::stream::raw::Decoder;
Expand All @@ -331,4 +357,22 @@ mod tests {
// println!("Output: {:?}", output);
assert_eq!(&output, input);
}

#[test]
fn test_decompress_with_capacity() {
use crate::stream::raw::Decoder;

let input = b"AbcdefghAbcdefgh.";
let compressed = crate::encode_all(&input[..], 1).unwrap();

// Test writer
let mut output = Vec::new();
{
let mut writer = Writer::new(&mut output, Decoder::new().unwrap(), 64);
assert_eq!(writer.buffer().capacity() == 64);
writer.write_all(&compressed).unwrap();
writer.finish().unwrap();
}
assert_eq!(&output, input);
}
}

0 comments on commit 03e955d

Please sign in to comment.