Skip to content

Commit

Permalink
Merge pull request #16 from stevenschlansker/double-close
Browse files Browse the repository at this point in the history
Double close causes exception, which violates Closeable contract.
  • Loading branch information
jpountz committed Jul 12, 2013
2 parents 1e9a2bc + 8d65589 commit 7ff7fa0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/java/net/jpountz/lz4/LZ4BlockOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ public void write(byte[] b) throws IOException {

@Override
public void close() throws IOException {
ensureNotFinished();
finish();
out.close();
if (!finished) {
finish();
}
if (out != null) {
out.close();
out = null;
}
}

private void flushBufferedData() throws IOException {
Expand Down
22 changes: 22 additions & 0 deletions src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.zip.Adler32;
import java.util.zip.CRC32;
Expand Down Expand Up @@ -271,4 +272,25 @@ public void testRoundtripEmpty() throws IOException {
testRoundTrip(new byte[0]);
}

@Test
public void testDoubleClose() throws IOException {
final byte[] testBytes = "Testing!".getBytes(Charset.forName("UTF-8"));

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
LZ4BlockOutputStream out = new LZ4BlockOutputStream(bytes);

out.write(testBytes);

out.close();
out.close();

LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(bytes.toByteArray()));
byte[] actual = new byte[testBytes.length];
in.read(actual);

assertArrayEquals(testBytes, actual);

in.close();
in.close();
}
}

0 comments on commit 7ff7fa0

Please sign in to comment.