Merge "Check the md5 checksum of the copied file."

main
Jean Chalard 2014-05-22 07:38:08 +00:00 committed by Android (Google) Code Review
commit 9b3d55fd1a
2 changed files with 13 additions and 3 deletions

View File

@ -20,7 +20,7 @@ import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
final class MD5Calculator { public final class MD5Calculator {
private MD5Calculator() {} // This helper class is not instantiable private MD5Calculator() {} // This helper class is not instantiable
public static String checksum(final InputStream in) throws IOException { public static String checksum(final InputStream in) throws IOException {

View File

@ -28,6 +28,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.dictionarypack.DictionaryPackConstants; import com.android.inputmethod.dictionarypack.DictionaryPackConstants;
import com.android.inputmethod.dictionarypack.MD5Calculator;
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.DictionaryInfoUtils.DictionaryInfo; import com.android.inputmethod.latin.utils.DictionaryInfoUtils.DictionaryInfo;
@ -38,6 +39,7 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -218,7 +220,8 @@ public final class BinaryDictionaryFileDumper {
* and creating it (and its containing directory) if necessary. * and creating it (and its containing directory) if necessary.
*/ */
private static void cacheWordList(final String wordlistId, final String locale, private static void cacheWordList(final String wordlistId, final String locale,
final ContentProviderClient providerClient, final Context context) { final String rawChecksum, final ContentProviderClient providerClient,
final Context context) {
final int COMPRESSED_CRYPTED_COMPRESSED = 0; final int COMPRESSED_CRYPTED_COMPRESSED = 0;
final int CRYPTED_COMPRESSED = 1; final int CRYPTED_COMPRESSED = 1;
final int COMPRESSED_CRYPTED = 2; final int COMPRESSED_CRYPTED = 2;
@ -300,6 +303,13 @@ public final class BinaryDictionaryFileDumper {
checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream); checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream);
bufferedOutputStream.flush(); bufferedOutputStream.flush();
bufferedOutputStream.close(); bufferedOutputStream.close();
final String actualRawChecksum = MD5Calculator.checksum(
new BufferedInputStream(new FileInputStream(outputFile)));
Log.i(TAG, "Computed checksum for downloaded dictionary. Expected = " + rawChecksum
+ " ; actual = " + actualRawChecksum);
if (!TextUtils.isEmpty(rawChecksum) && !rawChecksum.equals(actualRawChecksum)) {
throw new IOException("Could not decode the file correctly : checksum differs");
}
final File finalFile = new File(finalFileName); final File finalFile = new File(finalFileName);
finalFile.delete(); finalFile.delete();
if (!outputFile.renameTo(finalFile)) { if (!outputFile.renameTo(finalFile)) {
@ -409,7 +419,7 @@ public final class BinaryDictionaryFileDumper {
final List<WordListInfo> idList = getWordListWordListInfos(locale, context, final List<WordListInfo> idList = getWordListWordListInfos(locale, context,
hasDefaultWordList); hasDefaultWordList);
for (WordListInfo id : idList) { for (WordListInfo id : idList) {
cacheWordList(id.mId, id.mLocale, providerClient, context); cacheWordList(id.mId, id.mLocale, id.mRawChecksum, providerClient, context);
} }
} finally { } finally {
providerClient.release(); providerClient.release();