From 299cd5b1f57bfed43a72bc3390469b104e7f66a9 Mon Sep 17 00:00:00 2001 From: Mark Juggurnauth-Thomas Date: Thu, 19 Oct 2023 09:22:13 +0100 Subject: [PATCH] Ensure reader buffer is flushed when extracting reader 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. --- src/stream/zio/reader.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/stream/zio/reader.rs b/src/stream/zio/reader.rs index 4214bbd5..659a74d8 100644 --- a/src/stream/zio/reader.rs +++ b/src/stream/zio/reader.rs @@ -63,7 +63,13 @@ impl Reader { } /// Returns the inner reader. - pub fn into_inner(self) -> R { + pub fn into_inner(mut self) -> R + where + R: BufRead, + D: Operation, + { + // Ensure the input buffers have been flushed by reading to a zero-length buffer. + let _ = self.read(&mut [0; 0]); self.reader }