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

  1. /*
  2.  * @(#)Inflater.java    1.18 97/02/14
  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. /**
  26.  * This class provides support for general purpose decompression using
  27.  * the popular ZLIB compression library. The ZLIB compression library
  28.  * was initially developed as part of the PNG graphics standard and is
  29.  * not protected by patents. It is fully described in RFCs 1950, 1951,
  30.  * and 1952, which can be found at <a href="ftp://ds.internic.net/rfc">
  31.  * ftp://ds.internic.net/rfc</a> in the files rfc1950.txt (zlib format),
  32.  * rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  33.  * 
  34.  * @see        Deflater
  35.  * @version     1.18, 02/14/97
  36.  * @author     David Connelly
  37.  *
  38.  */
  39. public
  40. class Inflater {
  41.     private int strm;
  42.     private byte[] buf = new byte[0];
  43.     private int off, len;
  44.     private boolean finished;
  45.     private boolean needsDictionary;
  46.  
  47.     /*
  48.      * Loads the ZLIB library.
  49.      */
  50.     static {
  51.     System.loadLibrary("zip");
  52.     }
  53.  
  54.     /**
  55.      * Creates a new decompressor. If the parameter 'nowrap' is true then
  56.      * the ZLIB header and checksum fields will not be used in order to
  57.      * support the compression format used by both GZIP and PKZIP.
  58.      * @param nowrap if true then support GZIP compatible compression
  59.      */
  60.     public Inflater(boolean nowrap) {
  61.     init(nowrap);
  62.     }
  63.  
  64.     /**
  65.      * Creates a new decompressor.
  66.      */
  67.     public Inflater() {
  68.     this(false);
  69.     }
  70.  
  71.     /**
  72.      * Sets input data for decompression. Should be called whenever
  73.      * needsInput() returns true indicating that more input data is
  74.      * required.
  75.      * @param b the input data bytes
  76.      * @param off the start offset of the input data
  77.      * @param len the length of the input data
  78.      * @see Inflater#needsInput
  79.      */
  80.     public synchronized void setInput(byte[] b, int off, int len) {
  81.     if (b == null) {
  82.         throw new NullPointerException();
  83.     }
  84.     if (off < 0 || len < 0 || off + len > b.length) {
  85.         throw new ArrayIndexOutOfBoundsException();
  86.     }
  87.     this.buf = b;
  88.     this.off = off;
  89.     this.len = len;
  90.     }
  91.  
  92.     /**
  93.      * Sets input data for decompression. Should be called whenever
  94.      * needsInput() returns true indicating that more input data is
  95.      * required.
  96.      * @param b the input data bytes
  97.      * @see Inflater#needsInput
  98.      */
  99.     public void setInput(byte[] b) {
  100.     setInput(b, 0, b.length);
  101.     }
  102.  
  103.     /**
  104.      * Sets the preset dictionary to the given array of bytes. Should be
  105.      * called when inflate() returns 0 and needsDictionary() returns true
  106.      * indicating that a preset dictionary is required. The method getAdler()
  107.      * can be used to get the Adler-32 value of the dictionary needed.
  108.      * @param b the dictionary data bytes
  109.      * @param off the start offset of the data
  110.      * @param len the length of the data
  111.      * @see Inflater#needsDictionary
  112.      * @see Inflater#getAdler
  113.      */
  114.     public synchronized native void setDictionary(byte[] b, int off, int len);
  115.  
  116.     /**
  117.      * Sets the preset dictionary to the given array of bytes. Should be
  118.      * called when inflate() returns 0 and needsDictionary() returns true
  119.      * indicating that a preset dictionary is required. The method getAdler()
  120.      * can be used to get the Adler-32 value of the dictionary needed.
  121.      * @param b the dictionary data bytes
  122.      * @see Inflater#needsDictionary
  123.      * @see Inflater#getAdler
  124.      */
  125.     public void setDictionary(byte[] b) {
  126.     setDictionary(b, 0, b.length);
  127.     }
  128.  
  129.     /**
  130.      * Returns the total number of bytes remaining in the input buffer.
  131.      * This can be used to find out what bytes still remain in the input
  132.      * buffer after decompression has finished.
  133.      */
  134.     public synchronized int getRemaining() {
  135.     return len;
  136.     }
  137.  
  138.     /**
  139.      * Returns true if no data remains in the input buffer. This can
  140.      * be used to determine if #setInput should be called in order
  141.      * to provide more input.
  142.      */
  143.     public synchronized boolean needsInput() {
  144.     return len <= 0;
  145.     }
  146.  
  147.     /**
  148.      * Returns true if a preset dictionary is needed for decompression.
  149.      * @see InflatesetDictionary
  150.      */
  151.     public synchronized boolean needsDictionary() {
  152.     return needsDictionary;
  153.     }
  154.  
  155.     /**
  156.      * Return true if the end of the compressed data stream has been
  157.      * reached.
  158.      */
  159.     public synchronized boolean finished() {
  160.     return finished;
  161.     }
  162.  
  163.     /**
  164.      * Uncompresses bytes into specified buffer. Returns actual number
  165.      * of bytes uncompressed. A return value of 0 indicates that
  166.      * needsInput() or needsDictionary() should be called in order to
  167.      * determine if more input data or a preset dictionary is required.
  168.      * In the later case, getAdler() can be used to get the Adler-32
  169.      * value of the dictionary required.
  170.      * @param b the buffer for the uncompressed data
  171.      * @param off the start offset of the data
  172.      * @param len the maximum number of uncompressed bytes
  173.      * @return the actual number of uncompressed bytes
  174.      * @exception DataFormatException if the compressed data format is invalid
  175.      * @see Inflater#needsInput
  176.      * @see Inflater#needsDictionary
  177.      */
  178.     public synchronized native int inflate(byte[] b, int off, int len)
  179.         throws DataFormatException;
  180.  
  181.     /**
  182.      * Uncompresses bytes into specified buffer. Returns actual number
  183.      * of bytes uncompressed. A return value of 0 indicates that
  184.      * needsInput() or needsDictionary() should be called in order to
  185.      * determine if more input data or a preset dictionary is required.
  186.      * In the later case, getAdler() can be used to get the Adler-32
  187.      * value of the dictionary required.
  188.      * @param b the buffer for the uncompressed data
  189.      * @return the actual number of uncompressed bytes
  190.      * @exception DataFormatException if the compressed data format is invalid
  191.      * @see Inflater#needsInput
  192.      * @see Inflater#needsDictionary
  193.      */
  194.     public int inflate(byte[] b) throws DataFormatException {
  195.     return inflate(b, 0, b.length);
  196.     }
  197.  
  198.     /**
  199.      * Returns the ADLER-32 value of the uncompressed data.
  200.      */
  201.     public synchronized native int getAdler();
  202.  
  203.     /**
  204.      * Returns the total number of bytes input so far.
  205.      */
  206.     public synchronized native int getTotalIn();
  207.  
  208.     /**
  209.      * Returns the total number of bytes output so far.
  210.      */
  211.     public synchronized native int getTotalOut();
  212.  
  213.     /**
  214.      * Resets inflater so that a new set of input data can be processed.
  215.      */
  216.     public synchronized native void reset();
  217.  
  218.     /**
  219.      * Discards unprocessed input and frees internal data.
  220.      */
  221.     public synchronized native void end();
  222.  
  223.     /**
  224.      * Frees the decompressor when garbage is collected.
  225.      */
  226.     protected void finalize() {
  227.     end();
  228.     }
  229.  
  230.     private native void init(boolean nowrap);
  231. }
  232.