Skip to content

Commit

Permalink
handle empty input
Browse files Browse the repository at this point in the history
closes #116
  • Loading branch information
PSeitz committed May 30, 2023
1 parent 7b64741 commit b6774ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/frame/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub struct FrameEncoder<W: io::Write> {
dst: Vec<u8>,
/// Whether we have an open frame in the output.
is_frame_open: bool,
/// Whether we have an frame closed in the output.
data_to_frame_written: bool,
/// The frame information to be used in this encoder.
frame_info: FrameInfo,
}
Expand Down Expand Up @@ -140,6 +142,7 @@ impl<W: io::Write> FrameEncoder<W> {
content_len: 0,
dst: Vec::new(),
is_frame_open: false,
data_to_frame_written: false,
frame_info,
src_start: 0,
src_end: 0,
Expand Down Expand Up @@ -169,8 +172,15 @@ impl<W: io::Write> FrameEncoder<W> {
/// terminator.
pub fn try_finish(&mut self) -> Result<(), Error> {
match self.flush() {
Ok(()) if self.is_frame_open => self.end_frame(),
Ok(()) => Ok(()),
Ok(()) => {
// Empty input special case
if !self.is_frame_open && !self.data_to_frame_written {
self.begin_frame(0)?;
}
self.end_frame()?;
self.data_to_frame_written = true;
Ok(())
}
Err(err) => Err(err.into()),
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn test_roundtrip(bytes: impl AsRef<[u8]>) {
let mut frame_info = lz4_flex::frame::FrameInfo::new();
frame_info.block_mode = *bm;
let compressed_flex = lz4_flex_frame_compress_with(frame_info, bytes).unwrap();
dbg!(&compressed_flex);
let decompressed = lz4_flex_frame_decompress(&compressed_flex).unwrap();
assert_eq!(decompressed, bytes);
}
Expand Down

0 comments on commit b6774ac

Please sign in to comment.