952
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger
|
1152
|
3 |
|
1155
|
4 |
New code and modifications done at SWING Research Group [1]:
|
1152
|
5 |
|
952
|
6 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
|
|
7 |
SWING Research Group, Czech Technical University in Prague
|
|
8 |
|
|
9 |
This software is furnished under a license and may be used
|
|
10 |
only in accordance with the terms of that license and with the
|
|
11 |
inclusion of the above copyright notice. This software may not
|
|
12 |
be provided or otherwise made available to, or used by, any
|
|
13 |
other person. No title to or ownership of the software is
|
|
14 |
hereby transferred.
|
|
15 |
|
1155
|
16 |
[1] Code written at SWING Research Group contains a signature
|
|
17 |
of one of the above copright owners. For exact set of such code,
|
|
18 |
see the differences between this version and version stx:libjava
|
1152
|
19 |
as of 1.9.2010
|
952
|
20 |
"
|
|
21 |
"{ Package: 'stx:libjava' }"
|
|
22 |
|
|
23 |
Object subclass:#JavaZipSupport
|
|
24 |
instanceVariableNames:''
|
|
25 |
classVariableNames:''
|
|
26 |
poolDictionaries:''
|
1536
|
27 |
category:'Languages-Java-Support-Native'
|
|
28 |
!
|
|
29 |
|
|
30 |
Object subclass:#Deflater
|
|
31 |
instanceVariableNames:'vm z_stream'
|
|
32 |
classVariableNames:''
|
|
33 |
poolDictionaries:''
|
|
34 |
privateIn:JavaZipSupport
|
952
|
35 |
!
|
|
36 |
|
|
37 |
Object subclass:#Inflater
|
|
38 |
instanceVariableNames:'vm z_stream'
|
|
39 |
classVariableNames:''
|
|
40 |
poolDictionaries:''
|
|
41 |
privateIn:JavaZipSupport
|
|
42 |
!
|
|
43 |
|
|
44 |
!JavaZipSupport primitiveDefinitions!
|
|
45 |
%{
|
|
46 |
|
|
47 |
#include <stdio.h>
|
|
48 |
#include <stdlib.h>
|
|
49 |
#include <errno.h>
|
|
50 |
#include <string.h>
|
|
51 |
#include "zlib.h"
|
|
52 |
|
|
53 |
#define MIN2(x, y) ((x) < (y) ? (x) : (y))
|
|
54 |
|
|
55 |
#define __zStreamVal(x) ((z_stream *)(__ExternalBytesInstPtr(x)->e_address))
|
|
56 |
|
|
57 |
%}
|
|
58 |
! !
|
|
59 |
|
|
60 |
!JavaZipSupport class methodsFor:'documentation'!
|
|
61 |
|
|
62 |
copyright
|
|
63 |
"
|
|
64 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger
|
1152
|
65 |
|
1155
|
66 |
New code and modifications done at SWING Research Group [1]:
|
1152
|
67 |
|
952
|
68 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
|
|
69 |
SWING Research Group, Czech Technical University in Prague
|
|
70 |
|
|
71 |
This software is furnished under a license and may be used
|
|
72 |
only in accordance with the terms of that license and with the
|
|
73 |
inclusion of the above copyright notice. This software may not
|
|
74 |
be provided or otherwise made available to, or used by, any
|
|
75 |
other person. No title to or ownership of the software is
|
|
76 |
hereby transferred.
|
|
77 |
|
1155
|
78 |
[1] Code written at SWING Research Group contains a signature
|
|
79 |
of one of the above copright owners. For exact set of such code,
|
|
80 |
see the differences between this version and version stx:libjava
|
1152
|
81 |
as of 1.9.2010
|
952
|
82 |
|
|
83 |
"
|
|
84 |
! !
|
|
85 |
|
|
86 |
!JavaZipSupport class methodsFor:'instance creation'!
|
|
87 |
|
|
88 |
inflaterFor: vm
|
|
89 |
|
1536
|
90 |
^Deflater new vm: vm
|
952
|
91 |
|
|
92 |
"Created: / 30-08-2011 / 09:05:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
93 |
! !
|
|
94 |
|
1536
|
95 |
!JavaZipSupport::Deflater methodsFor:'accessing'!
|
|
96 |
|
|
97 |
vm: javaVM
|
|
98 |
|
|
99 |
vm := javaVM.
|
|
100 |
|
|
101 |
"Created: / 30-08-2011 / 09:05:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
102 |
! !
|
|
103 |
|
|
104 |
!JavaZipSupport::Deflater methodsFor:'inflater methods'!
|
|
105 |
|
|
106 |
adler
|
|
107 |
|
|
108 |
%{
|
|
109 |
RETURN ( __MKEXTERNALADDRESS ( __zStreamVal(__INST(z_stream))->adler ) );
|
|
110 |
%}
|
|
111 |
|
|
112 |
"
|
|
113 |
OpenJDK 7 Implementation:
|
|
114 |
|
|
115 |
JNIEXPORT jint JNICALL
|
|
116 |
Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
|
|
117 |
{
|
|
118 |
return ((z_stream *)jlong_to_ptr(addr))->adler;
|
|
119 |
}
|
|
120 |
"
|
|
121 |
|
|
122 |
"Created: / 31-08-2011 / 09:30:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
123 |
!
|
|
124 |
|
|
125 |
bytesRead
|
|
126 |
|
|
127 |
%{
|
|
128 |
RETURN ( __MKSMALLINT( __zStreamVal(__INST(z_stream))->total_in));
|
|
129 |
%}
|
|
130 |
|
|
131 |
|
|
132 |
"
|
|
133 |
OpenJDK 7 Implementation:
|
|
134 |
|
|
135 |
JNIEXPORT jlong JNICALL
|
|
136 |
Java_java_util_zip_Inflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
|
|
137 |
{
|
|
138 |
return ((z_stream *)jlong_to_ptr(addr))->total_in;
|
|
139 |
}
|
|
140 |
"
|
|
141 |
|
|
142 |
"Created: / 31-08-2011 / 09:30:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
143 |
!
|
|
144 |
|
|
145 |
bytesWritten
|
|
146 |
|
|
147 |
%{
|
|
148 |
RETURN ( __MKSMALLINT( __zStreamVal(__INST(z_stream))->total_out));
|
|
149 |
%}
|
|
150 |
|
|
151 |
|
|
152 |
"
|
|
153 |
OpenJDK 7 Implementation:
|
|
154 |
|
|
155 |
JNIEXPORT jlong JNICALL
|
|
156 |
Java_java_util_zip_Inflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
|
|
157 |
{
|
|
158 |
return ((z_stream *)jlong_to_ptr(addr))->total_out;
|
|
159 |
}
|
|
160 |
"
|
|
161 |
|
|
162 |
"Created: / 31-08-2011 / 09:30:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
163 |
!
|
|
164 |
|
|
165 |
deflate:"byte[]" out_buf offset: "int" out_off length: "int" out_len flush: flush deflaterr: deflater
|
|
166 |
|
|
167 |
| in_buf in_off in_len finished needDict errorType errorMsg ret |
|
|
168 |
in_buf := deflater instVarNamed:#buf.
|
|
169 |
in_off := deflater instVarNamed:#off.
|
|
170 |
in_len := deflater instVarNamed:#len.
|
|
171 |
|
|
172 |
finished := needDict := 0.
|
|
173 |
|
|
174 |
%{
|
|
175 |
#define strm (__zStreamVal(__INST(z_stream)))
|
|
176 |
int code, consumed;
|
|
177 |
|
|
178 |
strm->next_in = (Bytef *) (__ByteArrayInstPtr(in_buf)->ba_element + __intVal(in_off));
|
|
179 |
strm->next_out = (Bytef *) (__ByteArrayInstPtr(out_buf)->ba_element + __intVal(out_off));
|
|
180 |
strm->avail_in = __intVal(in_len);
|
|
181 |
strm->avail_out = __intVal(out_len);
|
|
182 |
|
|
183 |
|
|
184 |
code = inflate(strm, Z_PARTIAL_FLUSH);
|
|
185 |
|
|
186 |
switch (code) {
|
|
187 |
case Z_STREAM_END:
|
|
188 |
finished = __MKSMALLINT(1);
|
|
189 |
/* fall through */
|
|
190 |
case Z_OK:
|
|
191 |
consumed = __intVal(in_len) - strm->avail_in;
|
|
192 |
in_off = __MKSMALLINT(_intVal(in_off) + consumed);
|
|
193 |
in_len = __MKSMALLINT(_intVal(in_len) - consumed);
|
|
194 |
ret = __MKSMALLINT(__intVal(out_len) - strm->avail_out);
|
|
195 |
break;
|
|
196 |
case Z_NEED_DICT:
|
|
197 |
needDict = __MKSMALLINT(1);
|
|
198 |
/* Might have consumed some input here! */
|
|
199 |
consumed = __intVal(in_len) - strm->avail_in;
|
|
200 |
in_off = __MKSMALLINT(_intVal(in_off) + consumed);
|
|
201 |
in_len = __MKSMALLINT(_intVal(in_len) - consumed);
|
|
202 |
ret = __MKSMALLINT(0);
|
|
203 |
break;
|
|
204 |
case Z_BUF_ERROR:
|
|
205 |
ret = __MKSMALLINT(0);
|
|
206 |
break;
|
|
207 |
case Z_DATA_ERROR:
|
|
208 |
errorMsg = __MKSTRING(__zStreamVal(__INST(z_stream))->msg);
|
|
209 |
errorType = @symbol(DataFormatException);
|
|
210 |
|
|
211 |
break;
|
|
212 |
case Z_MEM_ERROR:
|
|
213 |
errorType = @symbol(OutOfMemoryError);
|
|
214 |
break;
|
|
215 |
default:
|
|
216 |
errorMsg = __MKSTRING(__zStreamVal(__INST(z_stream))->msg);
|
|
217 |
errorType = @symbol(InternalError);
|
|
218 |
break;
|
|
219 |
}
|
|
220 |
#undef strm
|
|
221 |
%}.
|
|
222 |
errorType == #DataFormatException ifTrue:[
|
|
223 |
vm throwDataFormatException: errorMsg.
|
|
224 |
^0.
|
|
225 |
].
|
|
226 |
errorType == #OutOfMemoryError ifTrue:[
|
|
227 |
vm throwOutOfMemoryError.
|
|
228 |
^0.
|
|
229 |
].
|
|
230 |
errorType == #InternalError ifTrue:[
|
|
231 |
vm internalError: errorMsg.
|
|
232 |
^0.
|
|
233 |
].
|
|
234 |
|
|
235 |
deflater instVarNamed:#off put: in_off.
|
|
236 |
deflater instVarNamed:#len put: in_len.
|
|
237 |
deflater instVarNamed:#finished put: finished.
|
|
238 |
deflater instVarNamed:#needDict put: needDict.
|
|
239 |
^ret
|
|
240 |
|
|
241 |
"
|
|
242 |
OpenJDK 7 Implementation
|
|
243 |
|
|
244 |
JNIEXPORT jint JNICALL
|
|
245 |
Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
|
|
246 |
jarray b, jint off, jint len)
|
|
247 |
{
|
|
248 |
z_stream *strm = jlong_to_ptr(addr);
|
|
249 |
|
|
250 |
jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID);
|
|
251 |
jint this_off = (*env)->GetIntField(env, this, offID);
|
|
252 |
jint this_len = (*env)->GetIntField(env, this, lenID);
|
|
253 |
jbyte *in_buf;
|
|
254 |
jbyte *out_buf;
|
|
255 |
int ret;
|
|
256 |
/*
|
|
257 |
* Avoid excess copying.
|
|
258 |
* zlib stream usually has a few bytes of overhead for header info
|
|
259 |
* (depends on the underlying data)
|
|
260 |
*
|
|
261 |
* (a) 5 bytes per 16KB
|
|
262 |
* (b) 6 bytes for entire stream
|
|
263 |
* (c) 4 bytes for gzip header
|
|
264 |
* (d) 2 bytes for crc
|
|
265 |
*
|
|
266 |
* Use 20 bytes as the 'safe cutoff' number.
|
|
267 |
*/
|
|
268 |
jint in_len = MIN2(this_len, len + 20);
|
|
269 |
jint consumed;
|
|
270 |
|
|
271 |
in_buf = (jbyte *) malloc(in_len);
|
|
272 |
if (in_buf == 0) {
|
|
273 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
274 |
return 0;
|
|
275 |
}
|
|
276 |
(*env)->GetByteArrayRegion(env, this_buf, this_off, in_len, in_buf);
|
|
277 |
|
|
278 |
out_buf = (jbyte *) malloc(len);
|
|
279 |
if (out_buf == 0) {
|
|
280 |
free(in_buf);
|
|
281 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
282 |
return 0;
|
|
283 |
}
|
|
284 |
|
|
285 |
strm->next_in = (Bytef *) in_buf;
|
|
286 |
strm->next_out = (Bytef *) out_buf;
|
|
287 |
strm->avail_in = in_len;
|
|
288 |
strm->avail_out = len;
|
|
289 |
ret = inflate(strm, Z_PARTIAL_FLUSH);
|
|
290 |
|
|
291 |
if (ret == Z_STREAM_END || ret == Z_OK) {
|
|
292 |
(*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
|
|
293 |
}
|
|
294 |
free(out_buf);
|
|
295 |
free(in_buf);
|
|
296 |
|
|
297 |
switch (ret) {
|
|
298 |
case Z_STREAM_END:
|
|
299 |
(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
|
|
300 |
/* fall through */
|
|
301 |
case Z_OK:
|
|
302 |
consumed = in_len - strm->avail_in;
|
|
303 |
(*env)->SetIntField(env, this, offID, this_off + consumed);
|
|
304 |
(*env)->SetIntField(env, this, lenID, this_len - consumed);
|
|
305 |
return len - strm->avail_out;
|
|
306 |
case Z_NEED_DICT:
|
|
307 |
(*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);
|
|
308 |
/* Might have consumed some input here!! */
|
|
309 |
consumed = in_len - strm->avail_in;
|
|
310 |
(*env)->SetIntField(env, this, offID, this_off + consumed);
|
|
311 |
(*env)->SetIntField(env, this, lenID, this_len - consumed);
|
|
312 |
return 0;
|
|
313 |
case Z_BUF_ERROR:
|
|
314 |
return 0;
|
|
315 |
case Z_DATA_ERROR:
|
|
316 |
ThrowDataFormatException(env, strm->msg);
|
|
317 |
return 0;
|
|
318 |
case Z_MEM_ERROR:
|
|
319 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
320 |
return 0;
|
|
321 |
default:
|
|
322 |
JNU_ThrowInternalError(env, strm->msg);
|
|
323 |
return 0;
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
"
|
|
328 |
|
|
329 |
"Created: / 20-07-2012 / 17:55:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
330 |
!
|
|
331 |
|
|
332 |
end
|
|
333 |
|
|
334 |
| ok |
|
|
335 |
|
|
336 |
ok := true.
|
|
337 |
|
|
338 |
%{
|
|
339 |
if (inflateEnd(__zStreamVal(__INST(z_stream))) == Z_STREAM_ERROR) {
|
|
340 |
ok = false;
|
|
341 |
}
|
|
342 |
%}.
|
|
343 |
z_stream free.
|
|
344 |
ok ifFalse:[
|
|
345 |
vm internalError: 'Cannot call inflateEnd()'.
|
|
346 |
].
|
|
347 |
|
|
348 |
"
|
|
349 |
OpenJDK 7 Implementation:
|
|
350 |
|
|
351 |
JNIEXPORT void JNICALL
|
|
352 |
Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)
|
|
353 |
{
|
|
354 |
if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
|
|
355 |
JNU_ThrowInternalError(env, 0);
|
|
356 |
} else {
|
|
357 |
free(jlong_to_ptr(addr));
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
"
|
|
362 |
|
|
363 |
"Created: / 30-08-2011 / 20:50:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
364 |
!
|
|
365 |
|
|
366 |
init: nowrap
|
|
367 |
|
|
368 |
| z_stream_size errorType errorMsg |
|
|
369 |
|
|
370 |
%{ z_stream_size = __MKSMALLINT(sizeof(z_stream)); %}.
|
|
371 |
|
|
372 |
z_stream := ExternalBytes unprotectedNew: z_stream_size.
|
|
373 |
|
|
374 |
|
|
375 |
%{
|
|
376 |
switch (inflateInit2(__zStreamVal(__INST(z_stream)), nowrap ? -MAX_WBITS : MAX_WBITS)) {
|
|
377 |
case Z_OK:
|
|
378 |
RETURN ( self );
|
|
379 |
case Z_MEM_ERROR:
|
|
380 |
errorType = @symbol(OutOfMemoryError);
|
|
381 |
break;
|
|
382 |
default:
|
|
383 |
errorMsg = __MKSTRING(__zStreamVal(__INST(z_stream))->msg);
|
|
384 |
errorType = @symbol(InternalError);
|
|
385 |
break;
|
|
386 |
}
|
|
387 |
%}.
|
|
388 |
|
|
389 |
z_stream free.
|
|
390 |
errorType == #OutOfMemoryError ifTrue:[
|
|
391 |
vm throwOutOfMemoryError.
|
|
392 |
^self.
|
|
393 |
].
|
|
394 |
errorType == #InternalError ifTrue:[
|
|
395 |
vm internalError: errorMsg.
|
|
396 |
^self.
|
|
397 |
].
|
|
398 |
|
|
399 |
|
|
400 |
|
|
401 |
"
|
|
402 |
OpenJDK 7 Implementation:
|
|
403 |
JNIEXPORT jlong JNICALL
|
|
404 |
Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)
|
|
405 |
{
|
|
406 |
z_stream *strm = calloc(1, sizeof(z_stream));
|
|
407 |
|
|
408 |
if (strm == 0) {
|
|
409 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
410 |
return jlong_zero;
|
|
411 |
} else {
|
|
412 |
char *msg;
|
|
413 |
switch (inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS)) {
|
|
414 |
case Z_OK:
|
|
415 |
return ptr_to_jlong(strm);
|
|
416 |
case Z_MEM_ERROR:
|
|
417 |
free(strm);
|
|
418 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
419 |
return jlong_zero;
|
|
420 |
default:
|
|
421 |
msg = strm->msg;
|
|
422 |
free(strm);
|
|
423 |
JNU_ThrowInternalError(env, msg);
|
|
424 |
return jlong_zero;
|
|
425 |
}
|
|
426 |
}
|
|
427 |
}
|
|
428 |
|
|
429 |
"
|
|
430 |
|
|
431 |
"Created: / 29-08-2011 / 17:22:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
432 |
!
|
|
433 |
|
|
434 |
reset
|
|
435 |
|
|
436 |
%{
|
|
437 |
if (inflateReset(__zStreamVal(__INST(z_stream))) == Z_OK) {
|
|
438 |
RETURN ( self );
|
|
439 |
}
|
|
440 |
%}.
|
|
441 |
|
|
442 |
vm internalError: 'Cannot reset inflater'.
|
|
443 |
|
|
444 |
"
|
|
445 |
OpenJDK 7 Implementation:
|
|
446 |
|
|
447 |
JNIEXPORT void JNICALL
|
|
448 |
Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
|
|
449 |
{
|
|
450 |
if (inflateReset(jlong_to_ptr(addr)) !!= Z_OK) {
|
|
451 |
JNU_ThrowInternalError(env, 0);
|
|
452 |
}
|
|
453 |
}
|
|
454 |
|
|
455 |
"
|
|
456 |
|
|
457 |
"Created: / 30-08-2011 / 12:45:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
458 |
! !
|
|
459 |
|
952
|
460 |
!JavaZipSupport::Inflater methodsFor:'accessing'!
|
|
461 |
|
|
462 |
vm: javaVM
|
|
463 |
|
|
464 |
vm := javaVM.
|
|
465 |
|
|
466 |
"Created: / 30-08-2011 / 09:05:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
467 |
! !
|
|
468 |
|
|
469 |
!JavaZipSupport::Inflater methodsFor:'inflater methods'!
|
|
470 |
|
|
471 |
adler
|
|
472 |
|
|
473 |
%{
|
|
474 |
RETURN ( __MKEXTERNALADDRESS ( __zStreamVal(__INST(z_stream))->adler ) );
|
|
475 |
%}
|
|
476 |
|
|
477 |
"
|
|
478 |
OpenJDK 7 Implementation:
|
|
479 |
|
|
480 |
JNIEXPORT jint JNICALL
|
|
481 |
Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
|
|
482 |
{
|
|
483 |
return ((z_stream *)jlong_to_ptr(addr))->adler;
|
|
484 |
}
|
|
485 |
"
|
|
486 |
|
|
487 |
"Created: / 31-08-2011 / 09:30:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
488 |
!
|
|
489 |
|
|
490 |
bytesRead
|
|
491 |
|
|
492 |
%{
|
|
493 |
RETURN ( __MKSMALLINT( __zStreamVal(__INST(z_stream))->total_in));
|
|
494 |
%}
|
|
495 |
|
|
496 |
|
|
497 |
"
|
|
498 |
OpenJDK 7 Implementation:
|
|
499 |
|
|
500 |
JNIEXPORT jlong JNICALL
|
|
501 |
Java_java_util_zip_Inflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
|
|
502 |
{
|
|
503 |
return ((z_stream *)jlong_to_ptr(addr))->total_in;
|
|
504 |
}
|
|
505 |
"
|
|
506 |
|
|
507 |
"Created: / 31-08-2011 / 09:30:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
508 |
!
|
|
509 |
|
|
510 |
bytesWritten
|
|
511 |
|
|
512 |
%{
|
|
513 |
RETURN ( __MKSMALLINT( __zStreamVal(__INST(z_stream))->total_out));
|
|
514 |
%}
|
|
515 |
|
|
516 |
|
|
517 |
"
|
|
518 |
OpenJDK 7 Implementation:
|
|
519 |
|
|
520 |
JNIEXPORT jlong JNICALL
|
|
521 |
Java_java_util_zip_Inflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
|
|
522 |
{
|
|
523 |
return ((z_stream *)jlong_to_ptr(addr))->total_out;
|
|
524 |
}
|
|
525 |
"
|
|
526 |
|
|
527 |
"Created: / 31-08-2011 / 09:30:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
528 |
!
|
|
529 |
|
|
530 |
end
|
|
531 |
|
|
532 |
| ok |
|
|
533 |
|
|
534 |
ok := true.
|
|
535 |
|
|
536 |
%{
|
|
537 |
if (inflateEnd(__zStreamVal(__INST(z_stream))) == Z_STREAM_ERROR) {
|
|
538 |
ok = false;
|
|
539 |
}
|
|
540 |
%}.
|
|
541 |
z_stream free.
|
|
542 |
ok ifFalse:[
|
|
543 |
vm internalError: 'Cannot call inflateEnd()'.
|
|
544 |
].
|
|
545 |
|
|
546 |
"
|
|
547 |
OpenJDK 7 Implementation:
|
|
548 |
|
|
549 |
JNIEXPORT void JNICALL
|
|
550 |
Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)
|
|
551 |
{
|
|
552 |
if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
|
|
553 |
JNU_ThrowInternalError(env, 0);
|
|
554 |
} else {
|
|
555 |
free(jlong_to_ptr(addr));
|
|
556 |
}
|
|
557 |
}
|
|
558 |
|
|
559 |
"
|
|
560 |
|
|
561 |
"Created: / 30-08-2011 / 20:50:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
562 |
!
|
|
563 |
|
|
564 |
inflate:"byte[]" out_buf offset: "int" out_off length: "int" out_len inflater: jinflater
|
|
565 |
|
|
566 |
| in_buf in_off in_len finished needDict errorType errorMsg ret |
|
|
567 |
in_buf := jinflater instVarNamed:#buf.
|
|
568 |
in_off := jinflater instVarNamed:#off.
|
|
569 |
in_len := jinflater instVarNamed:#len.
|
|
570 |
|
|
571 |
finished := needDict := 0.
|
|
572 |
|
|
573 |
%{
|
|
574 |
#define strm (__zStreamVal(__INST(z_stream)))
|
963
|
575 |
int code, consumed;
|
|
576 |
|
952
|
577 |
strm->next_in = (Bytef *) (__ByteArrayInstPtr(in_buf)->ba_element + __intVal(in_off));
|
|
578 |
strm->next_out = (Bytef *) (__ByteArrayInstPtr(out_buf)->ba_element + __intVal(out_off));
|
|
579 |
strm->avail_in = __intVal(in_len);
|
|
580 |
strm->avail_out = __intVal(out_len);
|
|
581 |
|
963
|
582 |
|
952
|
583 |
code = inflate(strm, Z_PARTIAL_FLUSH);
|
|
584 |
|
|
585 |
switch (code) {
|
|
586 |
case Z_STREAM_END:
|
|
587 |
finished = __MKSMALLINT(1);
|
|
588 |
/* fall through */
|
|
589 |
case Z_OK:
|
|
590 |
consumed = __intVal(in_len) - strm->avail_in;
|
|
591 |
in_off = __MKSMALLINT(_intVal(in_off) + consumed);
|
|
592 |
in_len = __MKSMALLINT(_intVal(in_len) - consumed);
|
|
593 |
ret = __MKSMALLINT(__intVal(out_len) - strm->avail_out);
|
|
594 |
break;
|
|
595 |
case Z_NEED_DICT:
|
|
596 |
needDict = __MKSMALLINT(1);
|
|
597 |
/* Might have consumed some input here! */
|
|
598 |
consumed = __intVal(in_len) - strm->avail_in;
|
|
599 |
in_off = __MKSMALLINT(_intVal(in_off) + consumed);
|
|
600 |
in_len = __MKSMALLINT(_intVal(in_len) - consumed);
|
|
601 |
ret = __MKSMALLINT(0);
|
|
602 |
break;
|
|
603 |
case Z_BUF_ERROR:
|
|
604 |
ret = __MKSMALLINT(0);
|
|
605 |
break;
|
|
606 |
case Z_DATA_ERROR:
|
|
607 |
errorMsg = __MKSTRING(__zStreamVal(__INST(z_stream))->msg);
|
|
608 |
errorType = @symbol(DataFormatException);
|
|
609 |
|
|
610 |
break;
|
|
611 |
case Z_MEM_ERROR:
|
|
612 |
errorType = @symbol(OutOfMemoryError);
|
|
613 |
break;
|
|
614 |
default:
|
|
615 |
errorMsg = __MKSTRING(__zStreamVal(__INST(z_stream))->msg);
|
|
616 |
errorType = @symbol(InternalError);
|
|
617 |
break;
|
|
618 |
}
|
|
619 |
#undef strm
|
|
620 |
%}.
|
|
621 |
errorType == #DataFormatException ifTrue:[
|
|
622 |
vm throwDataFormatException: errorMsg.
|
|
623 |
^0.
|
|
624 |
].
|
|
625 |
errorType == #OutOfMemoryError ifTrue:[
|
|
626 |
vm throwOutOfMemoryError.
|
|
627 |
^0.
|
|
628 |
].
|
|
629 |
errorType == #InternalError ifTrue:[
|
|
630 |
vm internalError: errorMsg.
|
|
631 |
^0.
|
|
632 |
].
|
|
633 |
|
|
634 |
jinflater instVarNamed:#off put: in_off.
|
|
635 |
jinflater instVarNamed:#len put: in_len.
|
|
636 |
jinflater instVarNamed:#finished put: finished.
|
|
637 |
jinflater instVarNamed:#needDict put: needDict.
|
|
638 |
^ret
|
|
639 |
|
|
640 |
"
|
|
641 |
OpenJDK 7 Implementation
|
|
642 |
|
|
643 |
JNIEXPORT jint JNICALL
|
|
644 |
Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
|
|
645 |
jarray b, jint off, jint len)
|
|
646 |
{
|
|
647 |
z_stream *strm = jlong_to_ptr(addr);
|
|
648 |
|
|
649 |
jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID);
|
|
650 |
jint this_off = (*env)->GetIntField(env, this, offID);
|
|
651 |
jint this_len = (*env)->GetIntField(env, this, lenID);
|
|
652 |
jbyte *in_buf;
|
|
653 |
jbyte *out_buf;
|
|
654 |
int ret;
|
|
655 |
/*
|
|
656 |
* Avoid excess copying.
|
|
657 |
* zlib stream usually has a few bytes of overhead for header info
|
|
658 |
* (depends on the underlying data)
|
|
659 |
*
|
|
660 |
* (a) 5 bytes per 16KB
|
|
661 |
* (b) 6 bytes for entire stream
|
|
662 |
* (c) 4 bytes for gzip header
|
|
663 |
* (d) 2 bytes for crc
|
|
664 |
*
|
|
665 |
* Use 20 bytes as the 'safe cutoff' number.
|
|
666 |
*/
|
|
667 |
jint in_len = MIN2(this_len, len + 20);
|
|
668 |
jint consumed;
|
|
669 |
|
|
670 |
in_buf = (jbyte *) malloc(in_len);
|
|
671 |
if (in_buf == 0) {
|
|
672 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
673 |
return 0;
|
|
674 |
}
|
|
675 |
(*env)->GetByteArrayRegion(env, this_buf, this_off, in_len, in_buf);
|
|
676 |
|
|
677 |
out_buf = (jbyte *) malloc(len);
|
|
678 |
if (out_buf == 0) {
|
|
679 |
free(in_buf);
|
|
680 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
681 |
return 0;
|
|
682 |
}
|
|
683 |
|
|
684 |
strm->next_in = (Bytef *) in_buf;
|
|
685 |
strm->next_out = (Bytef *) out_buf;
|
|
686 |
strm->avail_in = in_len;
|
|
687 |
strm->avail_out = len;
|
|
688 |
ret = inflate(strm, Z_PARTIAL_FLUSH);
|
|
689 |
|
|
690 |
if (ret == Z_STREAM_END || ret == Z_OK) {
|
|
691 |
(*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
|
|
692 |
}
|
|
693 |
free(out_buf);
|
|
694 |
free(in_buf);
|
|
695 |
|
|
696 |
switch (ret) {
|
|
697 |
case Z_STREAM_END:
|
|
698 |
(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
|
|
699 |
/* fall through */
|
|
700 |
case Z_OK:
|
|
701 |
consumed = in_len - strm->avail_in;
|
|
702 |
(*env)->SetIntField(env, this, offID, this_off + consumed);
|
|
703 |
(*env)->SetIntField(env, this, lenID, this_len - consumed);
|
|
704 |
return len - strm->avail_out;
|
|
705 |
case Z_NEED_DICT:
|
|
706 |
(*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);
|
|
707 |
/* Might have consumed some input here!! */
|
|
708 |
consumed = in_len - strm->avail_in;
|
|
709 |
(*env)->SetIntField(env, this, offID, this_off + consumed);
|
|
710 |
(*env)->SetIntField(env, this, lenID, this_len - consumed);
|
|
711 |
return 0;
|
|
712 |
case Z_BUF_ERROR:
|
|
713 |
return 0;
|
|
714 |
case Z_DATA_ERROR:
|
|
715 |
ThrowDataFormatException(env, strm->msg);
|
|
716 |
return 0;
|
|
717 |
case Z_MEM_ERROR:
|
|
718 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
719 |
return 0;
|
|
720 |
default:
|
|
721 |
JNU_ThrowInternalError(env, strm->msg);
|
|
722 |
return 0;
|
|
723 |
}
|
|
724 |
}
|
|
725 |
|
|
726 |
"
|
|
727 |
|
|
728 |
"Created: / 30-08-2011 / 09:31:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
729 |
!
|
|
730 |
|
|
731 |
init: nowrap
|
|
732 |
|
|
733 |
| z_stream_size errorType errorMsg |
|
|
734 |
|
|
735 |
%{ z_stream_size = __MKSMALLINT(sizeof(z_stream)); %}.
|
|
736 |
|
|
737 |
z_stream := ExternalBytes unprotectedNew: z_stream_size.
|
|
738 |
|
|
739 |
|
|
740 |
%{
|
|
741 |
switch (inflateInit2(__zStreamVal(__INST(z_stream)), nowrap ? -MAX_WBITS : MAX_WBITS)) {
|
|
742 |
case Z_OK:
|
|
743 |
RETURN ( self );
|
|
744 |
case Z_MEM_ERROR:
|
|
745 |
errorType = @symbol(OutOfMemoryError);
|
|
746 |
break;
|
|
747 |
default:
|
|
748 |
errorMsg = __MKSTRING(__zStreamVal(__INST(z_stream))->msg);
|
|
749 |
errorType = @symbol(InternalError);
|
|
750 |
break;
|
|
751 |
}
|
|
752 |
%}.
|
|
753 |
|
|
754 |
z_stream free.
|
|
755 |
errorType == #OutOfMemoryError ifTrue:[
|
|
756 |
vm throwOutOfMemoryError.
|
|
757 |
^self.
|
|
758 |
].
|
|
759 |
errorType == #InternalError ifTrue:[
|
|
760 |
vm internalError: errorMsg.
|
|
761 |
^self.
|
|
762 |
].
|
|
763 |
|
|
764 |
|
|
765 |
|
|
766 |
"
|
|
767 |
OpenJDK 7 Implementation:
|
|
768 |
JNIEXPORT jlong JNICALL
|
|
769 |
Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)
|
|
770 |
{
|
|
771 |
z_stream *strm = calloc(1, sizeof(z_stream));
|
|
772 |
|
|
773 |
if (strm == 0) {
|
|
774 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
775 |
return jlong_zero;
|
|
776 |
} else {
|
|
777 |
char *msg;
|
|
778 |
switch (inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS)) {
|
|
779 |
case Z_OK:
|
|
780 |
return ptr_to_jlong(strm);
|
|
781 |
case Z_MEM_ERROR:
|
|
782 |
free(strm);
|
|
783 |
JNU_ThrowOutOfMemoryError(env, 0);
|
|
784 |
return jlong_zero;
|
|
785 |
default:
|
|
786 |
msg = strm->msg;
|
|
787 |
free(strm);
|
|
788 |
JNU_ThrowInternalError(env, msg);
|
|
789 |
return jlong_zero;
|
|
790 |
}
|
|
791 |
}
|
|
792 |
}
|
|
793 |
|
|
794 |
"
|
|
795 |
|
|
796 |
"Created: / 29-08-2011 / 17:22:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
797 |
!
|
|
798 |
|
|
799 |
reset
|
|
800 |
|
|
801 |
%{
|
|
802 |
if (inflateReset(__zStreamVal(__INST(z_stream))) == Z_OK) {
|
|
803 |
RETURN ( self );
|
|
804 |
}
|
|
805 |
%}.
|
|
806 |
|
|
807 |
vm internalError: 'Cannot reset inflater'.
|
|
808 |
|
|
809 |
"
|
|
810 |
OpenJDK 7 Implementation:
|
|
811 |
|
|
812 |
JNIEXPORT void JNICALL
|
|
813 |
Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
|
|
814 |
{
|
|
815 |
if (inflateReset(jlong_to_ptr(addr)) !!= Z_OK) {
|
|
816 |
JNU_ThrowInternalError(env, 0);
|
|
817 |
}
|
|
818 |
}
|
|
819 |
|
|
820 |
"
|
|
821 |
|
|
822 |
"Created: / 30-08-2011 / 12:45:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
823 |
! !
|
|
824 |
|
|
825 |
!JavaZipSupport class methodsFor:'documentation'!
|
|
826 |
|
|
827 |
version_SVN
|
|
828 |
^ '$Id$'
|
|
829 |
! !
|