You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2013/10/21 14:44:43 UTC

svn commit: r1534135 - /lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/DictionaryUtil.java

Author: uschindler
Date: Mon Oct 21 12:44:42 2013
New Revision: 1534135

URL: http://svn.apache.org/r1534135
Log:
LUCENE-4956: Fix file not found case, add close on finally

Modified:
    lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/DictionaryUtil.java

Modified: lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/DictionaryUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/DictionaryUtil.java?rev=1534135&r1=1534134&r2=1534135&view=diff
==============================================================================
--- lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/DictionaryUtil.java (original)
+++ lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/DictionaryUtil.java Mon Oct 21 12:44:42 2013
@@ -18,6 +18,7 @@ package org.apache.lucene.analysis.ko.di
  */
 
 import java.io.BufferedInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashSet;
@@ -26,6 +27,7 @@ import java.util.Set;
 import org.apache.lucene.codecs.CodecUtil;
 import org.apache.lucene.store.DataInput;
 import org.apache.lucene.store.InputStreamDataInput;
+import org.apache.lucene.util.IOUtils;
 import org.apache.lucene.util.fst.FST;
 
 public class DictionaryUtil {
@@ -57,22 +59,24 @@ public class DictionaryUtil {
       });
 
       readFileToSet(josas,DictionaryResources.FILE_JOSA);
-      
       readFileToSet(eomis,DictionaryResources.FILE_EOMI);
-      
       readFileToSet(prefixs,DictionaryResources.FILE_PREFIX);
-  
       readFileToSet(suffixs,DictionaryResources.FILE_SUFFIX);
       
       InputStream stream = DictionaryResources.class.getResourceAsStream(DictionaryResources.FILE_WORDS_DAT);
-      DataInput dat = new InputStreamDataInput(new BufferedInputStream(stream));
-      CodecUtil.checkHeader(dat, DictionaryResources.FILE_WORDS_DAT, DictionaryResources.DATA_VERSION, DictionaryResources.DATA_VERSION);
-      byte metadata[] = new byte[dat.readByte() * HangulDictionary.RECORD_SIZE];
-      dat.readBytes(metadata, 0, metadata.length);
-      ByteOutputs outputs = ByteOutputs.getSingleton();
-      FST<Byte> fst = new FST<Byte>(dat, outputs);
-      dictionary = new HangulDictionary(fst, metadata);
-      stream.close();
+      if (stream == null)
+        throw new FileNotFoundException(DictionaryResources.FILE_WORDS_DAT);
+      try {
+        DataInput dat = new InputStreamDataInput(new BufferedInputStream(stream));
+        CodecUtil.checkHeader(dat, DictionaryResources.FILE_WORDS_DAT, DictionaryResources.DATA_VERSION, DictionaryResources.DATA_VERSION);
+        byte metadata[] = new byte[dat.readByte() * HangulDictionary.RECORD_SIZE];
+        dat.readBytes(metadata, 0, metadata.length);
+        ByteOutputs outputs = ByteOutputs.getSingleton();
+        FST<Byte> fst = new FST<Byte>(dat, outputs);
+        dictionary = new HangulDictionary(fst, metadata);
+      } finally {
+        IOUtils.closeWhileHandlingException(stream);
+      }
     } catch (IOException e) {
       throw new Error("Cannot load resource",e);
     }