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 / InflaterInputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  4.2 KB  |  153 lines

  1. /*
  2.  * @(#)InflaterInputStream.java    1.18 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. import java.io.EOFException;
  21.  
  22. /**
  23.  * This class implements a stream filter for uncompressing data in the
  24.  * "deflate" compression format. It is also used as the basis for other
  25.  * decompression filters, such as GZIPInputStream.
  26.  *
  27.  * @see        Inflater
  28.  * @version     1.18, 03/18/98
  29.  * @author     David Connelly
  30.  */
  31. public
  32. class InflaterInputStream extends FilterInputStream {
  33.     /**
  34.      * Decompressor for this stream.
  35.      */
  36.     protected Inflater inf;
  37.  
  38.     /**
  39.      * Input buffer for decompression.
  40.      */
  41.     protected byte[] buf;
  42.  
  43.     /**
  44.      * Length of input buffer.
  45.      */
  46.     protected int len;
  47.  
  48.     /**
  49.      * Creates a new input stream with the specified decompressor and
  50.      * buffer size.
  51.      * @param in the input stream
  52.      * @param inf the decompressor ("inflater")
  53.      * @param size the input buffer size
  54.      */
  55.     public InflaterInputStream(InputStream in, Inflater inf, int size) {
  56.     super(in);
  57.     this.inf = inf;
  58.     buf = new byte[size];
  59.     }
  60.  
  61.     /**
  62.      * Creates a new input stream with the specified decompressor and a
  63.      * default buffer size.
  64.      * @param in the input stream
  65.      * @param inf the decompressor ("inflater")
  66.      */
  67.     public InflaterInputStream(InputStream in, Inflater inf) {
  68.     this(in, inf, 512);
  69.     }
  70.  
  71.     /**
  72.      * Creates a new input stream with a default decompressor and buffer size.
  73.      */
  74.     public InflaterInputStream(InputStream in) {
  75.     this(in, new Inflater());
  76.     }
  77.  
  78.     /**
  79.      * Reads a byte of uncompressed data. This method will block until
  80.      * enough input is available for decompression.
  81.      * @return the byte read, or -1 if end of compressed input is reached
  82.      * @exception IOException if an I/O error has occurred
  83.      */
  84.     public int read() throws IOException {
  85.     byte[] b = new byte[1];
  86.     return read(b, 0, 1) == -1 ? -1 : b[0] & 0xff;
  87.     }
  88.  
  89.     /**
  90.      * Reads uncompressed data into an array of bytes. This method will
  91.      * block until some input can be decompressed.
  92.      * @param b the buffer into which the data is read
  93.      * @param off the start offset of the data
  94.      * @param len the maximum number of bytes read
  95.      * @return the actual number of bytes read, or -1 if the end of the
  96.      *         compressed input is reached or a preset dictionary is needed
  97.      * @exception ZipException if a ZIP format error has occurred
  98.      * @exception IOException if an I/O error has occurred
  99.      */
  100.     public int read(byte[] b, int off, int len) throws IOException {
  101.     if (len <= 0) {
  102.         return 0;
  103.     }
  104.     try {
  105.         int n;
  106.         while ((n = inf.inflate(b, off, len)) == 0) {
  107.         if (inf.finished() || inf.needsDictionary()) {
  108.             return -1;
  109.         }
  110.         if (inf.needsInput()) {
  111.             fill();
  112.         }
  113.         }
  114.         return n;
  115.     } catch (DataFormatException e) {
  116.         String s = e.getMessage();
  117.         throw new ZipException(s != null ? s : "Invalid ZLIB data format");
  118.     }
  119.     }
  120.  
  121.     /**
  122.      * Skips specified number of bytes of uncompressed data.
  123.      * @param n the number of bytes to skip
  124.      * @return the actual number of bytes skipped.
  125.      * @exception IOException if an I/O error has occurred
  126.      */
  127.     public long skip(long n) throws IOException {
  128.     byte[] b = new byte[512];
  129.     long total = 0;
  130.     while (total < n) {
  131.         long len = n - total;
  132.         len = read(b, 0, len < b.length ? (int)len : b.length);
  133.         if (len == -1) {
  134.         break;
  135.         }
  136.         total += len;
  137.     }
  138.     return total;
  139.     }
  140.  
  141.     /**
  142.      * Fills input buffer with more data to decompress.
  143.      * @exception IOException if an I/O error has occurred
  144.      */
  145.     protected void fill() throws IOException {
  146.     len = in.read(buf, 0, buf.length);
  147.     if (len == -1) {
  148.         throw new EOFException("Unexpected end of ZLIB input stream");
  149.     }
  150.     inf.setInput(buf, 0, len);
  151.     }
  152. }
  153.