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 / Inflater.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.5 KB  |  286 lines

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