Skip to content

Commit

Permalink
Ensure reader buffer is flushed when extracting reader
Browse files Browse the repository at this point in the history
After the decoder stream has yielded all of the uncompressed data, it is possible for the input stream to still not be fully consumed.  This means if we extract the inner stream at this point, it will not be pointing to the end of the compressed data.

From the [zstd documentation](https://facebook.github.io/zstd/zstd_manual.html#Chapter9) for `ZSTD_decompressStream`:

> But if `output.pos == output.size`, there might be some data left within internal buffers.
> In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.

This is only necessary if the caller wants the stream back, so at that point we can force an additional call to `ZSTD_decompressStream` by reading to a zero-length buffer.
  • Loading branch information
markbt committed Oct 24, 2023
1 parent 5161ab0 commit ea02a36
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/stream/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ impl<'a, R: BufRead> Decoder<'a, R> {
///
/// Calling `finish()` is not *required* after reading a stream -
/// just use it if you need to get the `Read` back.
pub fn finish(self) -> R {
pub fn finish(mut self) -> R {
// Ensure the input buffers have been flushed by reading to a zero-length buffer.
let _ = self.reader.read(&mut [0; 0]);
self.reader.into_inner()
}

Expand Down

0 comments on commit ea02a36

Please sign in to comment.