From bd6c0523c2de09250ff07db6e4a21227bd143ea2 Mon Sep 17 00:00:00 2001 From: Mike McCandless Date: Fri, 11 Nov 2016 13:42:06 -0500 Subject: [PATCH] LUCENE-7547: close the dictionary file so we don't leak file handles --- lucene/CHANGES.txt | 5 +++++ .../analysis/ja/JapaneseTokenizerFactory.java | 17 +++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) 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; }