Skip to content

Apache Lucene open-source search software

LUCENE-7547: close the dictionary file so we don't leak file handles

Mike McCandlessbd6c0523c2de
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 9cce9270471..1caaf86a87e 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -60,6 +60,11 @@ New features
* LUCENE-5867: Added BooleanSimilarity. (Robert Muir, Adrien Grand)
+Bug Fixes
+
+* LUCENE-7547: JapaneseTokenizerFactory was failing to close the
+ dictionary file it opened (Markus via Mike McCandless)
+
Improvements
* LUCENE-6824: TermAutomatonQuery now rewrites to TermQuery,
diff --git a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/JapaneseTokenizerFactory.java b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/JapaneseTokenizerFactory.java
index 99ad61b22ae..844684ae1b9 100644
--- a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/JapaneseTokenizerFactory.java
+++ b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/JapaneseTokenizerFactory.java
@@ -127,16 +127,17 @@ public class JapaneseTokenizerFactory extends TokenizerFactory implements Resour
@Override
public void inform(ResourceLoader loader) throws IOException {
if (userDictionaryPath != null) {
- InputStream stream = loader.openResource(userDictionaryPath);
- String encoding = userDictionaryEncoding;
- if (encoding == null) {
- encoding = IOUtils.UTF_8;
- }
- CharsetDecoder decoder = Charset.forName(encoding).newDecoder()
+ try (InputStream stream = loader.openResource(userDictionaryPath)) {
+ String encoding = userDictionaryEncoding;
+ if (encoding == null) {
+ encoding = IOUtils.UTF_8;
+ }
+ CharsetDecoder decoder = Charset.forName(encoding).newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
- Reader reader = new InputStreamReader(stream, decoder);
- userDictionary = UserDictionary.open(reader);
+ Reader reader = new InputStreamReader(stream, decoder);
+ userDictionary = UserDictionary.open(reader);
+ }
} else {
userDictionary = null;
}