Skip to content

Commit

Permalink
ensure all further read calls return 0
Browse files Browse the repository at this point in the history
hurry fix for #8
  • Loading branch information
bczhc committed Sep 22, 2023
1 parent 4c3a8dd commit 094b80c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -267,11 +268,10 @@ where
R: Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
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() {
Expand Down
25 changes: 25 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate core;

use std::fmt::Write as _;
use std::io::{self, Cursor, Read, Write};

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 094b80c

Please sign in to comment.