From 094b80c321732636f32733cc72f05ac3deffbe88 Mon Sep 17 00:00:00 2001 From: Zhai Can Date: Fri, 22 Sep 2023 19:46:56 +0800 Subject: [PATCH] ensure all further `read` calls return 0 hurry fix for #8 --- src/read.rs | 8 ++++---- tests/test.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/read.rs b/src/read.rs index bee3e45..e206f86 100644 --- a/src/read.rs +++ b/src/read.rs @@ -99,6 +99,7 @@ where if self.buffer_pos == self.buffer_len { // when the underlying `reader` reaches EOF and also // the buffer maintained by this struct is empty, it's all the end + // TODO: inconsistency EOF mark with `read::Bz3Decoder` if self.eof { return Ok(0); } @@ -267,11 +268,10 @@ where R: Read, { fn read(&mut self, buf: &mut [u8]) -> io::Result { + if self.eof { + return Ok(0); + } if self.buffer_pos == self.buffer_len { - if self.eof { - return Ok(0); - } - self.buffer_pos = 0; // re-fill the buffer match self.decompress_block() { diff --git a/tests/test.rs b/tests/test.rs index a087a67..5964923 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,3 +1,5 @@ +extern crate core; + use std::fmt::Write as _; use std::io::{self, Cursor, Read, Write}; @@ -86,6 +88,29 @@ fn test_compressing_and_decompressing_small_input() { }; assert_eq!(input, decompressed); + + // Input to be compressed and decompressed + let input: &[u8] = &[1, 2, 3]; + + let compressed = { + let mut output = vec![]; + io::copy( + &mut read::Bz3Encoder::new(input, 100 * KB).unwrap(), + &mut output, + ) + .unwrap(); + + output + }; + + let decompressed = { + let mut output = vec![]; + io::copy(&mut &*compressed, &mut write::Bz3Decoder::new(&mut output)).unwrap(); + + output + }; + + assert_eq!(input, decompressed); } #[test]