Apache Solr open-source search software
make sure we close all in/output in the case of an error
Simon Willnauer7190c9ed301b
diff --git a/lucene/src/java/org/apache/lucene/index/values/Bytes.java b/lucene/src/java/org/apache/lucene/index/values/Bytes.javaindex b254bc887c9..c7bdf3037a6 100644--- a/lucene/src/java/org/apache/lucene/index/values/Bytes.java+++ b/lucene/src/java/org/apache/lucene/index/values/Bytes.java@@ -35,6 +35,7 @@ import org.apache.lucene.util.AttributeSource;import org.apache.lucene.util.ByteBlockPool;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.CodecUtil;+import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.PagedBytes;/**@@ -353,16 +354,23 @@ public final class Bytes {super(bytesUsed);this.id = id;this.pool = pool;- datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",+ datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",DATA_EXTENSION));+ boolean success = false;+ try {CodecUtil.writeHeader(datOut, codecName, version);-- if (initIndex) {- idxOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",- INDEX_EXTENSION));- CodecUtil.writeHeader(idxOut, codecName, version);- } else {- idxOut = null;+ if (initIndex) {+ idxOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",+ INDEX_EXTENSION));+ CodecUtil.writeHeader(idxOut, codecName, version);+ } else {+ idxOut = null;+ }+ success = true;+ } finally {+ if (!success) {+ IOUtils.closeSafely(true, datOut, idxOut);+ }}}@@ -376,14 +384,10 @@ public final class Bytes {@Overridepublic void finish(int docCount) throws IOException {try {- datOut.close();+ IOUtils.closeSafely(false, datOut, idxOut);} finally {- try {- if (idxOut != null)- idxOut.close();- } finally {- if (pool != null)- pool.reset();+ if (pool != null) {+ pool.reset();}}}diff --git a/lucene/src/java/org/apache/lucene/index/values/Floats.java b/lucene/src/java/org/apache/lucene/index/values/Floats.javaindex 588d1507fa3..468c08c4510 100644--- a/lucene/src/java/org/apache/lucene/index/values/Floats.java+++ b/lucene/src/java/org/apache/lucene/index/values/Floats.java@@ -29,6 +29,7 @@ import org.apache.lucene.store.IndexOutput;import org.apache.lucene.util.AttributeSource;import org.apache.lucene.util.CodecUtil;import org.apache.lucene.util.FloatsRef;+import org.apache.lucene.util.IOUtils;/*** Exposes {@link Writer} and reader ({@link Source}) for 32 bit and 64 bit@@ -82,9 +83,17 @@ public class Floats {this.precision = (byte) precision;datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",Writer.DATA_EXTENSION));- CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT);- assert datOut.getFilePointer() == CodecUtil.headerLength(CODEC_NAME);- datOut.writeByte(this.precision);+ boolean success = false;+ try {+ CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT);+ assert datOut.getFilePointer() == CodecUtil.headerLength(CODEC_NAME);+ datOut.writeByte(this.precision);+ success = true;+ } finally {+ if (!success) {+ IOUtils.closeSafely(true, datOut);+ }+ }}diff --git a/lucene/src/java/org/apache/lucene/index/values/PackedIntsImpl.java b/lucene/src/java/org/apache/lucene/index/values/PackedIntsImpl.javaindex 4c8bd632d4f..5c61a93c5df 100644--- a/lucene/src/java/org/apache/lucene/index/values/PackedIntsImpl.java+++ b/lucene/src/java/org/apache/lucene/index/values/PackedIntsImpl.java@@ -27,6 +27,7 @@ import org.apache.lucene.store.IndexOutput;import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.AttributeSource;import org.apache.lucene.util.CodecUtil;+import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LongsRef;import org.apache.lucene.util.OpenBitSet;import org.apache.lucene.util.RamUsageEstimator;@@ -62,11 +63,21 @@ class PackedIntsImpl {super(bytesUsed);datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",DATA_EXTENSION));- CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT);- this.id = id;- docToValue = new long[1];- bytesUsed.addAndGet(RamUsageEstimator.NUM_BYTES_LONG); // TODO the bitset- // needs memory too+ boolean success = false;+ try {+ CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT);+ this.id = id;+ docToValue = new long[1];+ bytesUsed.addAndGet(RamUsageEstimator.NUM_BYTES_LONG); // TODO the+ // bitset+ // needs memory+ // too+ success = true;+ } finally {+ if (!success) {+ datOut.close();+ }+ }}@Override@@ -168,7 +179,15 @@ class PackedIntsImpl {protected IntsReader(Directory dir, String id) throws IOException {datIn = dir.openInput(IndexFileNames.segmentFileName(id, "",Writer.DATA_EXTENSION));- CodecUtil.checkHeader(datIn, CODEC_NAME, VERSION_START, VERSION_START);+ boolean success = false;+ try {+ CodecUtil.checkHeader(datIn, CODEC_NAME, VERSION_START, VERSION_START);+ success = true;+ } finally {+ if (!success) {+ IOUtils.closeSafely(true, datIn);+ }+ }}/**