ensure output is closed if finish throws exception

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1229937 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2012-01-11 09:12:09 +00:00
parent 7de1e259f1
commit ba60b2f98a
1 changed files with 20 additions and 5 deletions

View File

@ -17,6 +17,7 @@ package org.apache.lucene.codecs.simpletext;
* limitations under the License.
*/
import java.io.Closeable;
import java.io.IOException;
import java.util.Set;
@ -71,7 +72,17 @@ public class SimpleTextNormsConsumer extends PerDocConsumer {
@Override
public void close() throws IOException {
if (writer != null) {
writer.finish();
boolean success = false;
try {
writer.finish();
success = true;
} finally {
if (success) {
IOUtils.close(writer);
} else {
IOUtils.closeWhileHandlingException(writer);
}
}
}
}
@ -181,7 +192,7 @@ public class SimpleTextNormsConsumer extends PerDocConsumer {
return writer;
}
private static class NormsWriter {
private static class NormsWriter implements Closeable{
private final IndexOutput output;
private int numTotalDocs = 0;
@ -253,12 +264,16 @@ public class SimpleTextNormsConsumer extends PerDocConsumer {
}
public void abort() throws IOException {
IOUtils.close(output);
close();
}
public void finish() throws IOException {
finish(numTotalDocs);
IOUtils.close(output);
finish(numTotalDocs);
}
@Override
public void close() throws IOException {
output.close();
}
}