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 / CheckedInputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  2.7 KB  |  101 lines

  1. /*
  2.  * @(#)CheckedInputStream.java    1.12 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.FilterInputStream;
  18. import java.io.InputStream;
  19. import java.io.IOException;
  20.  
  21. /**
  22.  * An input stream that also maintains a checksum of the data being read.
  23.  * The checksum can then be used to verify the integrity of the input data.
  24.  *
  25.  * @see        Checksum
  26.  * @version     1.12, 03/18/98
  27.  * @author     David Connelly
  28.  */
  29. public
  30. class CheckedInputStream extends FilterInputStream {
  31.     private Checksum cksum;
  32.  
  33.     /**
  34.      * Creates an input stream using the specified Checksum.
  35.      * @param in the input stream
  36.      * @param cksum the Checksum
  37.      */
  38.     public CheckedInputStream(InputStream in, Checksum cksum) {
  39.     super(in);
  40.     this.cksum = cksum;
  41.     }
  42.  
  43.     /**
  44.      * Reads a byte. Will block if no input is available.
  45.      * @return the byte read, or -1 if the end of the stream is reached.
  46.      * @exception IOException if an I/O error has occurred
  47.      */
  48.     public int read() throws IOException {
  49.     int b = in.read();
  50.     if (b != -1) {
  51.         cksum.update(b);
  52.     }
  53.     return b;
  54.     }
  55.  
  56.     /**
  57.      * Reads into an array of bytes. Will block until some input
  58.      * is available.
  59.      * @param buf the buffer into which the data is read
  60.      * @param off the start offset of the data
  61.      * @param len the maximum number of bytes read
  62.      * @return    the actual number of bytes read, or -1 if the end
  63.      *          of the stream is reached.
  64.      * @exception IOException if an I/O error has occurred
  65.      */
  66.     public int read(byte[] buf, int off, int len) throws IOException {
  67.     len = in.read(buf, off, len);
  68.     if (len != -1) {
  69.         cksum.update(buf, off, len);
  70.     }
  71.     return len;
  72.     }
  73.  
  74.     /**
  75.      * Skips specified number of bytes of input.
  76.      * @param n the number of bytes to skip
  77.      * @return the actual number of bytes skipped
  78.      * @exception IOException if an I/O error has occurred
  79.      */
  80.     public long skip(long n) throws IOException {
  81.     byte[] buf = new byte[512];
  82.     long total = 0;
  83.     while (total < n) {
  84.         long len = n - total;
  85.         len = read(buf, 0, len < buf.length ? (int)len : buf.length);
  86.         if (len == -1) {
  87.         return total;
  88.         }
  89.         total += len;
  90.     }
  91.     return total;
  92.     }
  93.  
  94.     /**
  95.      * Returns the Checksum for this input stream.
  96.      */
  97.     public Checksum getChecksum() {
  98.     return cksum;
  99.     }
  100. }
  101.