aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorYuichiro Hanada <yhanada@google.com>2013-08-22 22:43:20 +0900
committerYuichiro Hanada <yhanada@google.com>2013-08-23 20:29:25 +0900
commite9a10ff0f026b5ec458f116afc7a75806574cbcd (patch)
treeb50ff7fc7e7b4a71db9410bfea829905f00817fb /java/src
parentb64157bf4720dfc2aa40ad8e6806459012f81082 (diff)
downloadlatinime-e9a10ff0f026b5ec458f116afc7a75806574cbcd.tar.gz
latinime-e9a10ff0f026b5ec458f116afc7a75806574cbcd.tar.xz
latinime-e9a10ff0f026b5ec458f116afc7a75806574cbcd.zip
Add DictDecoder.readDictionaryBinary.
Bug: 10434720 Change-Id: I14690a6e0f922ed1bab3a4b6c9a457ae84d4c1a4
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java2
-rw-r--r--java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java10
-rw-r--r--java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java27
-rw-r--r--java/src/com/android/inputmethod/latin/makedict/DictDecoder.java20
-rw-r--r--java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java45
-rw-r--r--java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java10
6 files changed, 79 insertions, 35 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
index 8f6b848bb..2b6d983c0 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
@@ -229,8 +229,6 @@ final public class BinaryDictionaryGetter {
try {
// Read the version of the file
final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(f);
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory());
final FileHeader header = dictDecoder.readHeader();
final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY);
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
index b82b41c7d..8b1d60b39 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
@@ -559,17 +559,9 @@ public final class BinaryDictDecoderUtils {
* @return the created (or merged) dictionary.
*/
@UsedForTesting
- public static FusionDictionary readDictionaryBinary(final Ver3DictDecoder dictDecoder,
+ /* package */ static FusionDictionary readDictionaryBinary(final Ver3DictDecoder dictDecoder,
final FusionDictionary dict) throws FileNotFoundException, IOException,
UnsupportedFormatException {
-
- // if the buffer has not been opened, open the buffer with bytebuffer.
- if (dictDecoder.getDictBuffer() == null) dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory());
- if (dictDecoder.getDictBuffer() == null) {
- MakedictLog.e("Cannot open the buffer");
- }
-
// Read header
final FileHeader fileHeader = dictDecoder.readHeader();
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
index 9f8842c9f..8dce1e9a4 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
@@ -521,20 +521,21 @@ public final class BinaryDictIOUtils {
final File file, final long offset, final long length)
throws FileNotFoundException, IOException, UnsupportedFormatException {
final byte[] buffer = new byte[HEADER_READING_BUFFER_SIZE];
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
- dictDecoder.openDictBuffer(new DictDecoder.DictionaryBufferFactory() {
- @Override
- public DictBuffer getDictionaryBuffer(File file)
- throws FileNotFoundException, IOException {
- final FileInputStream inStream = new FileInputStream(file);
- try {
- inStream.read(buffer);
- return new ByteArrayDictBuffer(buffer);
- } finally {
- inStream.close();
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file,
+ new DictDecoder.DictionaryBufferFactory() {
+ @Override
+ public DictBuffer getDictionaryBuffer(File file)
+ throws FileNotFoundException, IOException {
+ final FileInputStream inStream = new FileInputStream(file);
+ try {
+ inStream.read(buffer);
+ return new ByteArrayDictBuffer(buffer);
+ } finally {
+ inStream.close();
+ }
+ }
}
- }
- });
+ );
return dictDecoder.readHeader();
}
diff --git a/java/src/com/android/inputmethod/latin/makedict/DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/DictDecoder.java
index c02097fa7..13a42fabf 100644
--- a/java/src/com/android/inputmethod/latin/makedict/DictDecoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/DictDecoder.java
@@ -44,6 +44,26 @@ public interface DictDecoder {
*/
public CharGroupInfo readPtNode(final int ptNodePos, final FormatOptions formatOptions);
+ /**
+ * Reads a buffer and returns the memory representation of the dictionary.
+ *
+ * This high-level method takes a buffer and reads its contents, populating a
+ * FusionDictionary structure. The optional dict argument is an existing dictionary to
+ * which words from the buffer should be added. If it is null, a new dictionary is created.
+ *
+ * @param dict an optional dictionary to add words to, or null.
+ * @return the created (or merged) dictionary.
+ */
+ @UsedForTesting
+ public FusionDictionary readDictionaryBinary(final FusionDictionary dict)
+ throws FileNotFoundException, IOException, UnsupportedFormatException;
+
+ // Flags for DictionaryBufferFactory.
+ public static final int USE_READONLY_BYTEBUFFER = 0x01000000;
+ public static final int USE_BYTEARRAY = 0x02000000;
+ public static final int USE_WRITABLE_BYTEBUFFER = 0x04000000;
+ public static final int MASK_DICTBUFFER = 0x0F000000;
+
public interface DictionaryBufferFactory {
public DictBuffer getDictionaryBuffer(final File file)
throws FileNotFoundException, IOException;
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
index 494bbfcf2..f655e3051 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
@@ -165,31 +165,53 @@ public class Ver3DictDecoder implements DictDecoder {
}
private final File mDictionaryBinaryFile;
+ private final DictionaryBufferFactory mBufferFactory;
private DictBuffer mDictBuffer;
public Ver3DictDecoder(final File file) {
+ this(file, USE_READONLY_BYTEBUFFER);
+ }
+
+ public Ver3DictDecoder(final File file, final int factoryFlag) {
mDictionaryBinaryFile = file;
mDictBuffer = null;
+
+ if ((factoryFlag & MASK_DICTBUFFER) == USE_READONLY_BYTEBUFFER) {
+ mBufferFactory = new DictionaryBufferFromReadOnlyByteBufferFactory();
+ } else if ((factoryFlag & MASK_DICTBUFFER) == USE_BYTEARRAY) {
+ mBufferFactory = new DictionaryBufferFromByteArrayFactory();
+ } else if ((factoryFlag & MASK_DICTBUFFER) == USE_WRITABLE_BYTEBUFFER) {
+ mBufferFactory = new DictionaryBufferFromWritableByteBufferFactory();
+ } else {
+ mBufferFactory = new DictionaryBufferFromReadOnlyByteBufferFactory();
+ }
+ }
+
+ public Ver3DictDecoder(final File file, final DictionaryBufferFactory factory) {
+ mDictionaryBinaryFile = file;
+ mBufferFactory = factory;
}
- public void openDictBuffer(final DictDecoder.DictionaryBufferFactory factory)
- throws FileNotFoundException, IOException {
- mDictBuffer = factory.getDictionaryBuffer(mDictionaryBinaryFile);
+ public void openDictBuffer() throws FileNotFoundException, IOException {
+ mDictBuffer = mBufferFactory.getDictionaryBuffer(mDictionaryBinaryFile);
}
- public DictBuffer getDictBuffer() {
+ /* package */ DictBuffer getDictBuffer() {
return mDictBuffer;
}
@UsedForTesting
- public DictBuffer openAndGetDictBuffer(final DictDecoder.DictionaryBufferFactory factory)
- throws FileNotFoundException, IOException {
- openDictBuffer(factory);
+ /* package */ DictBuffer openAndGetDictBuffer() throws FileNotFoundException, IOException {
+ openDictBuffer();
return getDictBuffer();
}
@Override
public FileHeader readHeader() throws IOException, UnsupportedFormatException {
+ if (mDictBuffer == null) {
+ openDictBuffer();
+ }
+
final int version = HeaderReader.readVersion(mDictBuffer);
final int optionsFlags = HeaderReader.readOptionFlags(mDictBuffer);
@@ -278,4 +300,13 @@ public class Ver3DictDecoder implements DictDecoder {
return new CharGroupInfo(ptNodePos, addressPointer, flags, characters, frequency,
parentAddress, childrenAddress, shortcutTargets, bigrams);
}
+
+ @Override
+ public FusionDictionary readDictionaryBinary(final FusionDictionary dict)
+ throws FileNotFoundException, IOException, UnsupportedFormatException {
+ if (mDictBuffer == null) {
+ openDictBuffer();
+ }
+ return BinaryDictDecoderUtils.readDictionaryBinary(this, dict);
+ }
}
diff --git a/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java b/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java
index d046da850..d6456a3b9 100644
--- a/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java
+++ b/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java
@@ -28,6 +28,7 @@ import com.android.inputmethod.latin.ExpandableDictionary;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.WordComposer;
+import com.android.inputmethod.latin.makedict.DictDecoder;
import com.android.inputmethod.latin.makedict.DictEncoder;
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
import com.android.inputmethod.latin.makedict.Ver3DictDecoder;
@@ -242,11 +243,12 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
};
// Load the dictionary from binary file
- final Ver3DictDecoder reader = new Ver3DictDecoder(
- new File(getContext().getFilesDir(), fileName));
+ final File dictFile = new File(getContext().getFilesDir(), fileName);
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(dictFile,
+ DictDecoder.USE_BYTEARRAY);
try {
- reader.openDictBuffer(new Ver3DictDecoder.DictionaryBufferFromByteArrayFactory());
- UserHistoryDictIOUtils.readDictionaryBinary(reader, listener);
+ dictDecoder.openDictBuffer();
+ UserHistoryDictIOUtils.readDictionaryBinary(dictDecoder, listener);
} catch (FileNotFoundException e) {
// This is an expected condition: we don't have a user history dictionary for this
// language yet. It will be created sometime later.