001    /* Inflater.java - Decompress a data stream
002       Copyright (C) 1999, 2000, 2001, 2003  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010     
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package java.util.zip;
039    
040    import gnu.gcj.RawData;
041    
042    /* Written using on-line Java Platform 1.2 API Specification
043     * and JCL book.
044     * Believed complete and correct.
045     */
046    
047    /**
048     * Inflater is used to decompress data that has been compressed according 
049     * to the "deflate" standard described in rfc1950.
050     *
051     * The usage is as following.  First you have to set some input with
052     * <code>setInput()</code>, then inflate() it.  If inflate doesn't
053     * inflate any bytes there may be three reasons:
054     * <ul>
055     * <li>needsInput() returns true because the input buffer is empty.
056     * You have to provide more input with <code>setInput()</code>.  
057     * NOTE: needsInput() also returns true when, the stream is finished.
058     * </li>
059     * <li>needsDictionary() returns true, you have to provide a preset 
060     *     dictionary with <code>setDictionary()</code>.</li>
061     * <li>finished() returns true, the inflater has finished.</li>
062     * </ul>
063     * Once the first output byte is produced, a dictionary will not be
064     * needed at a later stage.
065     *
066     * @author John Leuner, Jochen Hoenicke
067     * @author Tom Tromey
068     * @date May 17, 1999
069     * @since JDK 1.1
070     */
071    public class Inflater
072    {
073      // The zlib stream.
074      private RawData zstream;
075    
076      // True if finished.
077      private boolean is_finished;
078    
079      // True if dictionary needed.
080      private boolean dict_needed;
081    
082      /**
083       * Creates a new inflater.
084       */
085      public Inflater ()
086      {
087        this (false);
088      }
089    
090      /**
091       * Creates a new inflater.
092       * @param nowrap true if no header and checksum field appears in the
093       * stream.  This is used for GZIPed input.  For compatibility with
094       * Sun JDK you should provide one byte of input more than needed in
095       * this case.
096       */
097      public Inflater (boolean noHeader)
098      {
099        init (noHeader);
100      }
101    
102      /**
103       * Finalizes this object.
104       */
105      protected void finalize ()
106      {
107        end ();
108      }
109    
110      /**
111       * Frees all objects allocated by the inflater.  There's no reason
112       * to call this, since you can just rely on garbage collection (even
113       * for the Sun implementation).  Exists only for compatibility
114       * with Sun's JDK, where the compressor allocates native memory.
115       * If you call any method (even reset) afterwards the behaviour is
116       * <i>undefined</i>.  
117       * @deprecated Just clear all references to inflater instead.
118       */
119      public native void end ();
120    
121      /**
122       * Returns true, if the inflater has finished.  This means, that no
123       * input is needed and no output can be produced.
124       */
125      public synchronized boolean finished ()
126      {
127        return is_finished;
128      }
129    
130      /**
131       * Gets the adler checksum.  This is either the checksum of all
132       * uncompressed bytes returned by inflate(), or if needsDictionary()
133       * returns true (and thus no output was yet produced) this is the
134       * adler checksum of the expected dictionary.
135       * @returns the adler checksum.
136       */
137      public native int getAdler ();
138      
139      /**
140       * Gets the number of unprocessed input.  Useful, if the end of the
141       * stream is reached and you want to further process the bytes after
142       * the deflate stream.  
143       * @return the number of bytes of the input which were not processed.
144       */
145      public native int getRemaining ();
146      
147      /**
148       * Gets the total number of processed compressed input bytes.
149       * @return the total number of bytes of processed input bytes.
150       */
151      public native int getTotalIn ();
152    
153      /**
154       * Gets the total number of output bytes returned by inflate().
155       * @return the total number of output bytes.
156       */
157      public native int getTotalOut ();
158    
159      /**
160       * Inflates the compressed stream to the output buffer.  If this
161       * returns 0, you should check, whether needsDictionary(),
162       * needsInput() or finished() returns true, to determine why no 
163       * further output is produced.
164       * @param buffer the output buffer.
165       * @return the number of bytes written to the buffer, 0 if no further
166       * output can be produced.  
167       * @exception DataFormatException if deflated stream is invalid.
168       * @exception IllegalArgumentException if buf has length 0.
169       */
170      public int inflate (byte[] buf) throws DataFormatException
171      {
172        return inflate (buf, 0, buf.length);
173      }
174    
175      /**
176       * Inflates the compressed stream to the output buffer.  If this
177       * returns 0, you should check, whether needsDictionary(),
178       * needsInput() or finished() returns true, to determine why no 
179       * further output is produced.
180       * @param buffer the output buffer.
181       * @param off the offset into buffer where the output should start.
182       * @param len the maximum length of the output.
183       * @return the number of bytes written to the buffer, 0 if no further
184       * output can be produced.  
185       * @exception DataFormatException if deflated stream is invalid.
186       * @exception IndexOutOfBoundsException if the off and/or len are wrong.
187       */
188      public native int inflate (byte[] buf, int off, int len)
189        throws DataFormatException;
190    
191      private native void init (boolean noHeader);
192    
193      /**
194       * Returns true, if a preset dictionary is needed to inflate the input.
195       */
196      public synchronized boolean needsDictionary ()
197      {
198        return dict_needed;
199      }
200    
201      /**
202       * Returns true, if the input buffer is empty.
203       * You should then call setInput(). <br>
204       *
205       * <em>NOTE</em>: This method also returns true when the stream is finished.
206       */
207      public synchronized boolean needsInput ()
208      {
209        return getRemaining () == 0;
210      }
211    
212      /**
213       * Resets the inflater so that a new stream can be decompressed.  All
214       * pending input and output will be discarded.
215       */
216      public native void reset ();
217    
218      /**
219       * Sets the preset dictionary.  This should only be called, if
220       * needsDictionary() returns true and it should set the same
221       * dictionary, that was used for deflating.  The getAdler()
222       * function returns the checksum of the dictionary needed.
223       * @param buffer the dictionary.
224       * @exception IllegalStateException if no dictionary is needed.
225       * @exception IllegalArgumentException if the dictionary checksum is
226       * wrong.  
227       */
228      public void setDictionary (byte[] buf)
229      {
230        setDictionary (buf, 0, buf.length);
231      }
232    
233      /**
234       * Sets the preset dictionary.  This should only be called, if
235       * needsDictionary() returns true and it should set the same
236       * dictionary, that was used for deflating.  The getAdler()
237       * function returns the checksum of the dictionary needed.
238       * @param buffer the dictionary.
239       * @param off the offset into buffer where the dictionary starts.
240       * @param len the length of the dictionary.
241       * @exception IllegalStateException if no dictionary is needed.
242       * @exception IllegalArgumentException if the dictionary checksum is
243       * wrong.  
244       * @exception IndexOutOfBoundsException if the off and/or len are wrong.
245       */
246      public native void setDictionary (byte[] buf, int off, int len);
247    
248      /**
249       * Sets the input.  This should only be called, if needsInput()
250       * returns true.
251       * @param buffer the input.
252       * @exception IllegalStateException if no input is needed.
253       */
254      public void setInput (byte[] buf) 
255      {
256        setInput (buf, 0, buf.length);
257      }
258    
259      /**
260       * Sets the input.  This should only be called, if needsInput()
261       * returns true.
262       * @param buffer the input.
263       * @param off the offset into buffer where the input starts.
264       * @param len the length of the input.  
265       * @exception IllegalStateException if no input is needed.
266       * @exception IndexOutOfBoundsException if the off and/or len are wrong.
267       */
268      public native void setInput (byte[] buf, int off, int len);
269    }