01: /*
02: * @(#)CheckedOutputStream.java 1.22 06/10/10
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: *
26: */
27:
28: package java.util.zip;
29:
30: import java.io.FilterOutputStream;
31: import java.io.OutputStream;
32: import java.io.IOException;
33:
34: /**
35: * An output stream that also maintains a checksum of the data being
36: * written. The checksum can then be used to verify the integrity of
37: * the output data.
38: *
39: * @see Checksum
40: * @version 1.15, 02/02/00
41: * @author David Connelly
42: */
43: public class CheckedOutputStream extends FilterOutputStream {
44: private Checksum cksum;
45:
46: /**
47: * Creates an output stream with the specified Checksum.
48: * @param out the output stream
49: * @param cksum the checksum
50: */
51: public CheckedOutputStream(OutputStream out, Checksum cksum) {
52: super (out);
53: this .cksum = cksum;
54: }
55:
56: /**
57: * Writes a byte. Will block until the byte is actually written.
58: * @param b the byte to be written
59: * @exception IOException if an I/O error has occurred
60: */
61: public void write(int b) throws IOException {
62: out.write(b);
63: cksum.update(b);
64: }
65:
66: /**
67: * Writes an array of bytes. Will block until the bytes are
68: * actually written.
69: * @param b the data to be written
70: * @param off the start offset of the data
71: * @param len the number of bytes to be written
72: * @exception IOException if an I/O error has occurred
73: */
74: public void write(byte[] b, int off, int len) throws IOException {
75: out.write(b, off, len);
76: cksum.update(b, off, len);
77: }
78:
79: /**
80: * Returns the Checksum for this output stream.
81: * @return the Checksum
82: */
83: public Checksum getChecksum() {
84: return cksum;
85: }
86: }
|