KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > zip > CheckedInputStream


1 /*
2  * @(#)CheckedInputStream.java 1.19 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.zip;
9
10 import java.io.FilterInputStream JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 /**
15  * An input stream that also maintains a checksum of the data being read.
16  * The checksum can then be used to verify the integrity of the input data.
17  *
18  * @see Checksum
19  * @version 1.19, 12/19/03
20  * @author David Connelly
21  */

22 public
23 class CheckedInputStream extends FilterInputStream JavaDoc {
24     private Checksum JavaDoc cksum;
25
26     /**
27      * Creates an input stream using the specified Checksum.
28      * @param in the input stream
29      * @param cksum the Checksum
30      */

31     public CheckedInputStream(InputStream JavaDoc in, Checksum JavaDoc cksum) {
32     super(in);
33     this.cksum = cksum;
34     }
35
36     /**
37      * Reads a byte. Will block if no input is available.
38      * @return the byte read, or -1 if the end of the stream is reached.
39      * @exception IOException if an I/O error has occurred
40      */

41     public int read() throws IOException JavaDoc {
42     int b = in.read();
43     if (b != -1) {
44         cksum.update(b);
45     }
46     return b;
47     }
48
49     /**
50      * Reads into an array of bytes. Will block until some input
51      * is available.
52      * @param buf the buffer into which the data is read
53      * @param off the start offset of the data
54      * @param len the maximum number of bytes read
55      * @return the actual number of bytes read, or -1 if the end
56      * of the stream is reached.
57      * @exception IOException if an I/O error has occurred
58      */

59     public int read(byte[] buf, int off, int len) throws IOException JavaDoc {
60     len = in.read(buf, off, len);
61     if (len != -1) {
62         cksum.update(buf, off, len);
63     }
64     return len;
65     }
66
67     /**
68      * Skips specified number of bytes of input.
69      * @param n the number of bytes to skip
70      * @return the actual number of bytes skipped
71      * @exception IOException if an I/O error has occurred
72      */

73     public long skip(long n) throws IOException JavaDoc {
74     byte[] buf = new byte[512];
75     long total = 0;
76     while (total < n) {
77         long len = n - total;
78         len = read(buf, 0, len < buf.length ? (int)len : buf.length);
79         if (len == -1) {
80         return total;
81         }
82         total += len;
83     }
84     return total;
85     }
86
87     /**
88      * Returns the Checksum for this input stream.
89      * @return the Checksum value
90      */

91     public Checksum JavaDoc getChecksum() {
92     return cksum;
93     }
94 }
95
Popular Tags