Skip to content

Commit

Permalink
Fix ZstdOutputStream corruption on double close
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mszabo-wikia committed Jan 5, 2024
1 parent 19976e2 commit b68f4fa
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/io/airlift/compress/zstd/ZstdOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b68f4fa

Please sign in to comment.