src/share/native/java/util/zip/Deflater.c

Print this page




 120 Java_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                 JNU_ThrowOutOfMemoryError(env, 0);
 142                 return 0;
 143             }
 144             (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
 145 
 146             out_buf = (jbyte *) malloc(len);
 147             if (out_buf == 0) {
 148                 free(in_buf);
 149                 JNU_ThrowOutOfMemoryError(env, 0);
 150                 return 0;
 151             }
 152 
 153             strm->next_in = (Bytef *) in_buf;
 154             strm->next_out = (Bytef *) out_buf;
 155             strm->avail_in = this_len;
 156             strm->avail_out = len;
 157             res = deflateParams(strm, level, strategy);
 158 
 159             if (res == Z_OK) {
 160                 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
 161             }
 162             free(out_buf);
 163             free(in_buf);
 164 
 165             switch (res) {
 166             case Z_OK:
 167                 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
 168                 this_off += this_len - strm->avail_in;
 169                 (*env)->SetIntField(env, this, offID, this_off);
 170                 (*env)->SetIntField(env, this, lenID, strm->avail_in);
 171                 return len - strm->avail_out;
 172             case Z_BUF_ERROR:
 173                 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
 174                 return 0;
 175             default:
 176                 JNU_ThrowInternalError(env, strm->msg);
 177                 return 0;
 178             }
 179         } else {
 180             jboolean finish = (*env)->GetBooleanField(env, this, finishID);
 181 
 182             in_buf = (jbyte *) malloc(this_len);
 183             if (in_buf == 0) {
 184                 JNU_ThrowOutOfMemoryError(env, 0);
 185                 return 0;
 186             }
 187             (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
 188 
 189             out_buf = (jbyte *) malloc(len);
 190             if (out_buf == 0) {
 191                 free(in_buf);
 192                 JNU_ThrowOutOfMemoryError(env, 0);
 193                 return 0;
 194             }
 195 
 196             strm->next_in = (Bytef *) in_buf;
 197             strm->next_out = (Bytef *) out_buf;
 198             strm->avail_in = this_len;
 199             strm->avail_out = len;
 200             res = deflate(strm, finish ? Z_FINISH : Z_NO_FLUSH);
 201 
 202             if (res == Z_STREAM_END || res == Z_OK) {
 203                 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
 204             }
 205             free(out_buf);
 206             free(in_buf);
 207 
 208             switch (res) {
 209             case Z_STREAM_END:
 210                 (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);




 120 Java_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 && this_len > 0) {
 141                 JNU_ThrowOutOfMemoryError(env, 0);
 142                 return 0;
 143             }
 144             (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
 145 
 146             out_buf = (jbyte *) malloc(len);
 147             if (out_buf == 0 && len > 0) {
 148                 free(in_buf);
 149                 JNU_ThrowOutOfMemoryError(env, 0);
 150                 return 0;
 151             }
 152 
 153             strm->next_in = (Bytef *) in_buf;
 154             strm->next_out = (Bytef *) out_buf;
 155             strm->avail_in = this_len;
 156             strm->avail_out = len;
 157             res = deflateParams(strm, level, strategy);
 158 
 159             if (res == Z_OK) {
 160                 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
 161             }
 162             free(out_buf);
 163             free(in_buf);
 164 
 165             switch (res) {
 166             case Z_OK:
 167                 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
 168                 this_off += this_len - strm->avail_in;
 169                 (*env)->SetIntField(env, this, offID, this_off);
 170                 (*env)->SetIntField(env, this, lenID, strm->avail_in);
 171                 return len - strm->avail_out;
 172             case Z_BUF_ERROR:
 173                 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
 174                 return 0;
 175             default:
 176                 JNU_ThrowInternalError(env, strm->msg);
 177                 return 0;
 178             }
 179         } else {
 180             jboolean finish = (*env)->GetBooleanField(env, this, finishID);
 181 
 182             in_buf = (jbyte *) malloc(this_len);
 183             if (in_buf == 0 && this_len > 0) {
 184                 JNU_ThrowOutOfMemoryError(env, 0);
 185                 return 0;
 186             }
 187             (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
 188 
 189             out_buf = (jbyte *) malloc(len);
 190             if (out_buf == 0 && len > 0) {
 191                 free(in_buf);
 192                 JNU_ThrowOutOfMemoryError(env, 0);
 193                 return 0;
 194             }
 195 
 196             strm->next_in = (Bytef *) in_buf;
 197             strm->next_out = (Bytef *) out_buf;
 198             strm->avail_in = this_len;
 199             strm->avail_out = len;
 200             res = deflate(strm, finish ? Z_FINISH : Z_NO_FLUSH);
 201 
 202             if (res == Z_STREAM_END || res == Z_OK) {
 203                 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
 204             }
 205             free(out_buf);
 206             free(in_buf);
 207 
 208             switch (res) {
 209             case Z_STREAM_END:
 210                 (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);