Add a flag to readDictioanryBinary in DictDecoder.
Change-Id: I356adb72047ebc43c924fbff1ff45e7460508a31main
parent
f7ec77fe2f
commit
95bc256f41
|
@ -54,10 +54,13 @@ public interface DictDecoder {
|
||||||
* which words from the buffer should be added. If it is null, a new dictionary is created.
|
* 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 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.
|
* @return the created (or merged) dictionary.
|
||||||
*/
|
*/
|
||||||
@UsedForTesting
|
@UsedForTesting
|
||||||
public FusionDictionary readDictionaryBinary(final FusionDictionary dict)
|
public FusionDictionary readDictionaryBinary(final FusionDictionary dict,
|
||||||
|
final boolean deleteDictIfBroken)
|
||||||
throws FileNotFoundException, IOException, UnsupportedFormatException;
|
throws FileNotFoundException, IOException, UnsupportedFormatException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -306,7 +306,8 @@ public class Ver3DictDecoder implements DictDecoder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FusionDictionary readDictionaryBinary(final FusionDictionary dict)
|
public FusionDictionary readDictionaryBinary(final FusionDictionary dict,
|
||||||
|
final boolean deleteDictIfBroken)
|
||||||
throws FileNotFoundException, IOException, UnsupportedFormatException {
|
throws FileNotFoundException, IOException, UnsupportedFormatException {
|
||||||
if (mDictBuffer == null) {
|
if (mDictBuffer == null) {
|
||||||
openDictBuffer();
|
openDictBuffer();
|
||||||
|
@ -315,13 +316,13 @@ public class Ver3DictDecoder implements DictDecoder {
|
||||||
return BinaryDictDecoderUtils.readDictionaryBinary(this, dict);
|
return BinaryDictDecoderUtils.readDictionaryBinary(this, dict);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "The dictionary " + mDictionaryBinaryFile.getName() + " is broken.", 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.");
|
Log.e(TAG, "Failed to delete the broken dictionary.");
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
} catch (UnsupportedFormatException e) {
|
} catch (UnsupportedFormatException e) {
|
||||||
Log.e(TAG, "The dictionary " + mDictionaryBinaryFile.getName() + " is broken.", 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.");
|
Log.e(TAG, "Failed to delete the broken dictionary.");
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -289,7 +289,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
dictDecoder.openDictBuffer();
|
dictDecoder.openDictBuffer();
|
||||||
assertNotNull(dictDecoder.getDictBuffer());
|
assertNotNull(dictDecoder.getDictBuffer());
|
||||||
now = System.currentTimeMillis();
|
now = System.currentTimeMillis();
|
||||||
dict = dictDecoder.readDictionaryBinary(null);
|
dict = dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */);
|
||||||
diff = System.currentTimeMillis() - now;
|
diff = System.currentTimeMillis() - now;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "IOException while reading dictionary", e);
|
Log.e(TAG, "IOException while reading dictionary", e);
|
||||||
|
|
|
@ -192,7 +192,7 @@ public final class BinaryDictOffdeviceUtils {
|
||||||
System.out.println("Packaging : " + decodedSpec.describeChain());
|
System.out.println("Packaging : " + decodedSpec.describeChain());
|
||||||
System.out.println("Uncompressed size : " + decodedSpec.mFile.length());
|
System.out.println("Uncompressed size : " + decodedSpec.mFile.length());
|
||||||
}
|
}
|
||||||
return dictDecoder.readDictionaryBinary(null);
|
return dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -268,7 +268,7 @@ public class DictionaryMaker {
|
||||||
throws FileNotFoundException, IOException, UnsupportedFormatException {
|
throws FileNotFoundException, IOException, UnsupportedFormatException {
|
||||||
final File file = new File(binaryFilename);
|
final File file = new File(binaryFilename);
|
||||||
final DictDecoder dictDecoder = new Ver3DictDecoder(file);
|
final DictDecoder dictDecoder = new Ver3DictDecoder(file);
|
||||||
return dictDecoder.readDictionaryBinary(null);
|
return dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -71,7 +71,8 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
|
||||||
assertEquals("Wrong decode spec", 3, decodeSpec.mDecoderSpec.size());
|
assertEquals("Wrong decode spec", 3, decodeSpec.mDecoderSpec.size());
|
||||||
final DictDecoder dictDecoder = new Ver3DictDecoder(decodeSpec.mFile);
|
final DictDecoder dictDecoder = new Ver3DictDecoder(decodeSpec.mFile);
|
||||||
final FusionDictionary resultDict = dictDecoder.readDictionaryBinary(
|
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",
|
assertEquals("Dictionary can't be read back correctly",
|
||||||
FusionDictionary.findWordInTree(resultDict.mRootNodeArray, "foo").getFrequency(),
|
FusionDictionary.findWordInTree(resultDict.mRootNodeArray, "foo").getFrequency(),
|
||||||
TEST_FREQ);
|
TEST_FREQ);
|
||||||
|
|
Loading…
Reference in New Issue