CheckedOutputStream.java revision 49965c1dc9da104344f4893a05e45795a5740d20
1/*
2 * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package java.util.zip;
27
28import java.io.FilterOutputStream;
29import java.io.OutputStream;
30import java.io.IOException;
31
32/**
33 * An output stream that also maintains a checksum of the data being
34 * written. The checksum can then be used to verify the integrity of
35 * the output data.
36 *
37 * @see         Checksum
38 * @author      David Connelly
39 */
40public
41class CheckedOutputStream extends FilterOutputStream {
42    private Checksum cksum;
43
44    /**
45     * Creates an output stream with the specified Checksum.
46     * @param out the output stream
47     * @param cksum the checksum
48     */
49    public CheckedOutputStream(OutputStream out, Checksum cksum) {
50        super(out);
51        this.cksum = cksum;
52    }
53
54    /**
55     * Writes a byte. Will block until the byte is actually written.
56     * @param b the byte to be written
57     * @exception IOException if an I/O error has occurred
58     */
59    public void write(int b) throws IOException {
60        out.write(b);
61        cksum.update(b);
62    }
63
64    /**
65     * Writes an array of bytes. Will block until the bytes are
66     * actually written.
67     * @param b the data to be written
68     * @param off the start offset of the data
69     * @param len the number of bytes to be written
70     * @exception IOException if an I/O error has occurred
71     */
72    public void write(byte[] b, int off, int len) throws IOException {
73        out.write(b, off, len);
74        cksum.update(b, off, len);
75    }
76
77    /**
78     * Returns the Checksum for this output stream.
79     * @return the Checksum
80     */
81    public Checksum getChecksum() {
82        return cksum;
83    }
84}
85