Fix possible NPE.

Bug: 11933670
Change-Id: Ib0ce853e691bdadffb73874ea9f188cfbec8bc93
main
Keisuke Kuroyanagi 2014-01-06 17:56:49 +09:00
parent 3f8c6b8c54
commit 8214a8c2cf
3 changed files with 19 additions and 2 deletions

View File

@ -643,6 +643,10 @@ public final class BinaryDictDecoderUtils {
* @return true if it's a binary dictionary, false otherwise * @return true if it's a binary dictionary, false otherwise
*/ */
public static boolean isBinaryDictionary(final File file) { public static boolean isBinaryDictionary(final File file) {
return FormatSpec.getDictDecoder(file).hasValidRawBinaryDictionary(); final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file);
if (dictDecoder == null) {
return false;
}
return dictDecoder.hasValidRawBinaryDictionary();
} }
} }

View File

@ -309,6 +309,7 @@ public final class BinaryDictIOUtils {
* @param file The file to read. * @param file The file to read.
* @param offset The offset in the file where to start reading the data. * @param offset The offset in the file where to start reading the data.
* @param length The length of the data file. * @param length The length of the data file.
* @return the header of the specified dictionary file.
*/ */
private static FileHeader getDictionaryFileHeader( private static FileHeader getDictionaryFileHeader(
final File file, final long offset, final long length) final File file, final long offset, final long length)
@ -330,6 +331,9 @@ public final class BinaryDictIOUtils {
} }
} }
); );
if (dictDecoder == null) {
return null;
}
return dictDecoder.readHeader(); return dictDecoder.readHeader();
} }

View File

@ -282,10 +282,19 @@ public class DictionaryInfoUtils {
return BinaryDictIOUtils.getDictionaryFileHeaderOrNull(file, 0, file.length()); return BinaryDictIOUtils.getDictionaryFileHeaderOrNull(file, 0, file.length());
} }
/**
* Returns information of the dictionary.
*
* @param fileAddress the asset dictionary file address.
* @return information of the specified dictionary.
*/
private static DictionaryInfo createDictionaryInfoFromFileAddress( private static DictionaryInfo createDictionaryInfoFromFileAddress(
final AssetFileAddress fileAddress) { final AssetFileAddress fileAddress) {
final FileHeader header = BinaryDictIOUtils.getDictionaryFileHeaderOrNull( final FileHeader header = BinaryDictIOUtils.getDictionaryFileHeaderOrNull(
new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength); new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength);
if (header == null) {
return null;
}
final String id = header.getId(); final String id = header.getId();
final Locale locale = LocaleUtils.constructLocaleFromString(header.getLocaleString()); final Locale locale = LocaleUtils.constructLocaleFromString(header.getLocaleString());
final String description = header.getDescription(); final String description = header.getDescription();
@ -328,7 +337,7 @@ public class DictionaryInfoUtils {
// Protect against cases of a less-specific dictionary being found, like an // Protect against cases of a less-specific dictionary being found, like an
// en dictionary being used for an en_US locale. In this case, the en dictionary // en dictionary being used for an en_US locale. In this case, the en dictionary
// should be used for en_US but discounted for listing purposes. // should be used for en_US but discounted for listing purposes.
if (!dictionaryInfo.mLocale.equals(locale)) continue; if (dictionaryInfo == null || !dictionaryInfo.mLocale.equals(locale)) continue;
addOrUpdateDictInfo(dictList, dictionaryInfo); addOrUpdateDictInfo(dictList, dictionaryInfo);
} }
} }