jdk/src/share/native/java/util/zip/ZipEntry.c
changeset 2673 edc66690be08
parent 2672 f3be0d512b5d
parent 2667 22ee97da5fd1
child 2682 045499ae1036
equal deleted inserted replaced
2672:f3be0d512b5d 2673:edc66690be08
     1 /*
       
     2  * Copyright 1998 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.ZipEntry
       
    28  */
       
    29 
       
    30 #include <stdio.h>
       
    31 #include <stdlib.h>
       
    32 #include "jlong.h"
       
    33 #include "jvm.h"
       
    34 #include "jni.h"
       
    35 #include "jni_util.h"
       
    36 #include "zip_util.h"
       
    37 
       
    38 #include "java_util_zip_ZipEntry.h"
       
    39 
       
    40 #define DEFLATED 8
       
    41 #define STORED 0
       
    42 
       
    43 static jfieldID nameID;
       
    44 static jfieldID timeID;
       
    45 static jfieldID crcID;
       
    46 static jfieldID sizeID;
       
    47 static jfieldID csizeID;
       
    48 static jfieldID methodID;
       
    49 static jfieldID extraID;
       
    50 static jfieldID commentID;
       
    51 
       
    52 JNIEXPORT void JNICALL
       
    53 Java_java_util_zip_ZipEntry_initIDs(JNIEnv *env, jclass cls)
       
    54 {
       
    55     nameID = (*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");
       
    56     timeID = (*env)->GetFieldID(env, cls, "time", "J");
       
    57     crcID = (*env)->GetFieldID(env, cls, "crc", "J");
       
    58     sizeID = (*env)->GetFieldID(env, cls, "size", "J");
       
    59     csizeID = (*env)->GetFieldID(env, cls, "csize", "J");
       
    60     methodID = (*env)->GetFieldID(env, cls, "method", "I");
       
    61     extraID = (*env)->GetFieldID(env, cls, "extra", "[B");
       
    62     commentID = (*env)->GetFieldID(env, cls, "comment", "Ljava/lang/String;");
       
    63 }
       
    64 
       
    65 JNIEXPORT void JNICALL
       
    66 Java_java_util_zip_ZipEntry_initFields(JNIEnv *env, jobject obj, jlong zentry)
       
    67 {
       
    68     jzentry *ze = jlong_to_ptr(zentry);
       
    69     jstring name = (*env)->GetObjectField(env, obj, nameID);
       
    70 
       
    71     if (name == 0) {
       
    72         name = (*env)->NewStringUTF(env, ze->name);
       
    73         if (name == 0) {
       
    74             return;
       
    75         }
       
    76         (*env)->SetObjectField(env, obj, nameID, name);
       
    77     }
       
    78     (*env)->SetLongField(env, obj, timeID, (jlong)ze->time & 0xffffffffUL);
       
    79     (*env)->SetLongField(env, obj, crcID, (jlong)ze->crc & 0xffffffffUL);
       
    80     (*env)->SetLongField(env, obj, sizeID, (jlong)ze->size);
       
    81     if (ze->csize == 0) {
       
    82         (*env)->SetLongField(env, obj, csizeID, (jlong)ze->size);
       
    83         (*env)->SetIntField(env, obj, methodID, STORED);
       
    84     } else {
       
    85         (*env)->SetLongField(env, obj, csizeID, (jlong)ze->csize);
       
    86         (*env)->SetIntField(env, obj, methodID, DEFLATED);
       
    87     }
       
    88     if (ze->extra != 0) {
       
    89         unsigned char *bp = (unsigned char *)&ze->extra[0];
       
    90         jsize len = (bp[0] | (bp[1] << 8));
       
    91         jbyteArray extra = (*env)->NewByteArray(env, len);
       
    92         if (extra == 0) {
       
    93             return;
       
    94         }
       
    95         (*env)->SetByteArrayRegion(env, extra, 0, len, &ze->extra[2]);
       
    96         (*env)->SetObjectField(env, obj, extraID, extra);
       
    97     }
       
    98     if (ze->comment != 0) {
       
    99         jstring comment = (*env)->NewStringUTF(env, ze->comment);
       
   100         if (comment == 0) {
       
   101             return;
       
   102         }
       
   103         (*env)->SetObjectField(env, obj, commentID, comment);
       
   104     }
       
   105 }