am d93eb680: Merge "Use BinaryDictionaryUtils to read dictionary header."

* commit 'd93eb680c463f6258f8b9e31bb379545b8679357':
  Use BinaryDictionaryUtils to read dictionary header.
main
Keisuke Kuroyanagi 2014-03-05 23:34:14 -08:00 committed by Android Git Automerger
commit 2cb31a0f23
7 changed files with 48 additions and 57 deletions

View File

@ -199,7 +199,6 @@ public final class BinaryDictionary extends Dictionary {
return true;
}
@UsedForTesting
public DictionaryHeader getHeader() throws UnsupportedFormatException {
if (mNativeDict == 0) {
return null;

View File

@ -21,10 +21,9 @@ import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.util.Log;
import com.android.inputmethod.latin.makedict.DictDecoder;
import com.android.inputmethod.latin.makedict.DictionaryHeader;
import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.DictionaryInfoUtils;
import com.android.inputmethod.latin.utils.LocaleUtils;
@ -226,12 +225,10 @@ final public class BinaryDictionaryGetter {
// ## HACK ## we prevent usage of a dictionary before version 18. The reason for this is, since
// those do not include whitelist entries, the new code with an old version of the dictionary
// would lose whitelist functionality.
private static boolean hackCanUseDictionaryFile(final Locale locale, final File f) {
private static boolean hackCanUseDictionaryFile(final Locale locale, final File file) {
try {
// Read the version of the file
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(f, 0, f.length());
final DictionaryHeader header = dictDecoder.readHeader();
final DictionaryHeader header = BinaryDictionaryUtils.getHeader(file);
final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY);
if (null == version) {
// No version in the options : the format is unexpected

View File

@ -222,54 +222,6 @@ public final class BinaryDictIOUtils {
return countSize;
}
private static final int HEADER_READING_BUFFER_SIZE = 16384;
/**
* Convenience method to read the header of a binary file.
*
* This is quite resource intensive - don't call when performance is critical.
*
* @param file The file to read.
* @param offset The offset in the file where to start reading the data.
* @param length The length of the data file.
* @return the header of the specified dictionary file.
*/
private static DictionaryHeader getDictionaryFileHeader(
final File file, final long offset, final long length)
throws FileNotFoundException, IOException, UnsupportedFormatException {
final byte[] buffer = new byte[HEADER_READING_BUFFER_SIZE];
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file, offset, length,
new DictDecoder.DictionaryBufferFactory() {
@Override
public DictBuffer getDictionaryBuffer(File file)
throws FileNotFoundException, IOException {
final FileInputStream inStream = new FileInputStream(file);
try {
inStream.skip(offset);
inStream.read(buffer);
return new ByteArrayDictBuffer(buffer);
} finally {
inStream.close();
}
}
});
if (dictDecoder == null) {
return null;
}
return dictDecoder.readHeader();
}
public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file, final long offset,
final long length) {
try {
final DictionaryHeader header = getDictionaryFileHeader(file, offset, length);
return header;
} catch (UnsupportedFormatException e) {
return null;
} catch (IOException e) {
return null;
}
}
/**
* Helper method to hide the actual value of the no children address.
*/

View File

@ -341,6 +341,7 @@ public final class FormatSpec {
return null;
}
@UsedForTesting
public static DictDecoder getDictDecoder(final File dictFile, final long offset,
final long length, final DictionaryBufferFactory factory) {
if (dictFile.isDirectory()) {
@ -351,6 +352,7 @@ public final class FormatSpec {
return null;
}
@UsedForTesting
public static DictDecoder getDictDecoder(final File dictFile, final long offset,
final long length) {
return getDictDecoder(dictFile, offset, length, DictDecoder.USE_READONLY_BYTEBUFFER);

View File

@ -123,6 +123,7 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
private final DictionaryBufferFactory mBufferFactory;
protected DictBuffer mDictBuffer;
@UsedForTesting
/* package */ Ver2DictDecoder(final File file, final long offset, final long length,
final int factoryFlag) {
mDictionaryBinaryFile = file;

View File

@ -17,8 +17,13 @@
package com.android.inputmethod.latin.utils;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.BinaryDictionary;
import com.android.inputmethod.latin.makedict.DictionaryHeader;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.personalization.PersonalizationHelper;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
@ -39,6 +44,26 @@ public final class BinaryDictionaryUtils {
private static native int editDistanceNative(int[] before, int[] after);
private static native int setCurrentTimeForTestNative(int currentTime);
public static DictionaryHeader getHeader(final File dictFile)
throws IOException, UnsupportedFormatException {
return getHeaderWithOffsetAndLength(dictFile, 0 /* offset */, dictFile.length());
}
public static DictionaryHeader getHeaderWithOffsetAndLength(final File dictFile,
final long offset, final long length) throws IOException, UnsupportedFormatException {
// dictType is never used for reading the header. Passing an empty string.
final BinaryDictionary binaryDictionary = new BinaryDictionary(
dictFile.getAbsolutePath(), offset, length,
true /* useFullEditDistance */, null /* locale */, "" /* dictType */,
false /* isUpdatable */);
final DictionaryHeader header = binaryDictionary.getHeader();
binaryDictionary.close();
if (header == null) {
throw new IOException();
}
return header;
}
public static boolean createEmptyDictFile(final String filePath, final long dictVersion,
final Locale locale, final Map<String, String> attributeMap) {
final String[] keyArray = new String[attributeMap.size()];

View File

@ -30,9 +30,11 @@ import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
import com.android.inputmethod.latin.makedict.DictionaryHeader;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
@ -283,7 +285,20 @@ public class DictionaryInfoUtils {
}
public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file) {
return BinaryDictIOUtils.getDictionaryFileHeaderOrNull(file, 0, file.length());
return getDictionaryFileHeaderOrNull(file, 0, file.length());
}
private static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
final long offset, final long length) {
try {
final DictionaryHeader header =
BinaryDictionaryUtils.getHeaderWithOffsetAndLength(file, offset, length);
return header;
} catch (UnsupportedFormatException e) {
return null;
} catch (IOException e) {
return null;
}
}
/**
@ -294,7 +309,7 @@ public class DictionaryInfoUtils {
*/
private static DictionaryInfo createDictionaryInfoFromFileAddress(
final AssetFileAddress fileAddress) {
final DictionaryHeader header = BinaryDictIOUtils.getDictionaryFileHeaderOrNull(
final DictionaryHeader header = getDictionaryFileHeaderOrNull(
new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength);
if (header == null) {
return null;