我正在使用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