Skip to content

Commit

Permalink
Make close() idempotent (fixes xerial#107).
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosen committed May 14, 2015
1 parent c5a3b10 commit 2b6c8dc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/xerial/snappy/SnappyOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class SnappyOutputStream extends OutputStream {
protected final byte[] outputBuffer;
private int inputCursor = 0;
private int outputCursor = 0;
private boolean closed;

public SnappyOutputStream(OutputStream out) {
this(out, DEFAULT_BLOCK_SIZE);
Expand Down Expand Up @@ -320,10 +321,14 @@ protected void compressInput() throws IOException {
*/
@Override
public void close() throws IOException {
if (closed) {
return;
}
try {
flush();
out.close();
} finally {
closed = true;
inputBufferAllocator.release(inputBuffer);
outputBufferAllocator.release(outputBuffer);
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/xerial/snappy/SnappyOutputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ public void closeShouldBeIdempotent() throws Exception {
os3.close();
SnappyInputStream in2 = new SnappyInputStream(new ByteArrayInputStream(ba2.toByteArray()));
assertEquals(2, in2.read());
in2.close();
SnappyInputStream in3 = new SnappyInputStream(new ByteArrayInputStream(ba3.toByteArray()));
assertEquals(3, in3.read());
in3.close();
}

@Test
Expand Down

0 comments on commit 2b6c8dc

Please sign in to comment.