Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Generate MD5 (http://www.velocityreviews.com/forums/t647695-generate-md5.html)

Bumsys@gmail.com 12-08-2008 09:35 AM

Generate MD5
 
I generate md5 for file using the following code:

private static byte[] createChecksum(final File file) throws
IOException,
NoSuchAlgorithmException {
InputStream fis = new FileInputStream(file);

byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();

return complete.digest();
}

How can I increase the getting md5 for file?

John B. Matthews 12-08-2008 02:23 PM

Re: Generate MD5
 
In article
<8f5280cf-4bf0-4bc2-b974-8848f60bc602@d14g2000yqb.googlegroups.com>,
Bumsys@gmail.com wrote:

> I generate md5 for file using the following code:
>
> private static byte[] createChecksum(final File file) throws
> IOException,
> NoSuchAlgorithmException {
> InputStream fis = new FileInputStream(file);
>
> byte[] buffer = new byte[1024];
> MessageDigest complete = MessageDigest.getInstance("MD5");
> int numRead;
> do {
> numRead = fis.read(buffer);
> if (numRead > 0) {
> complete.update(buffer, 0, numRead);
> }
> } while (numRead != -1);
> fis.close();
>
> return complete.digest();
> }
>
> How can I increase the [efficiency of] getting [the] md5 for [a] file?


Consider wrapping your FileInputStream in a BufferedInputStream:

<http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html>

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/

Arne Vajh°j 12-08-2008 10:26 PM

Re: Generate MD5
 
Bumsys@gmail.com wrote:
> I generate md5 for file using the following code:
>
> private static byte[] createChecksum(final File file) throws
> IOException,
> NoSuchAlgorithmException {
> InputStream fis = new FileInputStream(file);
>
> byte[] buffer = new byte[1024];
> MessageDigest complete = MessageDigest.getInstance("MD5");
> int numRead;
> do {
> numRead = fis.read(buffer);
> if (numRead > 0) {
> complete.update(buffer, 0, numRead);
> }
> } while (numRead != -1);
> fis.close();
>
> return complete.digest();
> }
>
> How can I increase the getting md5 for file?


Bigger buffer than 1024.

Arne

Arne Vajh°j 12-08-2008 10:34 PM

Re: Generate MD5
 
John B. Matthews wrote:
> In article
> <8f5280cf-4bf0-4bc2-b974-8848f60bc602@d14g2000yqb.googlegroups.com>,
> Bumsys@gmail.com wrote:
>> I generate md5 for file using the following code:
>>
>> private static byte[] createChecksum(final File file) throws
>> IOException,
>> NoSuchAlgorithmException {
>> InputStream fis = new FileInputStream(file);
>>
>> byte[] buffer = new byte[1024];
>> MessageDigest complete = MessageDigest.getInstance("MD5");
>> int numRead;
>> do {
>> numRead = fis.read(buffer);
>> if (numRead > 0) {
>> complete.update(buffer, 0, numRead);
>> }
>> } while (numRead != -1);
>> fis.close();
>>
>> return complete.digest();
>> }
>>
>> How can I increase the [efficiency of] getting [the] md5 for [a] file?

>
> Consider wrapping your FileInputStream in a BufferedInputStream:
>
> <http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html>


If the program is reading into a sufficient large buffer, then then
there are no benefits of using BufferedInputStream.
BufferedInputStream is a huge benefit when reading just a few bytes
at a time - on some more selective data processing scenario.

Arne

John B. Matthews 12-09-2008 12:38 AM

Re: Generate MD5
 
In article <493da0da$0$90268$14726298@news.sunsite.dk>,
Arne Vajh°j <arne@vajhoej.dk> wrote:

> John B. Matthews wrote:
> > In article
> > <8f5280cf-4bf0-4bc2-b974-8848f60bc602@d14g2000yqb.googlegroups.com>,
> > Bumsys@gmail.com wrote:
> >> I generate md5 for file using the following code:
> >>
> >> private static byte[] createChecksum(final File file) throws
> >> IOException,
> >> NoSuchAlgorithmException {
> >> InputStream fis = new FileInputStream(file);
> >>
> >> byte[] buffer = new byte[1024];
> >> MessageDigest complete = MessageDigest.getInstance("MD5");
> >> int numRead;
> >> do {
> >> numRead = fis.read(buffer);
> >> if (numRead > 0) {
> >> complete.update(buffer, 0, numRead);
> >> }
> >> } while (numRead != -1);
> >> fis.close();
> >>
> >> return complete.digest();
> >> }
> >>
> >> How can I increase the [efficiency of] getting [the] md5 for [a] file?

> >
> > Consider wrapping your FileInputStream in a BufferedInputStream:
> >
> > <http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html>

>
> If the program is reading into a sufficient large buffer, then then
> there are no benefits of using BufferedInputStream.
> BufferedInputStream is a huge benefit when reading just a few bytes
> at a time - on some more selective data processing scenario.


Thanks, Arne. I see your point: a MessageDigest instance can process an
entire buffer-sized chunk, so a bigger buffer is better than double
buffering.

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/

Bumsys@gmail.com 12-10-2008 09:43 AM

Re: Generate MD5
 
I increase the buffer more than 1024 but it does not help. The speed
is the same.

John B. Matthews 12-10-2008 10:37 AM

Re: Generate MD5
 
In article
<21863fb5-2d62-4423-be45-f2f05f40c3a7@s9g2000prg.googlegroups.com>,
Bumsys@gmail.com wrote:

> I increase the buffer more than 1024 but it does not help. The speed
> is the same.


Why not prepare an sscce <http://pscode.org/sscce.html> showing the
unchanged speed? Perhaps someone will spot the problem.

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/

Roedy Green 12-11-2008 07:37 PM

Re: Generate MD5
 
On Mon, 8 Dec 2008 01:35:01 -0800 (PST), Bumsys@gmail.com wrote,
quoted or indirectly quoted someone who said :

>I generate md5 for file using the following code:
>
>private static byte[] createChecksum(final File file) throws
>IOException,
> NoSuchAlgorithmException {
> InputStream fis = new FileInputStream(file);
>
> byte[] buffer = new byte[1024];
> MessageDigest complete = MessageDigest.getInstance("MD5");
> int numRead;
> do {
> numRead = fis.read(buffer);
> if (numRead > 0) {
> complete.update(buffer, 0, numRead);
> }
> } while (numRead != -1);
> fis.close();
>
> return complete.digest();
> }
>
>How can I increase the getting md5 for file?


Generate the MD5 purely in RAM with my code at
http://mindprod.com/jgloss/md5.html

Compare the speed. If they are the same, the problem is in the
complexity of computing MD5. You might try a faster hash, like Adler.

If they are different, your problem is in the i/o. You need bigger
buffers, or perhaps reading the file into RAM in a single I/O. without
buffering.
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP


All times are GMT. The time now is 05:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.