Use BinaryDictionaryUtils to read dictionary header.

Bug: 8187060
Bug:13035567
Change-Id: I90a5e0665c367f5a1bd786f4babf0d03e79e68fa
This commit is contained in:
Keisuke Kuroyanagi 2014-03-05 19:42:53 +09:00
parent e784148ae6
commit de36b47d29
7 changed files with 48 additions and 57 deletions

View file

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

View file

@ -21,10 +21,9 @@ import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor; import android.content.res.AssetFileDescriptor;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.makedict.DictDecoder;
import com.android.inputmethod.latin.makedict.DictionaryHeader; 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.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.DictionaryInfoUtils; import com.android.inputmethod.latin.utils.DictionaryInfoUtils;
import com.android.inputmethod.latin.utils.LocaleUtils; 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 // ## 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 // those do not include whitelist entries, the new code with an old version of the dictionary
// would lose whitelist functionality. // 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 { try {
// Read the version of the file // Read the version of the file
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(f, 0, f.length()); final DictionaryHeader header = BinaryDictionaryUtils.getHeader(file);
final DictionaryHeader header = dictDecoder.readHeader();
final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY); final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY);
if (null == version) { if (null == version) {
// No version in the options : the format is unexpected // No version in the options : the format is unexpected

View file

@ -222,54 +222,6 @@ public final class BinaryDictIOUtils {
return countSize; 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. * Helper method to hide the actual value of the no children address.
*/ */

View file

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

View file

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

View file

@ -17,8 +17,13 @@
package com.android.inputmethod.latin.utils; package com.android.inputmethod.latin.utils;
import com.android.inputmethod.annotations.UsedForTesting; 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 com.android.inputmethod.latin.personalization.PersonalizationHelper;
import java.io.File;
import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import java.util.Map; 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 editDistanceNative(int[] before, int[] after);
private static native int setCurrentTimeForTestNative(int currentTime); 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, public static boolean createEmptyDictFile(final String filePath, final long dictVersion,
final Locale locale, final Map<String, String> attributeMap) { final Locale locale, final Map<String, String> attributeMap) {
final String[] keyArray = new String[attributeMap.size()]; 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.R;
import com.android.inputmethod.latin.makedict.BinaryDictIOUtils; import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
import com.android.inputmethod.latin.makedict.DictionaryHeader; import com.android.inputmethod.latin.makedict.DictionaryHeader;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations; import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
@ -283,7 +285,20 @@ public class DictionaryInfoUtils {
} }
public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file) { 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( private static DictionaryInfo createDictionaryInfoFromFileAddress(
final AssetFileAddress fileAddress) { final AssetFileAddress fileAddress) {
final DictionaryHeader header = BinaryDictIOUtils.getDictionaryFileHeaderOrNull( final DictionaryHeader header = getDictionaryFileHeaderOrNull(
new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength); new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength);
if (header == null) { if (header == null) {
return null; return null;