我正在使用Java为某些文件生成MD5哈希。我需要为几个文件生成一个MD5,总大小约为1 GB。 这里是我的代码:使用Java为使用Java的大文件生成MD5非常缓慢
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) {
// ...
}
}
}
这似乎永远运行。 我该如何提高效率?
在'finally'块中''inputStream''不可能是'null'。 – BalusC 2012-02-17 02:42:49
无缓冲的IO缓慢,11时消息。 – 2012-04-16 16:55:56