Skip to content

Commit

Permalink
Support .flush for Compressor (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger authored Sep 8, 2021
1 parent 0a93fd8 commit 4b92e42
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cramjam"
version = "2.4.0-rc1"
version = "2.4.0-rc2"
authors = ["Miles Granger <miles59923@gmail.com>"]
edition = "2018"
license = "MIT License"
Expand Down
5 changes: 5 additions & 0 deletions src/brotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ impl Compressor {
crate::io::stream_compress(&mut self.inner, input)
}

/// Flush and return current compressed stream
pub fn flush(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_flush(&mut self.inner, |e| e.get_mut())
}

/// Consume the current compressor state and return the compressed stream
/// **NB** The compressor will not be usable after this method is called.
pub fn finish(&mut self) -> PyResult<RustyBuffer> {
Expand Down
5 changes: 5 additions & 0 deletions src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ impl Compressor {
crate::io::stream_compress(&mut self.inner, input)
}

/// Flush and return current compressed stream
pub fn flush(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_flush(&mut self.inner, |e| e.get_mut())
}

/// Consume the current compressor state and return the compressed stream
/// **NB** The compressor will not be usable after this method is called.
pub fn finish(&mut self) -> PyResult<RustyBuffer> {
Expand Down
5 changes: 5 additions & 0 deletions src/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ impl Compressor {
crate::io::stream_compress(&mut self.inner, input)
}

/// Flush and return current compressed stream
pub fn flush(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_flush(&mut self.inner, |e| e.get_mut())
}

/// Consume the current compressor state and return the compressed stream
/// **NB** The compressor will not be usable after this method is called.
pub fn finish(&mut self) -> PyResult<RustyBuffer> {
Expand Down
19 changes: 19 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,22 @@ where
None => Ok(RustyBuffer::from(vec![])),
}
}

// flush inner encoder data out
pub(crate) fn stream_flush<W, F>(encoder: &mut Option<W>, cursor_mut_ref: F) -> PyResult<RustyBuffer>
where
W: Write,
F: Fn(&mut W) -> &mut Cursor<Vec<u8>>,
{
match encoder {
Some(inner) => {
crate::to_py_err!(CompressionError -> inner.flush())?;
let cursor = cursor_mut_ref(inner);
let buf = RustyBuffer::from(cursor.get_ref().clone());
cursor.get_mut().truncate(0);
cursor.set_position(0);
Ok(buf)
}
None => Ok(RustyBuffer::from(vec![])),
}
}
9 changes: 9 additions & 0 deletions src/lz4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ impl Compressor {
crate::io::stream_compress(&mut self.inner, input)
}

/// Flush and return current compressed stream
#[allow(mutable_transmutes)] // TODO: feature req to lz4 to get mut ref to writer
pub fn flush(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_flush(&mut self.inner, |e| {
let writer = e.writer();
unsafe { std::mem::transmute::<&Cursor<Vec<u8>>, &mut Cursor<Vec<u8>>>(writer) }
})
}

/// Consume the current compressor state and return the compressed stream
/// **NB** The compressor will not be usable after this method is called.
pub fn finish(&mut self) -> PyResult<RustyBuffer> {
Expand Down
5 changes: 5 additions & 0 deletions src/snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ impl Compressor {
crate::io::stream_compress(&mut self.inner, input)
}

/// Flush and return current compressed stream
pub fn flush(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_flush(&mut self.inner, |e| e.get_mut())
}

/// Consume the current compressor state and return the compressed stream
/// **NB** The compressor will not be usable after this method is called.
pub fn finish(&mut self) -> PyResult<RustyBuffer> {
Expand Down
5 changes: 5 additions & 0 deletions src/zstd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ impl Compressor {
crate::io::stream_compress(&mut self.inner, input)
}

/// Flush and return current compressed stream
pub fn flush(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_flush(&mut self.inner, |e| e.get_mut())
}

/// Consume the current compressor state and return the compressed stream
/// **NB** The compressor will not be usable after this method is called.
pub fn finish(&mut self) -> PyResult<RustyBuffer> {
Expand Down
9 changes: 7 additions & 2 deletions tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,14 @@ def test_gzip_multiple_streams():
)
def test_streams_compressor(mod):
compressor = mod.Compressor()

compressor.compress(b"foo")
out = bytes(compressor.flush())

compressor.compress(b"bar")
out = compressor.finish()
out += bytes(compressor.flush())

out += bytes(compressor.finish())
decompressed = mod.decompress(out)
assert bytes(decompressed) == b"foobar"

Expand All @@ -266,4 +271,4 @@ def test_streams_compressor(mod):

# compress will raise an error as the stream is completed
with pytest.raises(cramjam.CompressionError):
compressor.compress(b'data')
compressor.compress(b"data")

0 comments on commit 4b92e42

Please sign in to comment.