Save This Page
Home » openjdk-7 » java » util » zip » [javadoc | source]
    1   /*
    2    * Copyright 1996-2006 Sun Microsystems, Inc.  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.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package java.util.zip;
   27   
   28   import java.io.FilterInputStream;
   29   import java.io.InputStream;
   30   import java.io.IOException;
   31   
   32   /**
   33    * An input stream that also maintains a checksum of the data being read.
   34    * The checksum can then be used to verify the integrity of the input data.
   35    *
   36    * @see         Checksum
   37    * @author      David Connelly
   38    */
   39   public
   40   class CheckedInputStream extends FilterInputStream {
   41       private Checksum cksum;
   42   
   43       /**
   44        * Creates an input stream using the specified Checksum.
   45        * @param in the input stream
   46        * @param cksum the Checksum
   47        */
   48       public CheckedInputStream(InputStream in, Checksum cksum) {
   49           super(in);
   50           this.cksum = cksum;
   51       }
   52   
   53       /**
   54        * Reads a byte. Will block if no input is available.
   55        * @return the byte read, or -1 if the end of the stream is reached.
   56        * @exception IOException if an I/O error has occurred
   57        */
   58       public int read() throws IOException {
   59           int b = in.read();
   60           if (b != -1) {
   61               cksum.update(b);
   62           }
   63           return b;
   64       }
   65   
   66       /**
   67        * Reads into an array of bytes. If <code>len</code> is not zero, the method
   68        * blocks until some input is available; otherwise, no
   69        * bytes are read and <code>0</code> is returned.
   70        * @param buf the buffer into which the data is read
   71        * @param off the start offset in the destination array <code>b</code>
   72        * @param len the maximum number of bytes read
   73        * @return    the actual number of bytes read, or -1 if the end
   74        *            of the stream is reached.
   75        * @exception  NullPointerException If <code>buf</code> is <code>null</code>.
   76        * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
   77        * <code>len</code> is negative, or <code>len</code> is greater than
   78        * <code>buf.length - off</code>
   79        * @exception IOException if an I/O error has occurred
   80        */
   81       public int read(byte[] buf, int off, int len) throws IOException {
   82           len = in.read(buf, off, len);
   83           if (len != -1) {
   84               cksum.update(buf, off, len);
   85           }
   86           return len;
   87       }
   88   
   89       /**
   90        * Skips specified number of bytes of input.
   91        * @param n the number of bytes to skip
   92        * @return the actual number of bytes skipped
   93        * @exception IOException if an I/O error has occurred
   94        */
   95       public long skip(long n) throws IOException {
   96           byte[] buf = new byte[512];
   97           long total = 0;
   98           while (total < n) {
   99               long len = n - total;
  100               len = read(buf, 0, len < buf.length ? (int)len : buf.length);
  101               if (len == -1) {
  102                   return total;
  103               }
  104               total += len;
  105           }
  106           return total;
  107       }
  108   
  109       /**
  110        * Returns the Checksum for this input stream.
  111        * @return the Checksum value
  112        */
  113       public Checksum getChecksum() {
  114           return cksum;
  115       }
  116   }

Save This Page
Home » openjdk-7 » java » util » zip » [javadoc | source]