mirror of https://github.com/apache/lucene.git
Simplify some exception handling with try-with-resources. (#589)
This commit is contained in:
parent
d9d65ab849
commit
74698994a9
|
@ -82,16 +82,15 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
}
|
||||
this.resourcePath = resourcePath;
|
||||
}
|
||||
InputStream mapIS = null, dictIS = null, posIS = null;
|
||||
int[] targetMapOffsets = null, targetMap = null;
|
||||
String[] posDict = null;
|
||||
String[] inflFormDict = null;
|
||||
String[] inflTypeDict = null;
|
||||
ByteBuffer buffer = null;
|
||||
boolean success = false;
|
||||
try {
|
||||
mapIS = getResource(TARGETMAP_FILENAME_SUFFIX);
|
||||
mapIS = new BufferedInputStream(mapIS);
|
||||
try (InputStream mapIS = new BufferedInputStream(getResource(TARGETMAP_FILENAME_SUFFIX));
|
||||
InputStream posIS = new BufferedInputStream(getResource(POSDICT_FILENAME_SUFFIX));
|
||||
// no buffering here, as we load in one large buffer
|
||||
InputStream dictIS = getResource(DICT_FILENAME_SUFFIX)) {
|
||||
DataInput in = new InputStreamDataInput(mapIS);
|
||||
CodecUtil.checkHeader(in, TARGETMAP_HEADER, VERSION, VERSION);
|
||||
targetMap = new int[in.readVInt()];
|
||||
|
@ -115,11 +114,7 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
+ ", sourceId="
|
||||
+ sourceId);
|
||||
targetMapOffsets[sourceId] = targetMap.length;
|
||||
mapIS.close();
|
||||
mapIS = null;
|
||||
|
||||
posIS = getResource(POSDICT_FILENAME_SUFFIX);
|
||||
posIS = new BufferedInputStream(posIS);
|
||||
in = new InputStreamDataInput(posIS);
|
||||
CodecUtil.checkHeader(in, POSDICT_HEADER, VERSION, VERSION);
|
||||
int posSize = in.readVInt();
|
||||
|
@ -138,11 +133,7 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
inflFormDict[j] = null;
|
||||
}
|
||||
}
|
||||
posIS.close();
|
||||
posIS = null;
|
||||
|
||||
dictIS = getResource(DICT_FILENAME_SUFFIX);
|
||||
// no buffering here, as we load in one large buffer
|
||||
in = new InputStreamDataInput(dictIS);
|
||||
CodecUtil.checkHeader(in, DICT_HEADER, VERSION, VERSION);
|
||||
final int size = in.readVInt();
|
||||
|
@ -152,16 +143,7 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
if (read != size) {
|
||||
throw new EOFException("Cannot read whole dictionary");
|
||||
}
|
||||
dictIS.close();
|
||||
dictIS = null;
|
||||
buffer = tmpBuffer.asReadOnlyBuffer();
|
||||
success = true;
|
||||
} finally {
|
||||
if (success) {
|
||||
IOUtils.close(mapIS, posIS, dictIS);
|
||||
} else {
|
||||
IOUtils.closeWhileHandlingException(mapIS, posIS, dictIS);
|
||||
}
|
||||
}
|
||||
|
||||
this.targetMap = targetMap;
|
||||
|
|
|
@ -22,7 +22,6 @@ import java.io.InputStream;
|
|||
import org.apache.lucene.codecs.CodecUtil;
|
||||
import org.apache.lucene.store.DataInput;
|
||||
import org.apache.lucene.store.InputStreamDataInput;
|
||||
import org.apache.lucene.util.IOUtils;
|
||||
|
||||
/** Character category data. */
|
||||
public final class CharacterDefinition {
|
||||
|
@ -69,11 +68,8 @@ public final class CharacterDefinition {
|
|||
public static final byte KANJINUMERIC = (byte) CharacterClass.KANJINUMERIC.ordinal();
|
||||
|
||||
private CharacterDefinition() throws IOException {
|
||||
InputStream is = null;
|
||||
boolean success = false;
|
||||
try {
|
||||
is = BinaryDictionary.getClassResource(getClass(), FILENAME_SUFFIX);
|
||||
is = new BufferedInputStream(is);
|
||||
try (InputStream is =
|
||||
new BufferedInputStream(BinaryDictionary.getClassResource(getClass(), FILENAME_SUFFIX))) {
|
||||
final DataInput in = new InputStreamDataInput(is);
|
||||
CodecUtil.checkHeader(in, HEADER, VERSION, VERSION);
|
||||
in.readBytes(characterCategoryMap, 0, characterCategoryMap.length);
|
||||
|
@ -82,13 +78,6 @@ public final class CharacterDefinition {
|
|||
invokeMap[i] = (b & 0x01) != 0;
|
||||
groupMap[i] = (b & 0x02) != 0;
|
||||
}
|
||||
success = true;
|
||||
} finally {
|
||||
if (success) {
|
||||
IOUtils.close(is);
|
||||
} else {
|
||||
IOUtils.closeWhileHandlingException(is);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -81,12 +81,12 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
}
|
||||
this.resourcePath = resourcePath;
|
||||
}
|
||||
InputStream mapIS = null, dictIS = null, posIS = null;
|
||||
int[] targetMapOffsets, targetMap;
|
||||
ByteBuffer buffer;
|
||||
try {
|
||||
mapIS = getResource(TARGETMAP_FILENAME_SUFFIX);
|
||||
mapIS = new BufferedInputStream(mapIS);
|
||||
try (InputStream mapIS = new BufferedInputStream(getResource(TARGETMAP_FILENAME_SUFFIX));
|
||||
InputStream posIS = new BufferedInputStream(getResource(POSDICT_FILENAME_SUFFIX));
|
||||
// no buffering here, as we load in one large buffer
|
||||
InputStream dictIS = getResource(DICT_FILENAME_SUFFIX)) {
|
||||
DataInput in = new InputStreamDataInput(mapIS);
|
||||
CodecUtil.checkHeader(in, TARGETMAP_HEADER, VERSION, VERSION);
|
||||
targetMap = new int[in.readVInt()];
|
||||
|
@ -110,11 +110,7 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
+ ", sourceId="
|
||||
+ sourceId);
|
||||
targetMapOffsets[sourceId] = targetMap.length;
|
||||
mapIS.close();
|
||||
mapIS = null;
|
||||
|
||||
posIS = getResource(POSDICT_FILENAME_SUFFIX);
|
||||
posIS = new BufferedInputStream(posIS);
|
||||
in = new InputStreamDataInput(posIS);
|
||||
CodecUtil.checkHeader(in, POSDICT_HEADER, VERSION, VERSION);
|
||||
int posSize = in.readVInt();
|
||||
|
@ -122,11 +118,7 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
for (int j = 0; j < posSize; j++) {
|
||||
posDict[j] = POS.resolveTag(in.readByte());
|
||||
}
|
||||
posIS.close();
|
||||
posIS = null;
|
||||
|
||||
dictIS = getResource(DICT_FILENAME_SUFFIX);
|
||||
// no buffering here, as we load in one large buffer
|
||||
in = new InputStreamDataInput(dictIS);
|
||||
CodecUtil.checkHeader(in, DICT_HEADER, VERSION, VERSION);
|
||||
final int size = in.readVInt();
|
||||
|
@ -136,11 +128,7 @@ public abstract class BinaryDictionary implements Dictionary {
|
|||
if (read != size) {
|
||||
throw new EOFException("Cannot read whole dictionary");
|
||||
}
|
||||
dictIS.close();
|
||||
dictIS = null;
|
||||
buffer = tmpBuffer.asReadOnlyBuffer();
|
||||
} finally {
|
||||
IOUtils.closeWhileHandlingException(mapIS, posIS, dictIS);
|
||||
}
|
||||
|
||||
this.targetMap = targetMap;
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.apache.lucene.analysis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
|
@ -35,7 +34,6 @@ import java.util.Set;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
import org.apache.lucene.util.IOUtils;
|
||||
import org.apache.lucene.util.ResourceLoader;
|
||||
import org.apache.lucene.util.ResourceLoaderAware;
|
||||
import org.apache.lucene.util.Version;
|
||||
|
@ -315,19 +313,13 @@ public abstract class AbstractAnalysisFactory {
|
|||
// big to start
|
||||
words = new CharArraySet(files.size() * 10, ignoreCase);
|
||||
for (String file : files) {
|
||||
InputStream stream = null;
|
||||
Reader reader = null;
|
||||
try {
|
||||
stream = loader.openResource(file.trim());
|
||||
CharsetDecoder decoder =
|
||||
StandardCharsets.UTF_8
|
||||
.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
reader = new InputStreamReader(stream, decoder);
|
||||
CharsetDecoder decoder =
|
||||
StandardCharsets.UTF_8
|
||||
.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
try (Reader reader = new InputStreamReader(loader.openResource(file.trim()), decoder); ) {
|
||||
WordlistLoader.getSnowballWordSet(reader, words);
|
||||
} finally {
|
||||
IOUtils.closeWhileHandlingException(reader, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,6 @@ import org.apache.lucene.util.BytesRef;
|
|||
import org.apache.lucene.util.CollectionUtil;
|
||||
import org.apache.lucene.util.Counter;
|
||||
import org.apache.lucene.util.FixedBitSet;
|
||||
import org.apache.lucene.util.IOUtils;
|
||||
import org.apache.lucene.util.IntBlockPool;
|
||||
import org.apache.lucene.util.TimSorter;
|
||||
import org.apache.lucene.util.automaton.CompiledAutomaton;
|
||||
|
@ -127,17 +126,9 @@ final class FreqProxTermsWriter extends TermsHash {
|
|||
};
|
||||
}
|
||||
|
||||
FieldsConsumer consumer = state.segmentInfo.getCodec().postingsFormat().fieldsConsumer(state);
|
||||
boolean success = false;
|
||||
try {
|
||||
try (FieldsConsumer consumer =
|
||||
state.segmentInfo.getCodec().postingsFormat().fieldsConsumer(state)) {
|
||||
consumer.write(fields, norms);
|
||||
success = true;
|
||||
} finally {
|
||||
if (success) {
|
||||
IOUtils.close(consumer);
|
||||
} else {
|
||||
IOUtils.closeWhileHandlingException(consumer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -170,9 +170,8 @@ public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy {
|
|||
|
||||
private synchronized void persist() throws IOException {
|
||||
String fileName = SNAPSHOTS_PREFIX + nextWriteGen;
|
||||
IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT);
|
||||
boolean success = false;
|
||||
try {
|
||||
try (IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT)) {
|
||||
CodecUtil.writeHeader(out, CODEC_NAME, VERSION_CURRENT);
|
||||
out.writeVInt(refCounts.size());
|
||||
for (Entry<Long, Integer> ent : refCounts.entrySet()) {
|
||||
|
@ -182,10 +181,7 @@ public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy {
|
|||
success = true;
|
||||
} finally {
|
||||
if (!success) {
|
||||
IOUtils.closeWhileHandlingException(out);
|
||||
IOUtils.deleteFilesIgnoringExceptions(dir, fileName);
|
||||
} else {
|
||||
IOUtils.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue