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?