JProfiler
Get rid of your performance problems and memory leaks!
A friendly place for Java greenhorns!
Java
Ranch
»
JavaRanch Saloon
Search
|
FAQ
|
Recent Topics
|
Hot Topics
Register
/
Login
Win a copy of
JBoss AS 5 Development
this week in the
JBoss
forum
or
Spring Dynamic Modules in Action
in the
Spring
forum!
Java
Ranch
»
Forums
»
Java
»
Java in General
Author
MD5 checksum, can i do this faster , from a inputstream ?
Frank Jacobsen
Ranch Hand
Joined: May 17, 2002
Messages: 308
posted
Jun 17, 2009 00:48:01
I calculate a MD5 checksum, from a inputstream, but with this code i read the 1 mb data to calculate the MD5......
Its not allsays 1 mb, it can be from 50 kb ---> 50 mb
Is there anyone, that knows if i can do this faster.....
java.security.MessageDigest md5er = MessageDigest.getInstance("MD5"); byte[] buf = new byte[8192]; int num = 0; int i = 1; num = connection.getInputStream().read(buf); while (num > 0) { md5er.update(buf,0,buf.length); System.out.println("FJA: " + i); length = length + buf.length; i++; num = connection.getInputStream().read(buf); } byte[] md5sum = md5er.digest(); BigInteger bigInt = new BigInteger(1, md5sum); String output = bigInt.toString(16); System.out.println("FJA length: " + length); System.out.println("MD5: " + output);
Frank
Rob Prime
Bartender
Joined: Oct 27, 2005
Messages: 8261
posted
Jun 17, 2009 04:18:00
md5er.update(buf,0,buf.length); ... length = length + buf.length;
Don't use buf.length but num. num can be smaller than buf.length if there weren't enough bytes available. This way you may write some garbage of a previous read to the MD5 digest, and your length may be too high.
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions
How To Answer Questions
Java
Ranch
»
Forums
»
Java
»
Java in General
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
trailboss links:
permaculture
diatomaceous earth
Sepp Holzer
rocket mass heater
raising chickens
cast iron
lawn care