01: /*
02: * ====================================================================
03: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
04: *
05: * This software is licensed as described in the file COPYING, which
06: * you should have received as part of this distribution. The terms
07: * are also available at http://svnkit.com/license.html
08: * If newer versions of this license are posted there, you may use a
09: * newer version instead, at your option.
10: * ====================================================================
11: */
12: package org.tmatesoft.svn.core.internal.wc.admin;
13:
14: import java.io.IOException;
15: import java.io.InputStream;
16: import java.security.MessageDigest;
17: import java.security.NoSuchAlgorithmException;
18:
19: import org.tmatesoft.svn.core.internal.wc.SVNFileUtil;
20:
21: /**
22: * @version 1.1.1
23: * @author TMate Software Ltd.
24: */
25: public class SVNChecksumInputStream extends InputStream {
26:
27: private InputStream mySource;
28: private MessageDigest myDigest;
29: private byte[] myDigestResult;
30:
31: public SVNChecksumInputStream(InputStream source) {
32: mySource = source;
33: try {
34: myDigest = MessageDigest.getInstance("MD5");
35: } catch (NoSuchAlgorithmException e) {
36: }
37: }
38:
39: public int read(byte[] b, int off, int len) throws IOException {
40: int r = mySource.read(b, off, len);
41: if (r >= 0) {
42: myDigest.update(b, 0, r);
43: }
44: return r;
45: }
46:
47: public int read(byte[] b) throws IOException {
48: int r = mySource.read(b);
49: if (r >= 0) {
50: myDigest.update(b, 0, r);
51: }
52: return r;
53: }
54:
55: public int read() throws IOException {
56: int r = mySource.read();
57: if (r >= 0) {
58: myDigest.update((byte) (r & 0xFF));
59: }
60: return r;
61: }
62:
63: public void close() throws IOException {
64: myDigestResult = myDigest.digest();
65: }
66:
67: public String getDigest() {
68: return SVNFileUtil.toHexDigest(myDigestResult);
69: }
70:
71: }
|