23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

10.2.3 Feeding Data to the Digest<br />

<strong>Java</strong> I/O<br />

Once you have a MessageDigest object, you feed data into that digest by passing bytes into<br />

one of three update() methods. If you're digesting some other form of data, like Unicode<br />

text, you must first convert that data to bytes.<br />

public void update(byte input)<br />

public void update(byte[] input)<br />

public void update(byte[] input, int offset, int length)<br />

For example:<br />

byte[] data = new byte[128];<br />

int bytesRead = in.read(data);<br />

sha.update(data, 0, bytesRead);<br />

The first update() method takes a single byte as an argument. The second method takes an<br />

entire array of bytes. The third method takes the subarray of input beginning at offset and<br />

continuing for length bytes. You can call update() repeatedly, as long as you have more<br />

data to feed it. Example 10.1 passed in bytes as they were read from the input stream. The<br />

only restriction is that the bytes should not be reordered between calls to update().<br />

10.2.4 Finishing the Digest<br />

In general, digest algorithms cannot finish the calculation and return the digest until the last<br />

byte is received. When you are ready to finish the calculation and receive the digest, you<br />

invoke one of three overloaded digest() methods:<br />

public byte[] digest()<br />

public byte[] digest(byte[] input)<br />

public int digest(byte[] output, int offset, int length)<br />

throws DigestException<br />

The first digest() method simply returns the digest as an array of bytes based on the data<br />

that was already passed in through update(). For example:<br />

byte[] result = sha.digest();<br />

The second digest() method receives one last chunk of data in the input array, then returns<br />

the digest. The third digest() method calculates the digest and places it in the array output<br />

beginning at offset and continuing for at most length bytes, then returns the number of<br />

bytes in the digest. If there are more than length bytes in the digest a DigestException is<br />

thrown. After you've called digest(), the MessageDigest object is reset so it can be reused<br />

to calculate a new digest.<br />

10.2.5 Reusing Digests<br />

There's some overhead associated with creating a new message digest with<br />

MessageDigest.getInstance(). Therefore, if you want to use the same algorithm to<br />

calculate digests for many different files, web pages, or other streams, you can reset the digest<br />

and reuse it. The reset() method accomplishes this:<br />

200

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!