jaroslav@609
|
1 |
/*
|
jaroslav@609
|
2 |
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@609
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@609
|
4 |
*
|
jaroslav@609
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@609
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@609
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@609
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@609
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@609
|
10 |
*
|
jaroslav@609
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@609
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@609
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@609
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@609
|
15 |
* accompanied this code).
|
jaroslav@609
|
16 |
*
|
jaroslav@609
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@609
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@609
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@609
|
20 |
*
|
jaroslav@609
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@609
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@609
|
23 |
* questions.
|
jaroslav@609
|
24 |
*/
|
jaroslav@609
|
25 |
|
jaroslav@609
|
26 |
package java.util.zip;
|
jaroslav@609
|
27 |
|
jaroslav@611
|
28 |
import org.apidesign.bck2brwsr.core.ExtraJavaScript;
|
jaroslav@611
|
29 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@611
|
30 |
|
jaroslav@609
|
31 |
/**
|
jaroslav@609
|
32 |
* This class provides support for general purpose decompression using the
|
jaroslav@609
|
33 |
* popular ZLIB compression library. The ZLIB compression library was
|
jaroslav@609
|
34 |
* initially developed as part of the PNG graphics standard and is not
|
jaroslav@609
|
35 |
* protected by patents. It is fully described in the specifications at
|
jaroslav@609
|
36 |
* the <a href="package-summary.html#package_description">java.util.zip
|
jaroslav@609
|
37 |
* package description</a>.
|
jaroslav@609
|
38 |
*
|
jaroslav@609
|
39 |
* <p>The following code fragment demonstrates a trivial compression
|
jaroslav@609
|
40 |
* and decompression of a string using <tt>Deflater</tt> and
|
jaroslav@609
|
41 |
* <tt>Inflater</tt>.
|
jaroslav@609
|
42 |
*
|
jaroslav@609
|
43 |
* <blockquote><pre>
|
jaroslav@609
|
44 |
* try {
|
jaroslav@609
|
45 |
* // Encode a String into bytes
|
jaroslav@609
|
46 |
* String inputString = "blahblahblah\u20AC\u20AC";
|
jaroslav@609
|
47 |
* byte[] input = inputString.getBytes("UTF-8");
|
jaroslav@609
|
48 |
*
|
jaroslav@609
|
49 |
* // Compress the bytes
|
jaroslav@609
|
50 |
* byte[] output = new byte[100];
|
jaroslav@609
|
51 |
* Deflater compresser = new Deflater();
|
jaroslav@609
|
52 |
* compresser.setInput(input);
|
jaroslav@609
|
53 |
* compresser.finish();
|
jaroslav@609
|
54 |
* int compressedDataLength = compresser.deflate(output);
|
jaroslav@609
|
55 |
*
|
jaroslav@609
|
56 |
* // Decompress the bytes
|
jaroslav@609
|
57 |
* Inflater decompresser = new Inflater();
|
jaroslav@609
|
58 |
* decompresser.setInput(output, 0, compressedDataLength);
|
jaroslav@609
|
59 |
* byte[] result = new byte[100];
|
jaroslav@609
|
60 |
* int resultLength = decompresser.inflate(result);
|
jaroslav@609
|
61 |
* decompresser.end();
|
jaroslav@609
|
62 |
*
|
jaroslav@609
|
63 |
* // Decode the bytes into a String
|
jaroslav@609
|
64 |
* String outputString = new String(result, 0, resultLength, "UTF-8");
|
jaroslav@609
|
65 |
* } catch(java.io.UnsupportedEncodingException ex) {
|
jaroslav@609
|
66 |
* // handle
|
jaroslav@609
|
67 |
* } catch (java.util.zip.DataFormatException ex) {
|
jaroslav@609
|
68 |
* // handle
|
jaroslav@609
|
69 |
* }
|
jaroslav@609
|
70 |
* </pre></blockquote>
|
jaroslav@609
|
71 |
*
|
jaroslav@609
|
72 |
* @see Deflater
|
jaroslav@609
|
73 |
* @author David Connelly
|
jaroslav@609
|
74 |
*
|
jaroslav@609
|
75 |
*/
|
jaroslav@611
|
76 |
@ExtraJavaScript(
|
jaroslav@611
|
77 |
resource = "/org/apidesign/vm4brwsr/emul/zip/js-inflate.min.js"
|
jaroslav@611
|
78 |
)
|
jaroslav@609
|
79 |
public
|
jaroslav@609
|
80 |
class Inflater {
|
jaroslav@611
|
81 |
private String data = "";
|
jaroslav@611
|
82 |
private int offset;
|
jaroslav@611
|
83 |
private long counter;
|
jaroslav@609
|
84 |
private boolean finished;
|
jaroslav@609
|
85 |
private boolean needDict;
|
jaroslav@609
|
86 |
|
jaroslav@609
|
87 |
private static final byte[] defaultBuf = new byte[0];
|
jaroslav@609
|
88 |
|
jaroslav@609
|
89 |
/**
|
jaroslav@609
|
90 |
* Creates a new decompressor. If the parameter 'nowrap' is true then
|
jaroslav@609
|
91 |
* the ZLIB header and checksum fields will not be used. This provides
|
jaroslav@609
|
92 |
* compatibility with the compression format used by both GZIP and PKZIP.
|
jaroslav@609
|
93 |
* <p>
|
jaroslav@609
|
94 |
* Note: When using the 'nowrap' option it is also necessary to provide
|
jaroslav@609
|
95 |
* an extra "dummy" byte as input. This is required by the ZLIB native
|
jaroslav@609
|
96 |
* library in order to support certain optimizations.
|
jaroslav@609
|
97 |
*
|
jaroslav@609
|
98 |
* @param nowrap if true then support GZIP compatible compression
|
jaroslav@609
|
99 |
*/
|
jaroslav@609
|
100 |
public Inflater(boolean nowrap) {
|
jaroslav@609
|
101 |
}
|
jaroslav@609
|
102 |
|
jaroslav@609
|
103 |
/**
|
jaroslav@609
|
104 |
* Creates a new decompressor.
|
jaroslav@609
|
105 |
*/
|
jaroslav@609
|
106 |
public Inflater() {
|
jaroslav@609
|
107 |
this(false);
|
jaroslav@609
|
108 |
}
|
jaroslav@609
|
109 |
|
jaroslav@609
|
110 |
/**
|
jaroslav@609
|
111 |
* Sets input data for decompression. Should be called whenever
|
jaroslav@609
|
112 |
* needsInput() returns true indicating that more input data is
|
jaroslav@609
|
113 |
* required.
|
jaroslav@609
|
114 |
* @param b the input data bytes
|
jaroslav@609
|
115 |
* @param off the start offset of the input data
|
jaroslav@609
|
116 |
* @param len the length of the input data
|
jaroslav@609
|
117 |
* @see Inflater#needsInput
|
jaroslav@609
|
118 |
*/
|
jaroslav@609
|
119 |
public void setInput(byte[] b, int off, int len) {
|
jaroslav@609
|
120 |
if (b == null) {
|
jaroslav@609
|
121 |
throw new NullPointerException();
|
jaroslav@609
|
122 |
}
|
jaroslav@609
|
123 |
if (off < 0 || len < 0 || off > b.length - len) {
|
jaroslav@609
|
124 |
throw new ArrayIndexOutOfBoundsException();
|
jaroslav@609
|
125 |
}
|
jaroslav@611
|
126 |
data = (String) infl(b, off, len);
|
jaroslav@609
|
127 |
}
|
jaroslav@609
|
128 |
|
jaroslav@609
|
129 |
/**
|
jaroslav@609
|
130 |
* Sets input data for decompression. Should be called whenever
|
jaroslav@609
|
131 |
* needsInput() returns true indicating that more input data is
|
jaroslav@609
|
132 |
* required.
|
jaroslav@609
|
133 |
* @param b the input data bytes
|
jaroslav@609
|
134 |
* @see Inflater#needsInput
|
jaroslav@609
|
135 |
*/
|
jaroslav@609
|
136 |
public void setInput(byte[] b) {
|
jaroslav@609
|
137 |
setInput(b, 0, b.length);
|
jaroslav@609
|
138 |
}
|
jaroslav@609
|
139 |
|
jaroslav@609
|
140 |
/**
|
jaroslav@609
|
141 |
* Sets the preset dictionary to the given array of bytes. Should be
|
jaroslav@609
|
142 |
* called when inflate() returns 0 and needsDictionary() returns true
|
jaroslav@609
|
143 |
* indicating that a preset dictionary is required. The method getAdler()
|
jaroslav@609
|
144 |
* can be used to get the Adler-32 value of the dictionary needed.
|
jaroslav@609
|
145 |
* @param b the dictionary data bytes
|
jaroslav@609
|
146 |
* @param off the start offset of the data
|
jaroslav@609
|
147 |
* @param len the length of the data
|
jaroslav@609
|
148 |
* @see Inflater#needsDictionary
|
jaroslav@609
|
149 |
* @see Inflater#getAdler
|
jaroslav@609
|
150 |
*/
|
jaroslav@609
|
151 |
public void setDictionary(byte[] b, int off, int len) {
|
jaroslav@609
|
152 |
if (b == null) {
|
jaroslav@609
|
153 |
throw new NullPointerException();
|
jaroslav@609
|
154 |
}
|
jaroslav@609
|
155 |
if (off < 0 || len < 0 || off > b.length - len) {
|
jaroslav@609
|
156 |
throw new ArrayIndexOutOfBoundsException();
|
jaroslav@609
|
157 |
}
|
jaroslav@611
|
158 |
needDict = false;
|
jaroslav@609
|
159 |
}
|
jaroslav@609
|
160 |
|
jaroslav@609
|
161 |
/**
|
jaroslav@609
|
162 |
* Sets the preset dictionary to the given array of bytes. Should be
|
jaroslav@609
|
163 |
* called when inflate() returns 0 and needsDictionary() returns true
|
jaroslav@609
|
164 |
* indicating that a preset dictionary is required. The method getAdler()
|
jaroslav@609
|
165 |
* can be used to get the Adler-32 value of the dictionary needed.
|
jaroslav@609
|
166 |
* @param b the dictionary data bytes
|
jaroslav@609
|
167 |
* @see Inflater#needsDictionary
|
jaroslav@609
|
168 |
* @see Inflater#getAdler
|
jaroslav@609
|
169 |
*/
|
jaroslav@609
|
170 |
public void setDictionary(byte[] b) {
|
jaroslav@609
|
171 |
setDictionary(b, 0, b.length);
|
jaroslav@609
|
172 |
}
|
jaroslav@609
|
173 |
|
jaroslav@609
|
174 |
/**
|
jaroslav@609
|
175 |
* Returns the total number of bytes remaining in the input buffer.
|
jaroslav@609
|
176 |
* This can be used to find out what bytes still remain in the input
|
jaroslav@609
|
177 |
* buffer after decompression has finished.
|
jaroslav@609
|
178 |
* @return the total number of bytes remaining in the input buffer
|
jaroslav@609
|
179 |
*/
|
jaroslav@609
|
180 |
public int getRemaining() {
|
jaroslav@611
|
181 |
return data.length() - offset;
|
jaroslav@609
|
182 |
}
|
jaroslav@609
|
183 |
|
jaroslav@609
|
184 |
/**
|
jaroslav@609
|
185 |
* Returns true if no data remains in the input buffer. This can
|
jaroslav@609
|
186 |
* be used to determine if #setInput should be called in order
|
jaroslav@609
|
187 |
* to provide more input.
|
jaroslav@609
|
188 |
* @return true if no data remains in the input buffer
|
jaroslav@609
|
189 |
*/
|
jaroslav@609
|
190 |
public boolean needsInput() {
|
jaroslav@611
|
191 |
return getRemaining() <= 0;
|
jaroslav@609
|
192 |
}
|
jaroslav@609
|
193 |
|
jaroslav@609
|
194 |
/**
|
jaroslav@609
|
195 |
* Returns true if a preset dictionary is needed for decompression.
|
jaroslav@609
|
196 |
* @return true if a preset dictionary is needed for decompression
|
jaroslav@609
|
197 |
* @see Inflater#setDictionary
|
jaroslav@609
|
198 |
*/
|
jaroslav@609
|
199 |
public boolean needsDictionary() {
|
jaroslav@611
|
200 |
return needDict;
|
jaroslav@609
|
201 |
}
|
jaroslav@609
|
202 |
|
jaroslav@609
|
203 |
/**
|
jaroslav@609
|
204 |
* Returns true if the end of the compressed data stream has been
|
jaroslav@609
|
205 |
* reached.
|
jaroslav@609
|
206 |
* @return true if the end of the compressed data stream has been
|
jaroslav@609
|
207 |
* reached
|
jaroslav@609
|
208 |
*/
|
jaroslav@609
|
209 |
public boolean finished() {
|
jaroslav@611
|
210 |
return finished;
|
jaroslav@609
|
211 |
}
|
jaroslav@609
|
212 |
|
jaroslav@609
|
213 |
/**
|
jaroslav@609
|
214 |
* Uncompresses bytes into specified buffer. Returns actual number
|
jaroslav@609
|
215 |
* of bytes uncompressed. A return value of 0 indicates that
|
jaroslav@609
|
216 |
* needsInput() or needsDictionary() should be called in order to
|
jaroslav@609
|
217 |
* determine if more input data or a preset dictionary is required.
|
jaroslav@609
|
218 |
* In the latter case, getAdler() can be used to get the Adler-32
|
jaroslav@609
|
219 |
* value of the dictionary required.
|
jaroslav@609
|
220 |
* @param b the buffer for the uncompressed data
|
jaroslav@609
|
221 |
* @param off the start offset of the data
|
jaroslav@609
|
222 |
* @param len the maximum number of uncompressed bytes
|
jaroslav@609
|
223 |
* @return the actual number of uncompressed bytes
|
jaroslav@609
|
224 |
* @exception DataFormatException if the compressed data format is invalid
|
jaroslav@609
|
225 |
* @see Inflater#needsInput
|
jaroslav@609
|
226 |
* @see Inflater#needsDictionary
|
jaroslav@609
|
227 |
*/
|
jaroslav@609
|
228 |
public int inflate(byte[] b, int off, int len)
|
jaroslav@609
|
229 |
throws DataFormatException
|
jaroslav@609
|
230 |
{
|
jaroslav@609
|
231 |
if (b == null) {
|
jaroslav@609
|
232 |
throw new NullPointerException();
|
jaroslav@609
|
233 |
}
|
jaroslav@609
|
234 |
if (off < 0 || len < 0 || off > b.length - len) {
|
jaroslav@609
|
235 |
throw new ArrayIndexOutOfBoundsException();
|
jaroslav@609
|
236 |
}
|
jaroslav@611
|
237 |
int cnt = 0;
|
jaroslav@611
|
238 |
while (offset < data.length()) {
|
jaroslav@611
|
239 |
b[off++] = (byte)data.charAt(offset++);
|
jaroslav@611
|
240 |
cnt++;
|
jaroslav@611
|
241 |
counter++;
|
jaroslav@609
|
242 |
}
|
jaroslav@611
|
243 |
return cnt;
|
jaroslav@609
|
244 |
}
|
jaroslav@611
|
245 |
|
jaroslav@611
|
246 |
@JavaScriptBody(args = { "arr", "offset", "len" }, body =
|
jaroslav@611
|
247 |
"var r = {};\n"
|
jaroslav@611
|
248 |
+ "r.charCodeAt = function(idx) { return arr[offset + idx]; };\n"
|
jaroslav@611
|
249 |
+ "return JSInflate.inflate(r);"
|
jaroslav@611
|
250 |
)
|
jaroslav@611
|
251 |
private static native Object infl(byte[] arr, int offset, int len);
|
jaroslav@609
|
252 |
|
jaroslav@609
|
253 |
/**
|
jaroslav@609
|
254 |
* Uncompresses bytes into specified buffer. Returns actual number
|
jaroslav@609
|
255 |
* of bytes uncompressed. A return value of 0 indicates that
|
jaroslav@609
|
256 |
* needsInput() or needsDictionary() should be called in order to
|
jaroslav@609
|
257 |
* determine if more input data or a preset dictionary is required.
|
jaroslav@609
|
258 |
* In the latter case, getAdler() can be used to get the Adler-32
|
jaroslav@609
|
259 |
* value of the dictionary required.
|
jaroslav@609
|
260 |
* @param b the buffer for the uncompressed data
|
jaroslav@609
|
261 |
* @return the actual number of uncompressed bytes
|
jaroslav@609
|
262 |
* @exception DataFormatException if the compressed data format is invalid
|
jaroslav@609
|
263 |
* @see Inflater#needsInput
|
jaroslav@609
|
264 |
* @see Inflater#needsDictionary
|
jaroslav@609
|
265 |
*/
|
jaroslav@609
|
266 |
public int inflate(byte[] b) throws DataFormatException {
|
jaroslav@609
|
267 |
return inflate(b, 0, b.length);
|
jaroslav@609
|
268 |
}
|
jaroslav@609
|
269 |
|
jaroslav@609
|
270 |
/**
|
jaroslav@609
|
271 |
* Returns the ADLER-32 value of the uncompressed data.
|
jaroslav@609
|
272 |
* @return the ADLER-32 value of the uncompressed data
|
jaroslav@609
|
273 |
*/
|
jaroslav@609
|
274 |
public int getAdler() {
|
jaroslav@611
|
275 |
return 0;
|
jaroslav@609
|
276 |
}
|
jaroslav@609
|
277 |
|
jaroslav@609
|
278 |
/**
|
jaroslav@609
|
279 |
* Returns the total number of compressed bytes input so far.
|
jaroslav@609
|
280 |
*
|
jaroslav@609
|
281 |
* <p>Since the number of bytes may be greater than
|
jaroslav@609
|
282 |
* Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
|
jaroslav@609
|
283 |
* the preferred means of obtaining this information.</p>
|
jaroslav@609
|
284 |
*
|
jaroslav@609
|
285 |
* @return the total number of compressed bytes input so far
|
jaroslav@609
|
286 |
*/
|
jaroslav@609
|
287 |
public int getTotalIn() {
|
jaroslav@609
|
288 |
return (int) getBytesRead();
|
jaroslav@609
|
289 |
}
|
jaroslav@609
|
290 |
|
jaroslav@609
|
291 |
/**
|
jaroslav@609
|
292 |
* Returns the total number of compressed bytes input so far.</p>
|
jaroslav@609
|
293 |
*
|
jaroslav@609
|
294 |
* @return the total (non-negative) number of compressed bytes input so far
|
jaroslav@609
|
295 |
* @since 1.5
|
jaroslav@609
|
296 |
*/
|
jaroslav@609
|
297 |
public long getBytesRead() {
|
jaroslav@611
|
298 |
return counter;
|
jaroslav@609
|
299 |
}
|
jaroslav@609
|
300 |
|
jaroslav@609
|
301 |
/**
|
jaroslav@609
|
302 |
* Returns the total number of uncompressed bytes output so far.
|
jaroslav@609
|
303 |
*
|
jaroslav@609
|
304 |
* <p>Since the number of bytes may be greater than
|
jaroslav@609
|
305 |
* Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
|
jaroslav@609
|
306 |
* the preferred means of obtaining this information.</p>
|
jaroslav@609
|
307 |
*
|
jaroslav@609
|
308 |
* @return the total number of uncompressed bytes output so far
|
jaroslav@609
|
309 |
*/
|
jaroslav@609
|
310 |
public int getTotalOut() {
|
jaroslav@609
|
311 |
return (int) getBytesWritten();
|
jaroslav@609
|
312 |
}
|
jaroslav@609
|
313 |
|
jaroslav@609
|
314 |
/**
|
jaroslav@609
|
315 |
* Returns the total number of uncompressed bytes output so far.</p>
|
jaroslav@609
|
316 |
*
|
jaroslav@609
|
317 |
* @return the total (non-negative) number of uncompressed bytes output so far
|
jaroslav@609
|
318 |
* @since 1.5
|
jaroslav@609
|
319 |
*/
|
jaroslav@609
|
320 |
public long getBytesWritten() {
|
jaroslav@611
|
321 |
return counter;
|
jaroslav@609
|
322 |
}
|
jaroslav@609
|
323 |
|
jaroslav@609
|
324 |
/**
|
jaroslav@609
|
325 |
* Resets inflater so that a new set of input data can be processed.
|
jaroslav@609
|
326 |
*/
|
jaroslav@609
|
327 |
public void reset() {
|
jaroslav@611
|
328 |
data = "";
|
jaroslav@611
|
329 |
finished = false;
|
jaroslav@611
|
330 |
needDict = false;
|
jaroslav@611
|
331 |
offset = 0;
|
jaroslav@609
|
332 |
}
|
jaroslav@609
|
333 |
|
jaroslav@609
|
334 |
/**
|
jaroslav@609
|
335 |
* Closes the decompressor and discards any unprocessed input.
|
jaroslav@609
|
336 |
* This method should be called when the decompressor is no longer
|
jaroslav@609
|
337 |
* being used, but will also be called automatically by the finalize()
|
jaroslav@609
|
338 |
* method. Once this method is called, the behavior of the Inflater
|
jaroslav@609
|
339 |
* object is undefined.
|
jaroslav@609
|
340 |
*/
|
jaroslav@609
|
341 |
public void end() {
|
jaroslav@609
|
342 |
}
|
jaroslav@609
|
343 |
}
|