【发布时间】:2012-03-08 11:29:19
【问题描述】:
我正在使用 Java 为某些文件生成 MD5 哈希。我需要为几个总大小约为 1 GB 的文件生成一个 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) {
// ...
}
}
}
这似乎永远运行。 如何提高效率?
【问题讨论】:
-
嘘,
inputStream不可能是null在finally块中。 -
无缓冲 IO 很慢,11 点消息。
标签: java performance file md5 checksum