KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > tools > SignatureInputStream


1 package net.sourceforge.jcetaglib.tools;
2
3 import java.io.FilterInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.security.Signature JavaDoc;
7 import java.security.SignatureException JavaDoc;
8
9 /**
10  * SignatureInputStream. As data is read it updates a MAC
11  */

12
13
14 public class SignatureInputStream extends FilterInputStream JavaDoc {
15     private Signature JavaDoc sig = null;
16
17     public SignatureInputStream(InputStream JavaDoc st,
18                                 Signature JavaDoc sig) {
19         super(st);
20         this.sig = sig;
21     }
22
23     public int read()
24             throws IOException JavaDoc {
25         int n = in.read();
26         if (n > -1) {
27             try {
28                 sig.update((byte) n);
29             } catch (SignatureException JavaDoc s_ex) {
30                 throw(new IOException JavaDoc(s_ex.getMessage()));
31             }
32         }
33
34         return (n);
35     }
36
37     public int read(byte[] b)
38             throws IOException JavaDoc {
39         int n = in.read(b);
40         if (n > -1) {
41             try {
42                 sig.update(b, 0, n);
43             } catch (SignatureException JavaDoc s_ex) {
44                 throw new IOException JavaDoc(s_ex.getMessage());
45             }
46         }
47
48         return (n);
49     }
50
51     public int read(byte[] b,
52                     int o,
53                     int l)
54             throws IOException JavaDoc {
55         int n = in.read(b, o, l);
56         if (n > -1) {
57             try {
58                 sig.update(b, o, n);
59             } catch (SignatureException JavaDoc s_ex) {
60                 throw(new IOException JavaDoc(s_ex.getMessage()));
61             }
62         }
63
64         return (n);
65     }
66 }
67
Popular Tags