mirror of
https://github.com/xerial/snappy-java.git
synced 2025-04-08 19:35:08 +02:00
Make close() idempotent (fixes #107).
This commit is contained in:
parent
c5a3b102bc
commit
2b6c8dc896
@ -69,6 +69,7 @@ public class SnappyOutputStream extends OutputStream {
|
|||||||
protected final byte[] outputBuffer;
|
protected final byte[] outputBuffer;
|
||||||
private int inputCursor = 0;
|
private int inputCursor = 0;
|
||||||
private int outputCursor = 0;
|
private int outputCursor = 0;
|
||||||
|
private boolean closed;
|
||||||
|
|
||||||
public SnappyOutputStream(OutputStream out) {
|
public SnappyOutputStream(OutputStream out) {
|
||||||
this(out, DEFAULT_BLOCK_SIZE);
|
this(out, DEFAULT_BLOCK_SIZE);
|
||||||
@ -320,10 +321,14 @@ public class SnappyOutputStream extends OutputStream {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
|
if (closed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
flush();
|
flush();
|
||||||
out.close();
|
out.close();
|
||||||
} finally {
|
} finally {
|
||||||
|
closed = true;
|
||||||
inputBufferAllocator.release(inputBuffer);
|
inputBufferAllocator.release(inputBuffer);
|
||||||
outputBufferAllocator.release(outputBuffer);
|
outputBufferAllocator.release(outputBuffer);
|
||||||
}
|
}
|
||||||
|
@ -187,8 +187,10 @@ public class SnappyOutputStreamTest
|
|||||||
os3.close();
|
os3.close();
|
||||||
SnappyInputStream in2 = new SnappyInputStream(new ByteArrayInputStream(ba2.toByteArray()));
|
SnappyInputStream in2 = new SnappyInputStream(new ByteArrayInputStream(ba2.toByteArray()));
|
||||||
assertEquals(2, in2.read());
|
assertEquals(2, in2.read());
|
||||||
|
in2.close();
|
||||||
SnappyInputStream in3 = new SnappyInputStream(new ByteArrayInputStream(ba3.toByteArray()));
|
SnappyInputStream in3 = new SnappyInputStream(new ByteArrayInputStream(ba3.toByteArray()));
|
||||||
assertEquals(3, in3.read());
|
assertEquals(3, in3.read());
|
||||||
|
in3.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user