2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 package java.util.zip;
29 * This class provides support for general purpose decompression using the
30 * popular ZLIB compression library. The ZLIB compression library was
31 * initially developed as part of the PNG graphics standard and is not
32 * protected by patents. It is fully described in the specifications at
33 * the <a href="package-summary.html#package_description">java.util.zip
34 * package description</a>.
36 * <p>The following code fragment demonstrates a trivial compression
37 * and decompression of a string using <tt>Deflater</tt> and
42 * // Encode a String into bytes
43 * String inputString = "blahblahblah\u20AC\u20AC";
44 * byte[] input = inputString.getBytes("UTF-8");
46 * // Compress the bytes
47 * byte[] output = new byte[100];
48 * Deflater compresser = new Deflater();
49 * compresser.setInput(input);
50 * compresser.finish();
51 * int compressedDataLength = compresser.deflate(output);
53 * // Decompress the bytes
54 * Inflater decompresser = new Inflater();
55 * decompresser.setInput(output, 0, compressedDataLength);
56 * byte[] result = new byte[100];
57 * int resultLength = decompresser.inflate(result);
60 * // Decode the bytes into a String
61 * String outputString = new String(result, 0, resultLength, "UTF-8");
62 * } catch(java.io.UnsupportedEncodingException ex) {
64 * } catch (java.util.zip.DataFormatException ex) {
70 * @author David Connelly
76 private final ZStreamRef zsRef;
77 private byte[] buf = defaultBuf;
79 private boolean finished;
80 private boolean needDict;
82 private static final byte[] defaultBuf = new byte[0];
85 /* Zip library is loaded from System.initializeSystemClass */
90 * Creates a new decompressor. If the parameter 'nowrap' is true then
91 * the ZLIB header and checksum fields will not be used. This provides
92 * compatibility with the compression format used by both GZIP and PKZIP.
94 * Note: When using the 'nowrap' option it is also necessary to provide
95 * an extra "dummy" byte as input. This is required by the ZLIB native
96 * library in order to support certain optimizations.
98 * @param nowrap if true then support GZIP compatible compression
100 public Inflater(boolean nowrap) {
101 zsRef = new ZStreamRef(init(nowrap));
105 * Creates a new decompressor.
112 * Sets input data for decompression. Should be called whenever
113 * needsInput() returns true indicating that more input data is
115 * @param b the input data bytes
116 * @param off the start offset of the input data
117 * @param len the length of the input data
118 * @see Inflater#needsInput
120 public void setInput(byte[] b, int off, int len) {
122 throw new NullPointerException();
124 if (off < 0 || len < 0 || off > b.length - len) {
125 throw new ArrayIndexOutOfBoundsException();
127 synchronized (zsRef) {
135 * Sets input data for decompression. Should be called whenever
136 * needsInput() returns true indicating that more input data is
138 * @param b the input data bytes
139 * @see Inflater#needsInput
141 public void setInput(byte[] b) {
142 setInput(b, 0, b.length);
146 * Sets the preset dictionary to the given array of bytes. Should be
147 * called when inflate() returns 0 and needsDictionary() returns true
148 * indicating that a preset dictionary is required. The method getAdler()
149 * can be used to get the Adler-32 value of the dictionary needed.
150 * @param b the dictionary data bytes
151 * @param off the start offset of the data
152 * @param len the length of the data
153 * @see Inflater#needsDictionary
154 * @see Inflater#getAdler
156 public void setDictionary(byte[] b, int off, int len) {
158 throw new NullPointerException();
160 if (off < 0 || len < 0 || off > b.length - len) {
161 throw new ArrayIndexOutOfBoundsException();
163 synchronized (zsRef) {
165 setDictionary(zsRef.address(), b, off, len);
171 * Sets the preset dictionary to the given array of bytes. Should be
172 * called when inflate() returns 0 and needsDictionary() returns true
173 * indicating that a preset dictionary is required. The method getAdler()
174 * can be used to get the Adler-32 value of the dictionary needed.
175 * @param b the dictionary data bytes
176 * @see Inflater#needsDictionary
177 * @see Inflater#getAdler
179 public void setDictionary(byte[] b) {
180 setDictionary(b, 0, b.length);
184 * Returns the total number of bytes remaining in the input buffer.
185 * This can be used to find out what bytes still remain in the input
186 * buffer after decompression has finished.
187 * @return the total number of bytes remaining in the input buffer
189 public int getRemaining() {
190 synchronized (zsRef) {
196 * Returns true if no data remains in the input buffer. This can
197 * be used to determine if #setInput should be called in order
198 * to provide more input.
199 * @return true if no data remains in the input buffer
201 public boolean needsInput() {
202 synchronized (zsRef) {
208 * Returns true if a preset dictionary is needed for decompression.
209 * @return true if a preset dictionary is needed for decompression
210 * @see Inflater#setDictionary
212 public boolean needsDictionary() {
213 synchronized (zsRef) {
219 * Returns true if the end of the compressed data stream has been
221 * @return true if the end of the compressed data stream has been
224 public boolean finished() {
225 synchronized (zsRef) {
231 * Uncompresses bytes into specified buffer. Returns actual number
232 * of bytes uncompressed. A return value of 0 indicates that
233 * needsInput() or needsDictionary() should be called in order to
234 * determine if more input data or a preset dictionary is required.
235 * In the latter case, getAdler() can be used to get the Adler-32
236 * value of the dictionary required.
237 * @param b the buffer for the uncompressed data
238 * @param off the start offset of the data
239 * @param len the maximum number of uncompressed bytes
240 * @return the actual number of uncompressed bytes
241 * @exception DataFormatException if the compressed data format is invalid
242 * @see Inflater#needsInput
243 * @see Inflater#needsDictionary
245 public int inflate(byte[] b, int off, int len)
246 throws DataFormatException
249 throw new NullPointerException();
251 if (off < 0 || len < 0 || off > b.length - len) {
252 throw new ArrayIndexOutOfBoundsException();
254 synchronized (zsRef) {
256 return inflateBytes(zsRef.address(), b, off, len);
261 * Uncompresses bytes into specified buffer. Returns actual number
262 * of bytes uncompressed. A return value of 0 indicates that
263 * needsInput() or needsDictionary() should be called in order to
264 * determine if more input data or a preset dictionary is required.
265 * In the latter case, getAdler() can be used to get the Adler-32
266 * value of the dictionary required.
267 * @param b the buffer for the uncompressed data
268 * @return the actual number of uncompressed bytes
269 * @exception DataFormatException if the compressed data format is invalid
270 * @see Inflater#needsInput
271 * @see Inflater#needsDictionary
273 public int inflate(byte[] b) throws DataFormatException {
274 return inflate(b, 0, b.length);
278 * Returns the ADLER-32 value of the uncompressed data.
279 * @return the ADLER-32 value of the uncompressed data
281 public int getAdler() {
282 synchronized (zsRef) {
284 return getAdler(zsRef.address());
289 * Returns the total number of compressed bytes input so far.
291 * <p>Since the number of bytes may be greater than
292 * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
293 * the preferred means of obtaining this information.</p>
295 * @return the total number of compressed bytes input so far
297 public int getTotalIn() {
298 return (int) getBytesRead();
302 * Returns the total number of compressed bytes input so far.</p>
304 * @return the total (non-negative) number of compressed bytes input so far
307 public long getBytesRead() {
308 synchronized (zsRef) {
310 return getBytesRead(zsRef.address());
315 * Returns the total number of uncompressed bytes output so far.
317 * <p>Since the number of bytes may be greater than
318 * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
319 * the preferred means of obtaining this information.</p>
321 * @return the total number of uncompressed bytes output so far
323 public int getTotalOut() {
324 return (int) getBytesWritten();
328 * Returns the total number of uncompressed bytes output so far.</p>
330 * @return the total (non-negative) number of uncompressed bytes output so far
333 public long getBytesWritten() {
334 synchronized (zsRef) {
336 return getBytesWritten(zsRef.address());
341 * Resets inflater so that a new set of input data can be processed.
343 public void reset() {
344 synchronized (zsRef) {
346 reset(zsRef.address());
355 * Closes the decompressor and discards any unprocessed input.
356 * This method should be called when the decompressor is no longer
357 * being used, but will also be called automatically by the finalize()
358 * method. Once this method is called, the behavior of the Inflater
359 * object is undefined.
362 synchronized (zsRef) {
363 long addr = zsRef.address();
373 * Closes the decompressor when garbage is collected.
375 protected void finalize() {
379 private void ensureOpen () {
380 assert Thread.holdsLock(zsRef);
381 if (zsRef.address() == 0)
382 throw new NullPointerException("Inflater has been closed");
386 synchronized (zsRef) {
387 return zsRef.address() == 0;
391 private native static void initIDs();
392 private native static long init(boolean nowrap);
393 private native static void setDictionary(long addr, byte[] b, int off,
395 private native int inflateBytes(long addr, byte[] b, int off, int len)
396 throws DataFormatException;
397 private native static int getAdler(long addr);
398 private native static long getBytesRead(long addr);
399 private native static long getBytesWritten(long addr);
400 private native static void reset(long addr);
401 private native static void end(long addr);