01: package uk.org.ponder.hashutil;
02:
03: import java.io.InputStream;
04: import java.io.IOException;
05:
06: import uk.org.ponder.streamutil.StreamCopyUtil;
07:
08: public class StreamDigestor {
09: /** Returns a byte array representing an SHA-1 digest of the supplied stream.
10: * This method closes the supplied stream.
11: * @param stream The stream to be digested
12: * @return A byte array holding the required digest.
13: * @exception IOException if an I/O error occurs.
14: */
15: public static byte[] digest(InputStream stream) throws IOException {
16: SHA1 sha = new SHA1();
17: byte[] buffer = new byte[StreamCopyUtil.PROCESS_BUFFER_SIZE];
18: try {
19: while (true) {
20: int bytesread = stream.read(buffer);
21: if (bytesread > 0) {
22: sha.update(buffer, 0, bytesread);
23: }
24: if (bytesread == -1)
25: break;
26: }
27: } finally {
28: stream.close();
29: }
30: return sha.digest();
31: }
32: }
|