blob: 98216b581aca69c091f84c46f00828b304e40d40 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
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).
16 *
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.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 * Native method support for java.util.zip.Deflater
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include "jlong.h"
33#include "jni.h"
34#include "jni_util.h"
35#include "zlib.h"
36
37#include "java_util_zip_Deflater.h"
38
39#define DEF_MEM_LEVEL 8
40
41static jfieldID strmID;
42static jfieldID levelID;
43static jfieldID strategyID;
44static jfieldID setParamsID;
45static jfieldID finishID;
46static jfieldID finishedID;
47static jfieldID bufID, offID, lenID;
48
49JNIEXPORT void JNICALL
50Java_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls)
51{
52 strmID = (*env)->GetFieldID(env, cls, "strm", "J");
53 levelID = (*env)->GetFieldID(env, cls, "level", "I");
54 strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");
55 setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");
56 finishID = (*env)->GetFieldID(env, cls, "finish", "Z");
57 finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
58 bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
59 offID = (*env)->GetFieldID(env, cls, "off", "I");
60 lenID = (*env)->GetFieldID(env, cls, "len", "I");
61}
62
63JNIEXPORT jlong JNICALL
64Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level,
65 jint strategy, jboolean nowrap)
66{
67 z_stream *strm = calloc(1, sizeof(z_stream));
68
69 if (strm == 0) {
70 JNU_ThrowOutOfMemoryError(env, 0);
71 return jlong_zero;
72 } else {
73 char *msg;
74 switch (deflateInit2(strm, level, Z_DEFLATED,
75 nowrap ? -MAX_WBITS : MAX_WBITS,
76 DEF_MEM_LEVEL, strategy)) {
77 case Z_OK:
78 return ptr_to_jlong(strm);
79 case Z_MEM_ERROR:
80 free(strm);
81 JNU_ThrowOutOfMemoryError(env, 0);
82 return jlong_zero;
83 case Z_STREAM_ERROR:
84 free(strm);
85 JNU_ThrowIllegalArgumentException(env, 0);
86 return jlong_zero;
87 default:
88 msg = strm->msg;
89 free(strm);
90 JNU_ThrowInternalError(env, msg);
91 return jlong_zero;
92 }
93 }
94}
95
96JNIEXPORT void JNICALL
97Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong strm,
98 jarray b, jint off, jint len)
99{
100 Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
101 int res;
102 if (buf == 0) {/* out of memory */
103 return;
104 }
105 res = deflateSetDictionary((z_stream *)jlong_to_ptr(strm), buf + off, len);
106 (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
107 switch (res) {
108 case Z_OK:
109 break;
110 case Z_STREAM_ERROR:
111 JNU_ThrowIllegalArgumentException(env, 0);
112 break;
113 default:
114 JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(strm))->msg);
115 break;
116 }
117}
118
119JNIEXPORT jint JNICALL
120Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this,
121 jarray b, jint off, jint len)
122{
123 z_stream *strm = jlong_to_ptr((*env)->GetLongField(env, this, strmID));
124
125 if (strm == 0) {
126 JNU_ThrowNullPointerException(env, 0);
127 return 0;
128 } else {
129 jarray this_buf = (*env)->GetObjectField(env, this, bufID);
130 jint this_off = (*env)->GetIntField(env, this, offID);
131 jint this_len = (*env)->GetIntField(env, this, lenID);
132 jbyte *in_buf;
133 jbyte *out_buf;
134 int res;
135 if ((*env)->GetBooleanField(env, this, setParamsID)) {
136 int level = (*env)->GetIntField(env, this, levelID);
137 int strategy = (*env)->GetIntField(env, this, strategyID);
138
139 in_buf = (jbyte *) malloc(this_len);
140 if (in_buf == 0) {
141 return 0;
142 }
143 (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
144
145 out_buf = (jbyte *) malloc(len);
146 if (out_buf == 0) {
147 free(in_buf);
148 return 0;
149 }
150
151 strm->next_in = (Bytef *) in_buf;
152 strm->next_out = (Bytef *) out_buf;
153 strm->avail_in = this_len;
154 strm->avail_out = len;
155 res = deflateParams(strm, level, strategy);
156
157 if (res == Z_OK) {
158 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
159 }
160 free(out_buf);
161 free(in_buf);
162
163 switch (res) {
164 case Z_OK:
165 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
166 this_off += this_len - strm->avail_in;
167 (*env)->SetIntField(env, this, offID, this_off);
168 (*env)->SetIntField(env, this, lenID, strm->avail_in);
169 return len - strm->avail_out;
170 case Z_BUF_ERROR:
171 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
172 return 0;
173 default:
174 JNU_ThrowInternalError(env, strm->msg);
175 return 0;
176 }
177 } else {
178 jboolean finish = (*env)->GetBooleanField(env, this, finishID);
179
180 in_buf = (jbyte *) malloc(this_len);
181 if (in_buf == 0) {
182 return 0;
183 }
184 (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
185
186 out_buf = (jbyte *) malloc(len);
187 if (out_buf == 0) {
188 free(in_buf);
189 return 0;
190 }
191
192 strm->next_in = (Bytef *) in_buf;
193 strm->next_out = (Bytef *) out_buf;
194 strm->avail_in = this_len;
195 strm->avail_out = len;
196 res = deflate(strm, finish ? Z_FINISH : Z_NO_FLUSH);
197
198 if (res == Z_STREAM_END || res == Z_OK) {
199 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
200 }
201 free(out_buf);
202 free(in_buf);
203
204 switch (res) {
205 case Z_STREAM_END:
206 (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
207 /* fall through */
208 case Z_OK:
209 this_off += this_len - strm->avail_in;
210 (*env)->SetIntField(env, this, offID, this_off);
211 (*env)->SetIntField(env, this, lenID, strm->avail_in);
212 return len - strm->avail_out;
213 case Z_BUF_ERROR:
214 return 0;
215 default:
216 JNU_ThrowInternalError(env, strm->msg);
217 return 0;
218 }
219 }
220 }
221}
222
223JNIEXPORT jint JNICALL
224Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong strm)
225{
226 return ((z_stream *)jlong_to_ptr(strm))->adler;
227}
228
229JNIEXPORT jlong JNICALL
230Java_java_util_zip_Deflater_getBytesRead(JNIEnv *env, jclass cls, jlong strm)
231{
232 return ((z_stream *)jlong_to_ptr(strm))->total_in;
233}
234
235JNIEXPORT jlong JNICALL
236Java_java_util_zip_Deflater_getBytesWritten(JNIEnv *env, jclass cls, jlong strm)
237{
238 return ((z_stream *)jlong_to_ptr(strm))->total_out;
239}
240
241JNIEXPORT void JNICALL
242Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong strm)
243{
244 if (deflateReset((z_stream *)jlong_to_ptr(strm)) != Z_OK) {
245 JNU_ThrowInternalError(env, 0);
246 }
247}
248
249JNIEXPORT void JNICALL
250Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong strm)
251{
252 if (deflateEnd((z_stream *)jlong_to_ptr(strm)) == Z_STREAM_ERROR) {
253 JNU_ThrowInternalError(env, 0);
254 } else {
255 free((z_stream *)jlong_to_ptr(strm));
256 }
257}