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;
28 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
32 * This class provides support for general purpose decompression using the
33 * popular ZLIB compression library. The ZLIB compression library was
34 * initially developed as part of the PNG graphics standard and is not
35 * protected by patents. It is fully described in the specifications at
36 * the <a href="package-summary.html#package_description">java.util.zip
37 * package description</a>.
39 * <p>The following code fragment demonstrates a trivial compression
40 * and decompression of a string using <tt>Deflater</tt> and
45 * // Encode a String into bytes
46 * String inputString = "blahblahblah\u20AC\u20AC";
47 * byte[] input = inputString.getBytes("UTF-8");
49 * // Compress the bytes
50 * byte[] output = new byte[100];
51 * Deflater compresser = new Deflater();
52 * compresser.setInput(input);
53 * compresser.finish();
54 * int compressedDataLength = compresser.deflate(output);
56 * // Decompress the bytes
57 * Inflater decompresser = new Inflater();
58 * decompresser.setInput(output, 0, compressedDataLength);
59 * byte[] result = new byte[100];
60 * int resultLength = decompresser.inflate(result);
63 * // Decode the bytes into a String
64 * String outputString = new String(result, 0, resultLength, "UTF-8");
65 * } catch(java.io.UnsupportedEncodingException ex) {
67 * } catch (java.util.zip.DataFormatException ex) {
73 * @author David Connelly
77 resource = "/org/apidesign/vm4brwsr/emul/zip/js-inflate.min.js"
81 private String data = "";
84 private boolean finished;
85 private boolean needDict;
87 private static final byte[] defaultBuf = new byte[0];
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) {
104 * Creates a new decompressor.
111 * Sets input data for decompression. Should be called whenever
112 * needsInput() returns true indicating that more input data is
114 * @param b the input data bytes
115 * @param off the start offset of the input data
116 * @param len the length of the input data
117 * @see Inflater#needsInput
119 public void setInput(byte[] b, int off, int len) {
121 throw new NullPointerException();
123 if (off < 0 || len < 0 || off > b.length - len) {
124 throw new ArrayIndexOutOfBoundsException();
126 data = (String) infl(b, off, len);
130 * Sets input data for decompression. Should be called whenever
131 * needsInput() returns true indicating that more input data is
133 * @param b the input data bytes
134 * @see Inflater#needsInput
136 public void setInput(byte[] b) {
137 setInput(b, 0, b.length);
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
151 public void setDictionary(byte[] b, int off, int len) {
153 throw new NullPointerException();
155 if (off < 0 || len < 0 || off > b.length - len) {
156 throw new ArrayIndexOutOfBoundsException();
162 * Sets the preset dictionary to the given array of bytes. Should be
163 * called when inflate() returns 0 and needsDictionary() returns true
164 * indicating that a preset dictionary is required. The method getAdler()
165 * can be used to get the Adler-32 value of the dictionary needed.
166 * @param b the dictionary data bytes
167 * @see Inflater#needsDictionary
168 * @see Inflater#getAdler
170 public void setDictionary(byte[] b) {
171 setDictionary(b, 0, b.length);
175 * Returns the total number of bytes remaining in the input buffer.
176 * This can be used to find out what bytes still remain in the input
177 * buffer after decompression has finished.
178 * @return the total number of bytes remaining in the input buffer
180 public int getRemaining() {
181 return data.length() - offset;
185 * Returns true if no data remains in the input buffer. This can
186 * be used to determine if #setInput should be called in order
187 * to provide more input.
188 * @return true if no data remains in the input buffer
190 public boolean needsInput() {
191 return getRemaining() <= 0;
195 * Returns true if a preset dictionary is needed for decompression.
196 * @return true if a preset dictionary is needed for decompression
197 * @see Inflater#setDictionary
199 public boolean needsDictionary() {
204 * Returns true if the end of the compressed data stream has been
206 * @return true if the end of the compressed data stream has been
209 public boolean finished() {
214 * Uncompresses bytes into specified buffer. Returns actual number
215 * of bytes uncompressed. A return value of 0 indicates that
216 * needsInput() or needsDictionary() should be called in order to
217 * determine if more input data or a preset dictionary is required.
218 * In the latter case, getAdler() can be used to get the Adler-32
219 * value of the dictionary required.
220 * @param b the buffer for the uncompressed data
221 * @param off the start offset of the data
222 * @param len the maximum number of uncompressed bytes
223 * @return the actual number of uncompressed bytes
224 * @exception DataFormatException if the compressed data format is invalid
225 * @see Inflater#needsInput
226 * @see Inflater#needsDictionary
228 public int inflate(byte[] b, int off, int len)
229 throws DataFormatException
232 throw new NullPointerException();
234 if (off < 0 || len < 0 || off > b.length - len) {
235 throw new ArrayIndexOutOfBoundsException();
238 while (offset < data.length()) {
239 b[off++] = (byte)data.charAt(offset++);
246 @JavaScriptBody(args = { "arr", "offset", "len" }, body =
248 + "r.charCodeAt = function(idx) { return arr[offset + idx]; };\n"
249 + "return JSInflate.inflate(r);"
251 private static native Object infl(byte[] arr, int offset, int len);
254 * Uncompresses bytes into specified buffer. Returns actual number
255 * of bytes uncompressed. A return value of 0 indicates that
256 * needsInput() or needsDictionary() should be called in order to
257 * determine if more input data or a preset dictionary is required.
258 * In the latter case, getAdler() can be used to get the Adler-32
259 * value of the dictionary required.
260 * @param b the buffer for the uncompressed data
261 * @return the actual number of uncompressed bytes
262 * @exception DataFormatException if the compressed data format is invalid
263 * @see Inflater#needsInput
264 * @see Inflater#needsDictionary
266 public int inflate(byte[] b) throws DataFormatException {
267 return inflate(b, 0, b.length);
271 * Returns the ADLER-32 value of the uncompressed data.
272 * @return the ADLER-32 value of the uncompressed data
274 public int getAdler() {
279 * Returns the total number of compressed bytes input so far.
281 * <p>Since the number of bytes may be greater than
282 * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
283 * the preferred means of obtaining this information.</p>
285 * @return the total number of compressed bytes input so far
287 public int getTotalIn() {
288 return (int) getBytesRead();
292 * Returns the total number of compressed bytes input so far.</p>
294 * @return the total (non-negative) number of compressed bytes input so far
297 public long getBytesRead() {
302 * Returns the total number of uncompressed bytes output so far.
304 * <p>Since the number of bytes may be greater than
305 * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
306 * the preferred means of obtaining this information.</p>
308 * @return the total number of uncompressed bytes output so far
310 public int getTotalOut() {
311 return (int) getBytesWritten();
315 * Returns the total number of uncompressed bytes output so far.</p>
317 * @return the total (non-negative) number of uncompressed bytes output so far
320 public long getBytesWritten() {
325 * Resets inflater so that a new set of input data can be processed.
327 public void reset() {
335 * Closes the decompressor and discards any unprocessed input.
336 * This method should be called when the decompressor is no longer
337 * being used, but will also be called automatically by the finalize()
338 * method. Once this method is called, the behavior of the Inflater
339 * object is undefined.