am 95bc256f: Add a flag to readDictioanryBinary in DictDecoder.

* commit '95bc256f419e9e47c26dfefd1ac31266dac2f344':
  Add a flag to readDictioanryBinary in DictDecoder.
main
Yuichiro Hanada 2013-09-11 02:27:33 -07:00 committed by Android Git Automerger
commit 811e37b37b
6 changed files with 13 additions and 8 deletions

View File

@ -54,10 +54,13 @@ public interface DictDecoder {
* 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.
* @param deleteDictIfBroken a flag indicating whether this method should remove the broken
* dictionary or not.
* @return the created (or merged) dictionary.
*/
@UsedForTesting
public FusionDictionary readDictionaryBinary(final FusionDictionary dict)
public FusionDictionary readDictionaryBinary(final FusionDictionary dict,
final boolean deleteDictIfBroken)
throws FileNotFoundException, IOException, UnsupportedFormatException;
/**

View File

@ -306,7 +306,8 @@ public class Ver3DictDecoder implements DictDecoder {
}
@Override
public FusionDictionary readDictionaryBinary(final FusionDictionary dict)
public FusionDictionary readDictionaryBinary(final FusionDictionary dict,
final boolean deleteDictIfBroken)
throws FileNotFoundException, IOException, UnsupportedFormatException {
if (mDictBuffer == null) {
openDictBuffer();
@ -315,13 +316,13 @@ public class Ver3DictDecoder implements DictDecoder {
return BinaryDictDecoderUtils.readDictionaryBinary(this, dict);
} catch (IOException e) {
Log.e(TAG, "The dictionary " + mDictionaryBinaryFile.getName() + " is broken.", e);
if (!mDictionaryBinaryFile.delete()) {
if (deleteDictIfBroken && !mDictionaryBinaryFile.delete()) {
Log.e(TAG, "Failed to delete the broken dictionary.");
}
throw e;
} catch (UnsupportedFormatException e) {
Log.e(TAG, "The dictionary " + mDictionaryBinaryFile.getName() + " is broken.", e);
if (!mDictionaryBinaryFile.delete()) {
if (deleteDictIfBroken && !mDictionaryBinaryFile.delete()) {
Log.e(TAG, "Failed to delete the broken dictionary.");
}
throw e;

View File

@ -289,7 +289,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
dictDecoder.openDictBuffer();
assertNotNull(dictDecoder.getDictBuffer());
now = System.currentTimeMillis();
dict = dictDecoder.readDictionaryBinary(null);
dict = dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */);
diff = System.currentTimeMillis() - now;
} catch (IOException e) {
Log.e(TAG, "IOException while reading dictionary", e);

View File

@ -192,7 +192,7 @@ public final class BinaryDictOffdeviceUtils {
System.out.println("Packaging : " + decodedSpec.describeChain());
System.out.println("Uncompressed size : " + decodedSpec.mFile.length());
}
return dictDecoder.readDictionaryBinary(null);
return dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */);
}
}
} catch (IOException e) {

View File

@ -268,7 +268,7 @@ public class DictionaryMaker {
throws FileNotFoundException, IOException, UnsupportedFormatException {
final File file = new File(binaryFilename);
final DictDecoder dictDecoder = new Ver3DictDecoder(file);
return dictDecoder.readDictionaryBinary(null);
return dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */);
}
/**

View File

@ -71,7 +71,8 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
assertEquals("Wrong decode spec", 3, decodeSpec.mDecoderSpec.size());
final DictDecoder dictDecoder = new Ver3DictDecoder(decodeSpec.mFile);
final FusionDictionary resultDict = dictDecoder.readDictionaryBinary(
null /* dict : an optional dictionary to add words to, or null */);
null /* dict : an optional dictionary to add words to, or null */,
false /* deleteDictIfBroken */);
assertEquals("Dictionary can't be read back correctly",
FusionDictionary.findWordInTree(resultDict.mRootNodeArray, "foo").getFrequency(),
TEST_FREQ);