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

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