KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > zip > Inflater


1 /*
2  * @(#)Inflater.java 1.44 05/11/11
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.zip;
9
10 /**
11  * This class provides support for general purpose decompression using the
12  * popular ZLIB compression library. The ZLIB compression library was
13  * initially developed as part of the PNG graphics standard and is not
14  * protected by patents. It is fully described in the specifications at
15  * the <a HREF="package-summary.html#package_description">java.util.zip
16  * package description</a>.
17  *
18  * <p>The following code fragment demonstrates a trivial compression
19  * and decompression of a string using <tt>Deflater</tt> and
20  * <tt>Inflater</tt>.
21  *
22  * <blockquote><pre>
23  * // Encode a String into bytes
24  * String inputString = "blahblahblah€€";
25  * byte[] input = inputString.getBytes("UTF-8");
26  *
27  * // Compress the bytes
28  * byte[] output = new byte[100];
29  * Deflater compresser = new Deflater();
30  * compresser.setInput(input);
31  * compresser.finish();
32  * int compressedDataLength = compresser.deflate(output);
33  *
34  * // Decompress the bytes
35  * Inflater decompresser = new Inflater();
36  * decompresser.setInput(output, 0, compressedDataLength);
37  * byte[] result = new byte[100];
38  * int resultLength = decompresser.inflate(result);
39  * decompresser.end();
40  *
41  * // Decode the bytes into a String
42  * String outputString = new String(result, 0, resultLength, "UTF-8");
43  * </pre></blockquote>
44  *
45  * @see Deflater
46  * @version 1.44, 11/11/05
47  * @author David Connelly
48  *
49  */

50 public
51 class Inflater {
52     private long strm;
53     private byte[] buf = new byte[0];
54     private int off, len;
55     private boolean finished;
56     private boolean needDict;
57
58     static {
59     /* Zip library is loaded from System.initializeSystemClass */
60     initIDs();
61     }
62
63     /**
64      * Creates a new decompressor. If the parameter 'nowrap' is true then
65      * the ZLIB header and checksum fields will not be used. This provides
66      * compatibility with the compression format used by both GZIP and PKZIP.
67      * <p>
68      * Note: When using the 'nowrap' option it is also necessary to provide
69      * an extra "dummy" byte as input. This is required by the ZLIB native
70      * library in order to support certain optimizations.
71      *
72      * @param nowrap if true then support GZIP compatible compression
73      */

74     public Inflater(boolean nowrap) {
75     strm = init(nowrap);
76     }
77
78     /**
79      * Creates a new decompressor.
80      */

81     public Inflater() {
82     this(false);
83     }
84
85     /**
86      * Sets input data for decompression. Should be called whenever
87      * needsInput() returns true indicating that more input data is
88      * required.
89      * @param b the input data bytes
90      * @param off the start offset of the input data
91      * @param len the length of the input data
92      * @see Inflater#needsInput
93      */

94     public synchronized void setInput(byte[] b, int off, int len) {
95     if (b == null) {
96         throw new NullPointerException JavaDoc();
97     }
98     if (off < 0 || len < 0 || off > b.length - len) {
99         throw new ArrayIndexOutOfBoundsException JavaDoc();
100     }
101     this.buf = b;
102     this.off = off;
103     this.len = len;
104     }
105
106     /**
107      * Sets input data for decompression. Should be called whenever
108      * needsInput() returns true indicating that more input data is
109      * required.
110      * @param b the input data bytes
111      * @see Inflater#needsInput
112      */

113     public void setInput(byte[] b) {
114     setInput(b, 0, b.length);
115     }
116
117     /**
118      * Sets the preset dictionary to the given array of bytes. Should be
119      * called when inflate() returns 0 and needsDictionary() returns true
120      * indicating that a preset dictionary is required. The method getAdler()
121      * can be used to get the Adler-32 value of the dictionary needed.
122      * @param b the dictionary data bytes
123      * @param off the start offset of the data
124      * @param len the length of the data
125      * @see Inflater#needsDictionary
126      * @see Inflater#getAdler
127      */

128     public synchronized void setDictionary(byte[] b, int off, int len) {
129     if (strm == 0 || b == null) {
130         throw new NullPointerException JavaDoc();
131     }
132     if (off < 0 || len < 0 || off > b.length - len) {
133         throw new ArrayIndexOutOfBoundsException JavaDoc();
134     }
135     setDictionary(strm, b, off, len);
136     needDict = false;
137     }
138
139     /**
140      * Sets the preset dictionary to the given array of bytes. Should be
141      * called when inflate() returns 0 and needsDictionary() returns true
142      * indicating that a preset dictionary is required. The method getAdler()
143      * can be used to get the Adler-32 value of the dictionary needed.
144      * @param b the dictionary data bytes
145      * @see Inflater#needsDictionary
146      * @see Inflater#getAdler
147      */

148     public void setDictionary(byte[] b) {
149     setDictionary(b, 0, b.length);
150     }
151
152     /**
153      * Returns the total number of bytes remaining in the input buffer.
154      * This can be used to find out what bytes still remain in the input
155      * buffer after decompression has finished.
156      * @return the total number of bytes remaining in the input buffer
157      */

158     public synchronized int getRemaining() {
159     return len;
160     }
161
162     /**
163      * Returns true if no data remains in the input buffer. This can
164      * be used to determine if #setInput should be called in order
165      * to provide more input.
166      * @return true if no data remains in the input buffer
167      */

168     public synchronized boolean needsInput() {
169     return len <= 0;
170     }
171
172     /**
173      * Returns true if a preset dictionary is needed for decompression.
174      * @return true if a preset dictionary is needed for decompression
175      * @see Inflater#setDictionary
176      */

177     public synchronized boolean needsDictionary() {
178     return needDict;
179     }
180
181     /**
182      * Returns true if the end of the compressed data stream has been
183      * reached.
184      * @return true if the end of the compressed data stream has been
185      * reached
186      */

187     public synchronized boolean finished() {
188     return finished;
189     }
190
191     /**
192      * Uncompresses bytes into specified buffer. Returns actual number
193      * of bytes uncompressed. A return value of 0 indicates that
194      * needsInput() or needsDictionary() should be called in order to
195      * determine if more input data or a preset dictionary is required.
196      * In the later case, getAdler() can be used to get the Adler-32
197      * value of the dictionary required.
198      * @param b the buffer for the uncompressed data
199      * @param off the start offset of the data
200      * @param len the maximum number of uncompressed bytes
201      * @return the actual number of uncompressed bytes
202      * @exception DataFormatException if the compressed data format is invalid
203      * @see Inflater#needsInput
204      * @see Inflater#needsDictionary
205      */

206     public synchronized int inflate(byte[] b, int off, int len)
207     throws DataFormatException JavaDoc
208     {
209     if (b == null) {
210         throw new NullPointerException JavaDoc();
211     }
212     if (off < 0 || len < 0 || off > b.length - len) {
213         throw new ArrayIndexOutOfBoundsException JavaDoc();
214     }
215     return inflateBytes(b, off, len);
216     }
217
218     /**
219      * Uncompresses bytes into specified buffer. Returns actual number
220      * of bytes uncompressed. A return value of 0 indicates that
221      * needsInput() or needsDictionary() should be called in order to
222      * determine if more input data or a preset dictionary is required.
223      * In the later case, getAdler() can be used to get the Adler-32
224      * value of the dictionary required.
225      * @param b the buffer for the uncompressed data
226      * @return the actual number of uncompressed bytes
227      * @exception DataFormatException if the compressed data format is invalid
228      * @see Inflater#needsInput
229      * @see Inflater#needsDictionary
230      */

231     public int inflate(byte[] b) throws DataFormatException JavaDoc {
232     return inflate(b, 0, b.length);
233     }
234
235     /**
236      * Returns the ADLER-32 value of the uncompressed data.
237      * @return the ADLER-32 value of the uncompressed data
238      */

239     public synchronized int getAdler() {
240     ensureOpen();
241     return getAdler(strm);
242     }
243
244     /**
245      * Returns the total number of compressed bytes input so far.
246      *
247      * <p>Since the number of bytes may be greater than
248      * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
249      * the preferred means of obtaining this information.</p>
250      *
251      * @return the total number of compressed bytes input so far
252      */

253     public int getTotalIn() {
254     return (int) getBytesRead();
255     }
256
257     /**
258      * Returns the total number of compressed bytes input so far.</p>
259      *
260      * @return the total (non-negative) number of compressed bytes input so far
261      */

262     public synchronized long getBytesRead() {
263     ensureOpen();
264     return getBytesRead(strm);
265     }
266
267     /**
268      * Returns the total number of uncompressed bytes output so far.
269      *
270      * <p>Since the number of bytes may be greater than
271      * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
272      * the preferred means of obtaining this information.</p>
273      *
274      * @return the total number of uncompressed bytes output so far
275      */

276     public int getTotalOut() {
277     return (int) getBytesWritten();
278     }
279
280     /**
281      * Returns the total number of uncompressed bytes output so far.</p>
282      *
283      * @return the total (non-negative) number of uncompressed bytes output so far
284      */

285     public synchronized long getBytesWritten() {
286     ensureOpen();
287     return getBytesWritten(strm);
288     }
289
290     /**
291      * Resets inflater so that a new set of input data can be processed.
292      */

293     public synchronized void reset() {
294     ensureOpen();
295     reset(strm);
296     finished = false;
297     needDict = false;
298     off = len = 0;
299     }
300
301     /**
302      * Closes the decompressor and discards any unprocessed input.
303      * This method should be called when the decompressor is no longer
304      * being used, but will also be called automatically by the finalize()
305      * method. Once this method is called, the behavior of the Inflater
306      * object is undefined.
307      */

308     public synchronized void end() {
309     if (strm != 0) {
310         end(strm);
311         strm = 0;
312         buf = null;
313     }
314     }
315
316     /**
317      * Closes the decompressor when garbage is collected.
318      */

319     protected void finalize() {
320     end();
321     }
322
323     private void ensureOpen () {
324     if (strm == 0)
325         throw new NullPointerException JavaDoc();
326     }
327
328     private native static void initIDs();
329     private native static long init(boolean nowrap);
330     private native static void setDictionary(long strm, byte[] b, int off,
331                          int len);
332     private native int inflateBytes(byte[] b, int off, int len)
333         throws DataFormatException JavaDoc;
334     private native static int getAdler(long strm);
335     private native static long getBytesRead(long strm);
336     private native static long getBytesWritten(long strm);
337     private native static void reset(long strm);
338     private native static void end(long strm);
339 }
340
Popular Tags