summaryrefslogtreecommitdiffstats
path: root/fsa
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-04-30 10:04:46 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2019-04-30 10:04:46 +0200
commitf07823f00141be73f893f05dc8e7168e6fb281dc (patch)
treea93b5693124167a4a839460cb16938d8b5107020 /fsa
parent7fb9ed41d77810803772f9459a6df5076127555c (diff)
Use final members for thread visibility. And close the InputStream.
Diffstat (limited to 'fsa')
-rw-r--r--fsa/src/main/java/com/yahoo/fsa/FSA.java70
1 files changed, 40 insertions, 30 deletions
diff --git a/fsa/src/main/java/com/yahoo/fsa/FSA.java b/fsa/src/main/java/com/yahoo/fsa/FSA.java
index a0ca7ebee36..692c85528e1 100644
--- a/fsa/src/main/java/com/yahoo/fsa/FSA.java
+++ b/fsa/src/main/java/com/yahoo/fsa/FSA.java
@@ -1,6 +1,7 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fsa;
+import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -315,13 +316,13 @@ public class FSA {
return new Iterator(state);
}
- private boolean _ok = false;
- private MappedByteBuffer _header;
- private MappedByteBuffer _symbol_tab;
- private MappedByteBuffer _state_tab;
- private MappedByteBuffer _data;
- private MappedByteBuffer _phash;
- private Charset _charset;
+ private final boolean _ok;
+ private final MappedByteBuffer _header;
+ private final MappedByteBuffer _symbol_tab;
+ private final MappedByteBuffer _state_tab;
+ private final MappedByteBuffer _data;
+ private final MappedByteBuffer _phash;
+ private final Charset _charset;
/**
* Loads an FSA from a resource file name, which is resolved from the class path of the
@@ -339,50 +340,49 @@ public class FSA {
*/
public static FSA loadFromResource(String resourceFileName,Class loadingClass) {
URL fsaUrl=loadingClass.getResource(resourceFileName);
- if ( ! "file".equals(fsaUrl.getProtocol()))
+ if ( ! "file".equals(fsaUrl.getProtocol())) {
throw new RuntimeException("Could not open non-file url '" + fsaUrl + "' as a file input stream: " +
- "The classloader of " + loadingClass + "' does not return file urls");
+ "The classloader of " + loadingClass + "' does not return file urls");
+ }
return new FSA(fsaUrl.getFile());
}
+ static FileInputStream createInputStream(String filename) {
+ try {
+ return new FileInputStream(filename);
+ } catch (FileNotFoundException e) {
+ throw new IllegalArgumentException("Could not find FSA file '" + filename + "'",e);
+ }
+ }
+
/**
* Loads an FSA from a file using utf-8 encoding
*
- * @throws IllegalArgumentException if the file is not found
+ * @throws FileNotFoundException if the file is not found
*/
public FSA(String filename) {
- init(filename,"utf-8");
+ this(filename,"utf-8");
}
/**
* Loads an FSA from a file using the specified character encoding.
*
- * @throws IllegalArgumentException if the file is not found
+ * @throws FileNotFoundException if the file is not found
*/
public FSA(String filename, String charsetname) {
- init(filename,charsetname);
+ this(createInputStream(filename), charsetname, true);
}
/** Loads an FSA from a file input stream using utf-8 encoding */
- public FSA(FileInputStream filename) {
- init(filename,"utf-8");
- }
-
- /** Loads an FSA from a file input stream using the specified character encoding */
- public FSA(FileInputStream filename, String charsetname) {
- init(filename,charsetname);
+ public FSA(FileInputStream file) {
+ this(file,"utf-8");
}
- private void init(String filename, String charsetname){
- try {
- init(new FileInputStream(filename),charsetname);
- }
- catch (FileNotFoundException e) {
- throw new IllegalArgumentException("Could not find FSA file '" + filename + "'",e);
- }
+ public FSA(FileInputStream file, String charsetname) {
+ this(file, charsetname, false);
}
-
- private void init(FileInputStream file, String charsetname) {
+ /** Loads an FSA from a file input stream using the specified character encoding */
+ private FSA(FileInputStream file, String charsetname, boolean closeInput) {
try {
_charset = Charset.forName(charsetname);
_header = file.getChannel().map(MapMode.READ_ONLY,0,256);
@@ -396,14 +396,24 @@ public class FSA {
_state_tab.order(ByteOrder.LITTLE_ENDIAN);
_data = file.getChannel().map(MapMode.READ_ONLY, 256+5*h_size(), h_data_size());
_data.order(ByteOrder.LITTLE_ENDIAN);
- if(h_has_phash()>0){
+ if (h_has_phash()>0){
_phash = file.getChannel().map(MapMode.READ_ONLY, 256+5*h_size()+h_data_size(), 4*h_size());
_phash.order(ByteOrder.LITTLE_ENDIAN);
+ } else {
+ _phash = null;
}
_ok=true;
}
catch (IOException e) {
throw new RuntimeException("IO error while reading FSA file",e);
+ } finally {
+ if (closeInput) {
+ try {
+ file.close();
+ } catch (IOException e) {
+ // Do nothing
+ }
+ }
}
}