001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.zip;
027
028 /**
029 * This class provides support for general purpose decompression using the
030 * popular ZLIB compression library. The ZLIB compression library was
031 * initially developed as part of the PNG graphics standard and is not
032 * protected by patents. It is fully described in the specifications at
033 * the <a href="package-summary.html#package_description">java.util.zip
034 * package description</a>.
035 *
036 * <p>The following code fragment demonstrates a trivial compression
037 * and decompression of a string using <tt>Deflater</tt> and
038 * <tt>Inflater</tt>.
039 *
040 * <blockquote><pre>
041 * try {
042 * // Encode a String into bytes
043 * String inputString = "blahblahblah\u20AC\u20AC";
044 * byte[] input = inputString.getBytes("UTF-8");
045 *
046 * // Compress the bytes
047 * byte[] output = new byte[100];
048 * Deflater compresser = new Deflater();
049 * compresser.setInput(input);
050 * compresser.finish();
051 * int compressedDataLength = compresser.deflate(output);
052 *
053 * // Decompress the bytes
054 * Inflater decompresser = new Inflater();
055 * decompresser.setInput(output, 0, compressedDataLength);
056 * byte[] result = new byte[100];
057 * int resultLength = decompresser.inflate(result);
058 * decompresser.end();
059 *
060 * // Decode the bytes into a String
061 * String outputString = new String(result, 0, resultLength, "UTF-8");
062 * } catch(java.io.UnsupportedEncodingException ex) {
063 * // handle
064 * } catch (java.util.zip.DataFormatException ex) {
065 * // handle
066 * }
067 * </pre></blockquote>
068 *
069 * @see Deflater
070 * @version 1.53, 05/05/07
071 * @author David Connelly
072 *
073 */
074 public class Inflater {
075 private long strm;
076 private byte[] buf = new byte[0];
077 private int off, len;
078 private boolean finished;
079 private boolean needDict;
080
081 static {
082 /* Zip library is loaded from System.initializeSystemClass */
083 initIDs();
084 }
085
086 /**
087 * Creates a new decompressor. If the parameter 'nowrap' is true then
088 * the ZLIB header and checksum fields will not be used. This provides
089 * compatibility with the compression format used by both GZIP and PKZIP.
090 * <p>
091 * Note: When using the 'nowrap' option it is also necessary to provide
092 * an extra "dummy" byte as input. This is required by the ZLIB native
093 * library in order to support certain optimizations.
094 *
095 * @param nowrap if true then support GZIP compatible compression
096 */
097 public Inflater(boolean nowrap) {
098 strm = init(nowrap);
099 }
100
101 /**
102 * Creates a new decompressor.
103 */
104 public Inflater() {
105 this (false);
106 }
107
108 /**
109 * Sets input data for decompression. Should be called whenever
110 * needsInput() returns true indicating that more input data is
111 * required.
112 * @param b the input data bytes
113 * @param off the start offset of the input data
114 * @param len the length of the input data
115 * @see Inflater#needsInput
116 */
117 public synchronized void setInput(byte[] b, int off, int len) {
118 if (b == null) {
119 throw new NullPointerException();
120 }
121 if (off < 0 || len < 0 || off > b.length - len) {
122 throw new ArrayIndexOutOfBoundsException();
123 }
124 this .buf = b;
125 this .off = off;
126 this .len = len;
127 }
128
129 /**
130 * Sets input data for decompression. Should be called whenever
131 * needsInput() returns true indicating that more input data is
132 * required.
133 * @param b the input data bytes
134 * @see Inflater#needsInput
135 */
136 public void setInput(byte[] b) {
137 setInput(b, 0, b.length);
138 }
139
140 /**
141 * Sets the preset dictionary to the given array of bytes. Should be
142 * called when inflate() returns 0 and needsDictionary() returns true
143 * indicating that a preset dictionary is required. The method getAdler()
144 * can be used to get the Adler-32 value of the dictionary needed.
145 * @param b the dictionary data bytes
146 * @param off the start offset of the data
147 * @param len the length of the data
148 * @see Inflater#needsDictionary
149 * @see Inflater#getAdler
150 */
151 public synchronized void setDictionary(byte[] b, int off, int len) {
152 if (strm == 0 || b == null) {
153 throw new NullPointerException();
154 }
155 if (off < 0 || len < 0 || off > b.length - len) {
156 throw new ArrayIndexOutOfBoundsException();
157 }
158 setDictionary(strm, b, off, len);
159 needDict = false;
160 }
161
162 /**
163 * Sets the preset dictionary to the given array of bytes. Should be
164 * called when inflate() returns 0 and needsDictionary() returns true
165 * indicating that a preset dictionary is required. The method getAdler()
166 * can be used to get the Adler-32 value of the dictionary needed.
167 * @param b the dictionary data bytes
168 * @see Inflater#needsDictionary
169 * @see Inflater#getAdler
170 */
171 public void setDictionary(byte[] b) {
172 setDictionary(b, 0, b.length);
173 }
174
175 /**
176 * Returns the total number of bytes remaining in the input buffer.
177 * This can be used to find out what bytes still remain in the input
178 * buffer after decompression has finished.
179 * @return the total number of bytes remaining in the input buffer
180 */
181 public synchronized int getRemaining() {
182 return len;
183 }
184
185 /**
186 * Returns true if no data remains in the input buffer. This can
187 * be used to determine if #setInput should be called in order
188 * to provide more input.
189 * @return true if no data remains in the input buffer
190 */
191 public synchronized boolean needsInput() {
192 return len <= 0;
193 }
194
195 /**
196 * Returns true if a preset dictionary is needed for decompression.
197 * @return true if a preset dictionary is needed for decompression
198 * @see Inflater#setDictionary
199 */
200 public synchronized boolean needsDictionary() {
201 return needDict;
202 }
203
204 /**
205 * Returns true if the end of the compressed data stream has been
206 * reached.
207 * @return true if the end of the compressed data stream has been
208 * reached
209 */
210 public synchronized boolean finished() {
211 return finished;
212 }
213
214 /**
215 * Uncompresses bytes into specified buffer. Returns actual number
216 * of bytes uncompressed. A return value of 0 indicates that
217 * needsInput() or needsDictionary() should be called in order to
218 * determine if more input data or a preset dictionary is required.
219 * In the latter case, getAdler() can be used to get the Adler-32
220 * value of the dictionary required.
221 * @param b the buffer for the uncompressed data
222 * @param off the start offset of the data
223 * @param len the maximum number of uncompressed bytes
224 * @return the actual number of uncompressed bytes
225 * @exception DataFormatException if the compressed data format is invalid
226 * @see Inflater#needsInput
227 * @see Inflater#needsDictionary
228 */
229 public synchronized int inflate(byte[] b, int off, int len)
230 throws DataFormatException {
231 if (b == null) {
232 throw new NullPointerException();
233 }
234 if (off < 0 || len < 0 || off > b.length - len) {
235 throw new ArrayIndexOutOfBoundsException();
236 }
237 return inflateBytes(b, off, len);
238 }
239
240 /**
241 * Uncompresses bytes into specified buffer. Returns actual number
242 * of bytes uncompressed. A return value of 0 indicates that
243 * needsInput() or needsDictionary() should be called in order to
244 * determine if more input data or a preset dictionary is required.
245 * In the latter case, getAdler() can be used to get the Adler-32
246 * value of the dictionary required.
247 * @param b the buffer for the uncompressed data
248 * @return the actual number of uncompressed bytes
249 * @exception DataFormatException if the compressed data format is invalid
250 * @see Inflater#needsInput
251 * @see Inflater#needsDictionary
252 */
253 public int inflate(byte[] b) throws DataFormatException {
254 return inflate(b, 0, b.length);
255 }
256
257 /**
258 * Returns the ADLER-32 value of the uncompressed data.
259 * @return the ADLER-32 value of the uncompressed data
260 */
261 public synchronized int getAdler() {
262 ensureOpen();
263 return getAdler(strm);
264 }
265
266 /**
267 * Returns the total number of compressed bytes input so far.
268 *
269 * <p>Since the number of bytes may be greater than
270 * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
271 * the preferred means of obtaining this information.</p>
272 *
273 * @return the total number of compressed bytes input so far
274 */
275 public int getTotalIn() {
276 return (int) getBytesRead();
277 }
278
279 /**
280 * Returns the total number of compressed bytes input so far.</p>
281 *
282 * @return the total (non-negative) number of compressed bytes input so far
283 * @since 1.5
284 */
285 public synchronized long getBytesRead() {
286 ensureOpen();
287 return getBytesRead(strm);
288 }
289
290 /**
291 * Returns the total number of uncompressed bytes output so far.
292 *
293 * <p>Since the number of bytes may be greater than
294 * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
295 * the preferred means of obtaining this information.</p>
296 *
297 * @return the total number of uncompressed bytes output so far
298 */
299 public int getTotalOut() {
300 return (int) getBytesWritten();
301 }
302
303 /**
304 * Returns the total number of uncompressed bytes output so far.</p>
305 *
306 * @return the total (non-negative) number of uncompressed bytes output so far
307 * @since 1.5
308 */
309 public synchronized long getBytesWritten() {
310 ensureOpen();
311 return getBytesWritten(strm);
312 }
313
314 /**
315 * Resets inflater so that a new set of input data can be processed.
316 */
317 public synchronized void reset() {
318 ensureOpen();
319 reset(strm);
320 finished = false;
321 needDict = false;
322 off = len = 0;
323 }
324
325 /**
326 * Closes the decompressor and discards any unprocessed input.
327 * This method should be called when the decompressor is no longer
328 * being used, but will also be called automatically by the finalize()
329 * method. Once this method is called, the behavior of the Inflater
330 * object is undefined.
331 */
332 public synchronized void end() {
333 if (strm != 0) {
334 end(strm);
335 strm = 0;
336 buf = null;
337 }
338 }
339
340 /**
341 * Closes the decompressor when garbage is collected.
342 */
343 protected void finalize() {
344 end();
345 }
346
347 private void ensureOpen() {
348 if (strm == 0)
349 throw new NullPointerException();
350 }
351
352 private native static void initIDs();
353
354 private native static long init(boolean nowrap);
355
356 private native static void setDictionary(long strm, byte[] b,
357 int off, int len);
358
359 private native int inflateBytes(byte[] b, int off, int len)
360 throws DataFormatException;
361
362 private native static int getAdler(long strm);
363
364 private native static long getBytesRead(long strm);
365
366 private native static long getBytesWritten(long strm);
367
368 private native static void reset(long strm);
369
370 private native static void end(long strm);
371 }
|