home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / zip / CheckedOutputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  2.0 KB  |  74 lines

  1. /*
  2.  * @(#)CheckedOutputStream.java    1.11 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util.zip;
  16.  
  17. import java.io.FilterOutputStream;
  18. import java.io.OutputStream;
  19. import java.io.IOException;
  20.  
  21. /**
  22.  * An output stream that also maintains a checksum of the data being
  23.  * written. The checksum can then be used to verify the integrity of
  24.  * the output data.
  25.  *
  26.  * @see        Checksum
  27.  * @version     1.11, 03/18/98
  28.  * @author     David Connelly
  29.  */
  30. public
  31. class CheckedOutputStream extends FilterOutputStream {
  32.     private Checksum cksum;
  33.  
  34.     /**
  35.      * Creates an output stream with the specified Checksum.
  36.      * @param out the output stream
  37.      * @param cksum the checksum
  38.      */
  39.     public CheckedOutputStream(OutputStream out, Checksum cksum) {
  40.     super(out);
  41.     this.cksum = cksum;
  42.     }
  43.  
  44.     /**
  45.      * Writes a byte. Will block until the byte is actually written.
  46.      * @param b the byte to be written
  47.      * @exception IOException if an I/O error has occurred
  48.      */
  49.     public void write(int b) throws IOException {
  50.     out.write(b);
  51.     cksum.update(b);
  52.     }
  53.  
  54.     /**
  55.      * Writes an array of bytes. Will block until the bytes are
  56.      * actually written.
  57.      * @param buf the data to be written
  58.      * @param off the start offset of the data
  59.      * @param len the number of bytes to be written
  60.      * @exception IOException if an I/O error has occurred
  61.      */
  62.     public void write(byte[] b, int off, int len) throws IOException {
  63.     out.write(b, off, len);
  64.     cksum.update(b, off, len);
  65.     }
  66.  
  67.     /**
  68.      * Returns the Checksum for this output stream.
  69.      */
  70.     public Checksum getChecksum() {
  71.     return cksum;
  72.     }
  73. }
  74.