KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > MD5InputStream


1 /*
2  * @(#)MD5InputStream.java 0.3-2 18/06/1999
3  *
4  * This file is part of the HTTPClient package
5  * Copyright (C) 1996-1999 Ronald Tschalär
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307, USA
21  *
22  * For questions, suggestions, bug-reports, enhancement-requests etc.
23  * I may be contacted at:
24  *
25  * ronald@innovation.ch
26  *
27  */

28
29 package HTTPClient;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.FilterInputStream JavaDoc;
34 import java.net.ProtocolException JavaDoc;
35
36
37 /**
38  * This class calculates a running md5 digest of the data read. When the
39  * stream is closed the calculated digest is passed to a HashVerifier which
40  * is expected to verify this digest and to throw an Exception if it fails.
41  *
42  * @version 0.3-2 18/06/1999
43  * @author Ronald Tschalär
44  */

45 class MD5InputStream extends FilterInputStream JavaDoc
46 {
47     private HashVerifier verifier;
48     private MD5 md5;
49     private long rcvd = 0;
50     private boolean closed = false;
51
52
53     /**
54      * @param is the input stream over which the md5 hash is to be calculated
55      * @param verifier the HashVerifier to invoke when the stream is closed
56      */

57     public MD5InputStream(InputStream JavaDoc is, HashVerifier verifier)
58     {
59     super(is);
60     this.verifier = verifier;
61     md5 = new MD5();
62     }
63
64
65     public synchronized int read() throws IOException JavaDoc
66     {
67     int b = in.read();
68     if (b != -1)
69         md5.Update((byte) b);
70     else
71         real_close();
72
73     rcvd++;
74     return b;
75     }
76
77
78     public synchronized int read(byte[] buf, int off, int len)
79         throws IOException JavaDoc
80     {
81     int num = in.read(buf, off, len);
82     if (num > 0)
83         md5.Update(buf, off, num);
84     else
85         real_close();
86
87     rcvd += num;
88     return num;
89     }
90
91
92     public synchronized long skip(long num) throws IOException JavaDoc
93     {
94     byte[] tmp = new byte[(int) num];
95     int got = read(tmp, 0, (int) num);
96
97     if (got > 0)
98         return (long) got;
99     else
100         return 0L;
101     }
102
103
104     /**
105      * Close the stream and check the digest. If the stream has not been
106      * fully read then the rest of the data will first be read (and discarded)
107      * to complete the digest calculation.
108      *
109      * @exception IOException if the close()'ing the underlying stream throws
110      * an IOException, or if the expected digest and
111      * the calculated digest don't match.
112      */

113     public synchronized void close() throws IOException JavaDoc
114     {
115     while (skip(10000) > 0) ;
116     real_close();
117     }
118
119
120     /**
121      * Close the stream and check the digest.
122      *
123      * @exception IOException if the close()'ing the underlying stream throws
124      * an IOException, or if the expected digest and
125      * the calculated digest don't match.
126      */

127     private void real_close() throws IOException JavaDoc
128     {
129     if (closed) return;
130     closed = true;
131
132     in.close();
133     verifier.verifyHash(md5.Final(), rcvd);
134     }
135 }
136
137
Popular Tags