KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)CheckedOutputStream.java 1.18 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.FilterOutputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 /**
15  * An output stream that also maintains a checksum of the data being
16  * written. The checksum can then be used to verify the integrity of
17  * the output data.
18  *
19  * @see Checksum
20  * @version 1.18, 12/19/03
21  * @author David Connelly
22  */

23 public
24 class CheckedOutputStream extends FilterOutputStream JavaDoc {
25     private Checksum JavaDoc cksum;
26
27     /**
28      * Creates an output stream with the specified Checksum.
29      * @param out the output stream
30      * @param cksum the checksum
31      */

32     public CheckedOutputStream(OutputStream JavaDoc out, Checksum JavaDoc cksum) {
33     super(out);
34     this.cksum = cksum;
35     }
36
37     /**
38      * Writes a byte. Will block until the byte is actually written.
39      * @param b the byte to be written
40      * @exception IOException if an I/O error has occurred
41      */

42     public void write(int b) throws IOException JavaDoc {
43     out.write(b);
44     cksum.update(b);
45     }
46
47     /**
48      * Writes an array of bytes. Will block until the bytes are
49      * actually written.
50      * @param b the data to be written
51      * @param off the start offset of the data
52      * @param len the number of bytes to be written
53      * @exception IOException if an I/O error has occurred
54      */

55     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
56     out.write(b, off, len);
57     cksum.update(b, off, len);
58     }
59
60     /**
61      * Returns the Checksum for this output stream.
62      * @return the Checksum
63      */

64     public Checksum JavaDoc getChecksum() {
65     return cksum;
66     }
67 }
68
Popular Tags