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