LUCENE-6152: Fix double close bug in OutputStreamIndexOutput

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1648724 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2014-12-31 15:52:01 +00:00
parent 4a6d85790a
commit 2d422a995a
2 changed files with 12 additions and 3 deletions

View File

@ -400,6 +400,9 @@ Bug Fixes
* LUCENE-6124: Fix double-close() problems in codec and store APIs.
(Robert Muir)
* LUCENE-6152: Fix double close problems in OutputStreamIndexOutput.
(Uwe Schindler)
Documentation
* LUCENE-5392: Add/improve analysis package documentation to reflect

View File

@ -30,6 +30,7 @@ public class OutputStreamIndexOutput extends IndexOutput {
private final BufferedOutputStream os;
private long bytesWritten = 0L;
private boolean flushedOnClose = false;
/**
* Creates a new {@link OutputStreamIndexOutput} with the given buffer size.
@ -58,9 +59,14 @@ public class OutputStreamIndexOutput extends IndexOutput {
try (final OutputStream o = os) {
// We want to make sure that os.flush() was running before close:
// BufferedOutputStream may ignore IOExceptions while flushing on close().
// TODO: this is no longer an issue in Java 8:
// http://hg.openjdk.java.net/jdk8/tl/jdk/rev/759aa847dcaf
o.flush();
// We keep this also in Java 8, although it claims to be fixed there,
// because there are more bugs around this! See:
// # https://bugs.openjdk.java.net/browse/JDK-7015589
// # https://bugs.openjdk.java.net/browse/JDK-8054565
if (!flushedOnClose) {
flushedOnClose = true; // set this BEFORE calling flush!
o.flush();
}
}
}