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 
  26 package java.util.zip;
  27 
  28 import java.io.FilterOutputStream;
  29 import java.io.OutputStream;
  30 import 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  * @since 1.1
  40  */
  41 public
  42 class CheckedOutputStream extends FilterOutputStream {
  43     private Checksum cksum;
  44 
  45     /**
  46      * Creates an output stream with the specified Checksum.
  47      * @param out the output stream
  48      * @param cksum the checksum
  49      */
  50     public CheckedOutputStream(OutputStream out, Checksum cksum) {
  51         super(out);
  52         this.cksum = cksum;
  53     }
  54 
  55     /**
  56      * Writes a byte. Will block until the byte is actually written.
  57      * @param b the byte to be written
  58      * @exception IOException if an I/O error has occurred
  59      */
  60     public void write(int b) throws IOException {
  61         out.write(b);
  62         cksum.update(b);
  63     }
  64 
  65     /**
  66      * Writes an array of bytes. Will block until the bytes are
  67      * actually written.
  68      * @param b the data to be written
  69      * @param off the start offset of the data
  70      * @param len the number of bytes to be written
  71      * @exception IOException if an I/O error has occurred
  72      */
  73     public void write(byte[] b, int off, int len) throws IOException {
  74         out.write(b, off, len);
  75         cksum.update(b, off, len);
  76     }
  77 
  78     /**
  79      * Returns the Checksum for this output stream.
  80      * @return the Checksum
  81      */
  82     public Checksum getChecksum() {
  83         return cksum;
  84     }
  85 }