1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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. 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.
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
147 // Throw OOME only when length is not zero
148 if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL)
149 JNU_ThrowOutOfMemoryError(env, 0);
150 return 0;
151 }
152 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
153 if (out_buf == NULL) {
154 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
155 if (len != 0 && (*env)->ExceptionOccurred(env) == NULL)
156 JNU_ThrowOutOfMemoryError(env, 0);
157 return 0;
158 }
159
160 strm->next_in = (Bytef *) (in_buf + this_off);
161 strm->next_out = (Bytef *) (out_buf + off);
162 strm->avail_in = this_len;
163 strm->avail_out = len;
164 res = deflateParams(strm, level, strategy);
165 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
166 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
167
168 switch (res) {
169 case Z_OK:
170 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
171 this_off += this_len - strm->avail_in;
172 (*env)->SetIntField(env, this, offID, this_off);
173 (*env)->SetIntField(env, this, lenID, strm->avail_in);
174 return (jint) (len - strm->avail_out);
175 case Z_BUF_ERROR:
176 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
177 return 0;
178 default:
179 JNU_ThrowInternalError(env, strm->msg);
180 return 0;
181 }
182 } else {
183 jboolean finish = (*env)->GetBooleanField(env, this, finishID);
184 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
185 if (in_buf == NULL) {
186 if (this_len != 0)
187 JNU_ThrowOutOfMemoryError(env, 0);
188 return 0;
189 }
190 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
191 if (out_buf == NULL) {
192 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
193 if (len != 0)
194 JNU_ThrowOutOfMemoryError(env, 0);
195
196 return 0;
197 }
198
199 strm->next_in = (Bytef *) (in_buf + this_off);
200 strm->next_out = (Bytef *) (out_buf + off);
201 strm->avail_in = this_len;
202 strm->avail_out = len;
203 res = deflate(strm, finish ? Z_FINISH : flush);
204 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
205 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
206
207 switch (res) {
208 case Z_STREAM_END:
209 (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
210 /* fall through */
211 case Z_OK:
212 this_off += this_len - strm->avail_in;
213 (*env)->SetIntField(env, this, offID, this_off);
214 (*env)->SetIntField(env, this, lenID, strm->avail_in);
215 return len - strm->avail_out;
216 case Z_BUF_ERROR:
217 return 0;
218 default:
219 JNU_ThrowInternalError(env, strm->msg);
220 return 0;
221 }
222 }
223 }
224
225 JNIEXPORT jint JNICALL
226 Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
227 {
228 return ((z_stream *)jlong_to_ptr(addr))->adler;
229 }
230
231 JNIEXPORT void JNICALL
232 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
233 {
234 if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
235 JNU_ThrowInternalError(env, 0);
236 }
237 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. 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. 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.
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
147 // Throw OOME only when length is not zero
148 if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL)
149 JNU_ThrowOutOfMemoryError(env, 0);
150 return 0;
151 }
152 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
153 if (out_buf == NULL) {
154 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
155 if (len != 0 && (*env)->ExceptionOccurred(env) == NULL)
156 JNU_ThrowOutOfMemoryError(env, 0);
157 return 0;
158 }
159
160 strm->next_in = (Bytef *) (in_buf + this_off);
161 strm->next_out = (Bytef *) (out_buf + off);
162 strm->avail_in = this_len;
163 strm->avail_out = len;
164 res = deflateParams(strm, level, strategy);
165 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
166 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
167 switch (res) {
168 case Z_OK:
169 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
170 case Z_BUF_ERROR:
171 this_off += this_len - strm->avail_in;
172 (*env)->SetIntField(env, this, offID, this_off);
173 (*env)->SetIntField(env, this, lenID, strm->avail_in);
174 return (jint) (len - strm->avail_out);
175 default:
176 JNU_ThrowInternalError(env, strm->msg);
177 return 0;
178 }
179 } else {
180 jboolean finish = (*env)->GetBooleanField(env, this, finishID);
181 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
182 if (in_buf == NULL) {
183 if (this_len != 0)
184 JNU_ThrowOutOfMemoryError(env, 0);
185 return 0;
186 }
187 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
188 if (out_buf == NULL) {
189 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
190 if (len != 0)
191 JNU_ThrowOutOfMemoryError(env, 0);
192
193 return 0;
194 }
195
196 strm->next_in = (Bytef *) (in_buf + this_off);
197 strm->next_out = (Bytef *) (out_buf + off);
198 strm->avail_in = this_len;
199 strm->avail_out = len;
200 res = deflate(strm, finish ? Z_FINISH : flush);
201 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
202 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
203 switch (res) {
204 case Z_STREAM_END:
205 (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
206 /* fall through */
207 case Z_OK:
208 case Z_BUF_ERROR:
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 default:
214 JNU_ThrowInternalError(env, strm->msg);
215 return 0;
216 }
217 }
218 }
219
220 JNIEXPORT jint JNICALL
221 Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
222 {
223 return ((z_stream *)jlong_to_ptr(addr))->adler;
224 }
225
226 JNIEXPORT void JNICALL
227 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
228 {
229 if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
230 JNU_ThrowInternalError(env, 0);
231 }
232 }
|