使用Java为大文件生成MD5非常慢 [英] Very slow to generate MD5 for large file using Java
本文介绍了使用Java为大文件生成MD5非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Java为某些文件生成MD5哈希。我需要为几个文件生成一个MD5,总大小约为1千兆字节。
这是我的代码:
I am using Java to generate the MD5 hash for some files. I need to generate one MD5 for several files with a total size of about 1 gigabyte. Here's my code:
private String generateMD5(SequenceInputStream inputStream){
if(inputStream==null){
return null;
}
MessageDigest md;
try {
int read =0;
byte[] buf = new byte[2048];
md = MessageDigest.getInstance("MD5");
while((read = inputStream.read(buf))>0){
md.update(buf,0,read);
}
byte[] hashValue = md.digest();
return new String(hashValue);
} catch (NoSuchAlgorithmException e) {
return null;
} catch (IOException e) {
return null;
}finally{
try {
if(inputStream!=null)inputStream.close();
} catch (IOException e) {
// ...
}
}
}
这似乎永远存在。
如何提高效率?
This seems to run forever. How can I make it more efficient?
推荐答案
您可能想要使用 Fast MD5 库。它比Java的内置MD5提供程序快得多,并且获取哈希就像这样简单:
You may want to use the Fast MD5 library. It's much faster than Java's built-in MD5 provider and getting a hash is as simple as:
String hash = MD5.asHex(MD5.getHash(new File(filename)));
请注意,速度慢也可能是因为文件I / O速度慢。
Be aware that the slow speed may also be due to slow File I/O.
这篇关于使用Java为大文件生成MD5非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文