home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / CheckedInputStream.java < prev    next >
Text File  |  1997-05-20  |  3KB  |  109 lines

  1. /*
  2.  * @(#)CheckedInputStream.java    1.10 96/11/23
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. import java.io.FilterInputStream;
  26. import java.io.InputStream;
  27. import java.io.IOException;
  28.  
  29. /**
  30.  * An input stream that also maintains a checksum of the data being read.
  31.  * The checksum can then be used to verify the integrity of the input data.
  32.  *
  33.  * @see        Checksum
  34.  * @version     1.10, 11/23/96
  35.  * @author     David Connelly
  36.  */
  37. public
  38. class CheckedInputStream extends FilterInputStream {
  39.     private Checksum cksum;
  40.  
  41.     /**
  42.      * Creates an input stream using the specified Checksum.
  43.      * @param in the input stream
  44.      * @param cksum the Checksum
  45.      */
  46.     public CheckedInputStream(InputStream in, Checksum cksum) {
  47.     super(in);
  48.     this.cksum = cksum;
  49.     }
  50.  
  51.     /**
  52.      * Reads a byte. Will block if no input is available.
  53.      * @return the byte read, or -1 if the end of the stream is reached.
  54.      * @exception IOException if an I/O error has occurred
  55.      */
  56.     public int read() throws IOException {
  57.     int b = in.read();
  58.     if (b != -1) {
  59.         cksum.update(b);
  60.     }
  61.     return b;
  62.     }
  63.  
  64.     /**
  65.      * Reads into an array of bytes. Will block until some input
  66.      * is available.
  67.      * @param buf the buffer into which the data is read
  68.      * @param off the start offset of the data
  69.      * @param len the maximum number of bytes read
  70.      * @return    the actual number of bytes read, or -1 if the end
  71.      *          of the stream is reached.
  72.      * @exception IOException if an I/O error has occurred
  73.      */
  74.     public int read(byte[] buf, int off, int len) throws IOException {
  75.     len = in.read(buf, off, len);
  76.     if (len != -1) {
  77.         cksum.update(buf, off, len);
  78.     }
  79.     return len;
  80.     }
  81.  
  82.     /**
  83.      * Skips specified number of bytes of input.
  84.      * @param n the number of bytes to skip
  85.      * @return the actual number of bytes skipped
  86.      * @exception IOException if an I/O error has occurred
  87.      */
  88.     public long skip(long n) throws IOException {
  89.     byte[] buf = new byte[512];
  90.     long total = 0;
  91.     while (total < n) {
  92.         long len = n - total;
  93.         len = read(buf, 0, len < buf.length ? (int)len : buf.length);
  94.         if (len == -1) {
  95.         return total;
  96.         }
  97.         total += len;
  98.     }
  99.     return total;
  100.     }
  101.  
  102.     /**
  103.      * Returns the Checksum for this input stream.
  104.      */
  105.     public Checksum getChecksum() {
  106.     return cksum;
  107.     }
  108. }
  109.