From b68f4faa31642457fe32df6ae7f3d89926fb5c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Szab=C3=B3?= Date: Fri, 5 Jan 2024 20:14:05 +0100 Subject: [PATCH] Fix ZstdOutputStream corruption on double close ZstdOutputStream will write out the last chunk every time close() is invoked on it, which can cause errors when the output is later decompressed. Per the java.io.Closeable interface documentation, the close() method should have no effect if invoked on an already-closed stream (which is how e.g. the core DeflaterOutputStream behaves as well), so make it a noop if the stream was already closed. --- .../io/airlift/compress/zstd/ZstdOutputStream.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/airlift/compress/zstd/ZstdOutputStream.java b/src/main/java/io/airlift/compress/zstd/ZstdOutputStream.java index 90a2236c..7a9a600f 100644 --- a/src/main/java/io/airlift/compress/zstd/ZstdOutputStream.java +++ b/src/main/java/io/airlift/compress/zstd/ZstdOutputStream.java @@ -133,18 +133,22 @@ private void compressIfNecessary() void finishWithoutClosingSource() throws IOException { - writeChunk(true); - closed = true; + if (!closed) { + writeChunk(true); + closed = true; + } } @Override public void close() throws IOException { - writeChunk(true); + if (!closed) { + writeChunk(true); - closed = true; - outputStream.close(); + closed = true; + outputStream.close(); + } } private void writeChunk(boolean lastChunk)