diff --git a/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java b/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java index ffdebb99..f32c0ef6 100644 --- a/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java +++ b/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java @@ -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 { diff --git a/src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java b/src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java index 695e5714..2d47be33 100644 --- a/src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java +++ b/src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java @@ -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; @@ -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(); + } }