From e301085a70c9e5816fd98fb7d0fec3b2790b9359 Mon Sep 17 00:00:00 2001 From: Yuichiro Hanada Date: Wed, 21 Aug 2013 21:15:21 +0900 Subject: [PATCH] Move findWordByBinaryDictReader to BinaryDictIOUtilsTests. Change-Id: I443238fd816dea9650dcbbeb3ea757f9674fa52f --- .../makedict/BinaryDictEncoderUtils.java | 4 +-- .../latin/makedict/BinaryDictIOUtils.java | 25 +------------------ .../DynamicPredictionDictionaryBase.java | 22 +++------------- .../latin/utils/UserHistoryDictIOUtils.java | 7 +++--- .../makedict/BinaryDictIOUtilsTests.java | 24 +++++++++++++++++- .../utils/UserHistoryDictIOUtilsTests.java | 13 +++------- 6 files changed, 36 insertions(+), 59 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java index 3fe3ae6ce..71c1d425c 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java @@ -911,13 +911,11 @@ public class BinaryDictEncoderUtils { /** * Dumps a FusionDictionary to a file. * - * This is the public entry point to write a dictionary to a file. - * * @param destination the stream to write the binary data to. * @param dict the dictionary to write. * @param formatOptions file format options. */ - public static void writeDictionaryBinary(final OutputStream destination, + /* package */ static void writeDictionaryBinary(final OutputStream destination, final FusionDictionary dict, final FormatOptions formatOptions) throws IOException, UnsupportedFormatException { diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java index 8e1e14e9e..9f8842c9f 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java @@ -375,7 +375,7 @@ public final class BinaryDictIOUtils { * @param info the char group info to be written. * @return the size written, in bytes. */ - public static int writeCharGroup(final OutputStream destination, final CharGroupInfo info) + private static int writeCharGroup(final OutputStream destination, final CharGroupInfo info) throws IOException { int size = FormatSpec.GROUP_FLAGS_SIZE; destination.write((byte)info.mFlags); @@ -507,29 +507,6 @@ public final class BinaryDictIOUtils { return size + FormatSpec.FORWARD_LINK_ADDRESS_SIZE; } - /** - * Find a word using the Ver3DictDecoder. - * - * @param dictDecoder the dict reader - * @param word the word searched - * @return the found group - * @throws IOException - * @throws UnsupportedFormatException - */ - @UsedForTesting - public static CharGroupInfo findWordByBinaryDictReader(final Ver3DictDecoder dictDecoder, - final String word) throws IOException, UnsupportedFormatException { - int position = getTerminalPosition(dictDecoder, word); - final DictBuffer dictBuffer = dictDecoder.getDictBuffer(); - if (position != FormatSpec.NOT_VALID_WORD) { - dictBuffer.position(0); - final FileHeader header = dictDecoder.readHeader(); - dictBuffer.position(position); - return dictDecoder.readPtNode(position, header.mFormatOptions); - } - return null; - } - private static final int HEADER_READING_BUFFER_SIZE = 16384; /** * Convenience method to read the header of a binary file. diff --git a/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java b/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java index b565b2f9f..a41b95106 100644 --- a/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java +++ b/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java @@ -28,8 +28,10 @@ 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.DictEncoder; import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; import com.android.inputmethod.latin.makedict.Ver3DictDecoder; +import com.android.inputmethod.latin.makedict.Ver3DictEncoder; import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils; @@ -40,7 +42,6 @@ import com.android.inputmethod.latin.utils.UserHistoryForgettingCurveUtils.Forge import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicReference; @@ -332,24 +333,9 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona final String fileName = mDynamicPredictionDictionary.getDictionaryFileName(); final File file = new File(mContext.getFilesDir(), fileName); - FileOutputStream out = null; - try { - out = new FileOutputStream(file); - UserHistoryDictIOUtils.writeDictionaryBinary(out, this, mBigramList, VERSION3); - out.flush(); - out.close(); - } catch (IOException e) { - Log.e(TAG, "IO Exception while writing file", e); - } finally { - if (out != null) { - try { - out.close(); - } catch (IOException e) { - // ignore - } - } - } + final DictEncoder dictEncoder = new Ver3DictEncoder(file); + UserHistoryDictIOUtils.writeDictionary(dictEncoder, this, mBigramList, VERSION3); // Save the timestamp after we finish writing the binary dictionary. Settings.writeLastUserHistoryWriteTime(mPrefs, mLocale); diff --git a/java/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtils.java b/java/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtils.java index 771db3a47..9d3d8a5da 100644 --- a/java/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtils.java @@ -19,8 +19,8 @@ package com.android.inputmethod.latin.utils; import android.util.Log; import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.makedict.BinaryDictEncoderUtils; import com.android.inputmethod.latin.makedict.BinaryDictIOUtils; +import com.android.inputmethod.latin.makedict.DictEncoder; import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; import com.android.inputmethod.latin.makedict.FusionDictionary; import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray; @@ -30,7 +30,6 @@ import com.android.inputmethod.latin.makedict.Ver3DictDecoder; import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList; import java.io.IOException; -import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -57,12 +56,12 @@ public final class UserHistoryDictIOUtils { /** * Writes dictionary to file. */ - public static void writeDictionaryBinary(final OutputStream destination, + public static void writeDictionary(final DictEncoder dictEncoder, final BigramDictionaryInterface dict, final UserHistoryDictionaryBigramList bigrams, final FormatOptions formatOptions) { final FusionDictionary fusionDict = constructFusionDictionary(dict, bigrams); try { - BinaryDictEncoderUtils.writeDictionaryBinary(destination, fusionDict, formatOptions); + dictEncoder.writeDictionary(fusionDict, formatOptions); Log.d(TAG, "end writing"); } catch (IOException e) { Log.e(TAG, "IO exception while writing file", e); diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java index b13b5a15f..bfdc0407a 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java @@ -161,13 +161,35 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { return position; } + /** + * Find a word using the Ver3DictDecoder. + * + * @param dictDecoder the dict decoder + * @param word the word searched + * @return the found group + * @throws IOException + * @throws UnsupportedFormatException + */ + private static CharGroupInfo findWordByBinaryDictReader(final Ver3DictDecoder dictDecoder, + final String word) throws IOException, UnsupportedFormatException { + int position = BinaryDictIOUtils.getTerminalPosition(dictDecoder, word); + final DictBuffer dictBuffer = dictDecoder.getDictBuffer(); + if (position != FormatSpec.NOT_VALID_WORD) { + dictBuffer.position(0); + final FileHeader header = dictDecoder.readHeader(); + dictBuffer.position(position); + return dictDecoder.readPtNode(position, header.mFormatOptions); + } + return null; + } + private CharGroupInfo findWordFromFile(final File file, final String word) { final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file); CharGroupInfo info = null; try { dictDecoder.openDictBuffer( new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory()); - info = BinaryDictIOUtils.findWordByBinaryDictReader(dictDecoder, word); + info = findWordByBinaryDictReader(dictDecoder, word); } catch (IOException e) { } catch (UnsupportedFormatException e) { } diff --git a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java index eca12c0d8..7b3a01ccf 100644 --- a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java @@ -21,17 +21,18 @@ import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.LargeTest; import android.util.Log; +import com.android.inputmethod.latin.makedict.DictEncoder; import com.android.inputmethod.latin.makedict.FormatSpec; import com.android.inputmethod.latin.makedict.FusionDictionary; import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; import com.android.inputmethod.latin.makedict.Ver3DictDecoder; +import com.android.inputmethod.latin.makedict.Ver3DictEncoder; import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.BigramDictionaryInterface; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -136,14 +137,8 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase private void writeDictToFile(final File file, final UserHistoryDictionaryBigramList bigramList) { - try { - final FileOutputStream out = new FileOutputStream(file); - UserHistoryDictIOUtils.writeDictionaryBinary(out, this, bigramList, FORMAT_OPTIONS); - out.flush(); - out.close(); - } catch (IOException e) { - Log.e(TAG, "IO exception while writing file", e); - } + final DictEncoder dictEncoder = new Ver3DictEncoder(file); + UserHistoryDictIOUtils.writeDictionary(dictEncoder, this, bigramList, FORMAT_OPTIONS); } private void readDictFromFile(final File file, final OnAddWordListener listener) {