jdk/src/share/native/com/sun/java/util/jar/pack/jni.cpp
author ksrini
Wed, 09 May 2012 07:28:12 -0700
changeset 12680 289f842c0175
parent 5506 202f599c92aa
child 16074 97593512057a
permissions -rw-r--r--
7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking Reviewed-by: alanb, dholmes, ksrini Contributed-by: youdwei@linux.vnet.ibm.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12680
289f842c0175 7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdarg.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <com_sun_java_util_jar_pack_NativeUnpack.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "defines.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "bytes.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "utils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "coding.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "bands.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "constants.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "zip.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include "unpack.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
static jfieldID  unpackerPtrFID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static jmethodID currentInstMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
static jmethodID readInputMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static jclass    NIclazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static char* dbg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#define THROW_IOE(x) JNU_ThrowIOException(env,x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
static jlong read_input_via_jni(unpacker* self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                void* buf, jlong minlen, jlong maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
static unpacker* get_unpacker(JNIEnv *env, jobject pObj, bool noCreate=false) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    62
  unpacker* uPtr;
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    63
  uPtr = (unpacker*)jlong2ptr(env->GetLongField(pObj, unpackerPtrFID));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  //fprintf(stderr, "get_unpacker(%p) uPtr=%p\n", pObj, uPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  if (uPtr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if (noCreate)  return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    uPtr = new unpacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if (uPtr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      THROW_IOE(ERROR_ENOMEM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    //fprintf(stderr, "get_unpacker(%p) uPtr=%p initializing\n", pObj, uPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    uPtr->init(read_input_via_jni);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    uPtr->jniobj = (void*) env->NewGlobalRef(pObj);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    75
    env->SetLongField(pObj, unpackerPtrFID, ptr2jlong(uPtr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  uPtr->jnienv = env;  // keep refreshing this in case of MT access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  return uPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
// This is the harder trick:  Pull the current state out of mid-air.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static unpacker* get_unpacker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  //fprintf(stderr, "get_unpacker()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  JavaVM* vm = null;
12680
289f842c0175 7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents: 5506
diff changeset
    85
  jsize nVM = 0;
289f842c0175 7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents: 5506
diff changeset
    86
  jint retval = JNI_GetCreatedJavaVMs(&vm, 1, &nVM);
289f842c0175 7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents: 5506
diff changeset
    87
  // other VM implements may differ, thus for correctness, we need these checks
289f842c0175 7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents: 5506
diff changeset
    88
  if (retval != JNI_OK || nVM != 1)
289f842c0175 7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents: 5506
diff changeset
    89
    return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  void* envRaw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
  vm->GetEnv(&envRaw, JNI_VERSION_1_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  JNIEnv* env = (JNIEnv*) envRaw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  //fprintf(stderr, "get_unpacker() env=%p\n", env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  if (env == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  jobject pObj = env->CallStaticObjectMethod(NIclazz, currentInstMID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  //fprintf(stderr, "get_unpacker() pObj=%p\n", pObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
  if (pObj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  // Got pObj and env; now do it the easy way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  return get_unpacker(env, pObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
static void free_unpacker(JNIEnv *env, jobject pObj, unpacker* uPtr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
  if (uPtr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    //fprintf(stderr, "free_unpacker(%p) uPtr=%p\n", pObj, uPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    env->DeleteGlobalRef((jobject) uPtr->jniobj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    uPtr->jniobj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    uPtr->free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    delete uPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    env->SetLongField(pObj, unpackerPtrFID, (jlong)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
unpacker* unpacker::current() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  return get_unpacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
// Callback for fetching data, Java style.  Calls NativeUnpack.readInputFn().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
static jlong read_input_via_jni(unpacker* self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                void* buf, jlong minlen, jlong maxlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  JNIEnv* env = (JNIEnv*) self->jnienv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  jobject pbuf = env->NewDirectByteBuffer(buf, maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  return env->CallLongMethod((jobject) self->jniobj, readInputMID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                             pbuf, minlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
Java_com_sun_java_util_jar_pack_NativeUnpack_initIDs(JNIEnv *env, jclass clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  dbg = getenv("DEBUG_ATTACH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  while( dbg != null) { sleep(10); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  NIclazz = (jclass) env->NewGlobalRef(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  unpackerPtrFID = env->GetFieldID(clazz, "unpackerPtr", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  currentInstMID = env->GetStaticMethodID(clazz, "currentInstance",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                          "()Ljava/lang/Object;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  readInputMID = env->GetMethodID(clazz, "readInputFn",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                  "(Ljava/nio/ByteBuffer;J)J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
  if (unpackerPtrFID == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
      currentInstMID == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
      readInputMID == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
      NIclazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    THROW_IOE("cannot init class members");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
Java_com_sun_java_util_jar_pack_NativeUnpack_start(JNIEnv *env, jobject pObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                   jobject pBuf, jlong offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
  unpacker* uPtr = get_unpacker(env, pObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
  // redirect our io to the default log file or whatever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
  uPtr->redirect_stdio();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
  void*  buf    = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  size_t buflen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  if (pBuf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    buf    = env->GetDirectBufferAddress(pBuf);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   158
    buflen = (size_t)env->GetDirectBufferCapacity(pBuf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    if (buflen == 0)  buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    if (buf == null) { THROW_IOE(ERROR_INTERNAL); return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    if ((size_t)offset >= buflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      { buf = null; buflen = 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      { buf = (char*)buf + (size_t)offset; buflen -= (size_t)offset; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
  uPtr->start(buf, buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  if (uPtr->aborting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    THROW_IOE(uPtr->get_abort_message());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
  return ((jlong)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
          uPtr->get_segments_remaining() << 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    + uPtr->get_files_remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
Java_com_sun_java_util_jar_pack_NativeUnpack_getNextFile(JNIEnv *env, jobject pObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                         jobjectArray pParts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
  unpacker* uPtr = get_unpacker(env, pObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
  unpacker::file* filep = uPtr->get_next_file();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
  if (uPtr->aborting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    THROW_IOE(uPtr->get_abort_message());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
  if (filep == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    return false;   // end of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
  assert(filep == &uPtr->cur_file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
  int pidx = 0, iidx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
  jintArray pIntParts = (jintArray) env->GetObjectArrayElement(pParts, pidx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  jint*     intParts  = env->GetIntArrayElements(pIntParts, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  intParts[iidx++] = (jint)( (julong)filep->size >> 32 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
  intParts[iidx++] = (jint)( (julong)filep->size >>  0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  intParts[iidx++] = filep->modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
  intParts[iidx++] = filep->deflate_hint() ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
  env->ReleaseIntArrayElements(pIntParts, intParts, JNI_COMMIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
  env->SetObjectArrayElement(pParts, pidx++, env->NewStringUTF(filep->name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
  jobject pDataBuf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  if (filep->data[0].len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    pDataBuf = env->NewDirectByteBuffer(filep->data[0].ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                        filep->data[0].len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
  env->SetObjectArrayElement(pParts, pidx++, pDataBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
  pDataBuf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  if (filep->data[1].len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    pDataBuf = env->NewDirectByteBuffer(filep->data[1].ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                        filep->data[1].len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
  env->SetObjectArrayElement(pParts, pidx++, pDataBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
Java_com_sun_java_util_jar_pack_NativeUnpack_getUnusedInput(JNIEnv *env, jobject pObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
  unpacker* uPtr = get_unpacker(env, pObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
  unpacker::file* filep = &uPtr->cur_file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  if (uPtr->aborting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    THROW_IOE(uPtr->get_abort_message());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
  // We have fetched all the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
  // Now swallow up any remaining input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
  if (uPtr->input_remaining() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    return env->NewDirectByteBuffer(uPtr->input_scan(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                    uPtr->input_remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
Java_com_sun_java_util_jar_pack_NativeUnpack_finish(JNIEnv *env, jobject pObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
  unpacker* uPtr = get_unpacker(env, pObj, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  if (uPtr == null)  return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
  size_t consumed = uPtr->input_consumed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
  free_unpacker(env, pObj, uPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
  return consumed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
Java_com_sun_java_util_jar_pack_NativeUnpack_setOption(JNIEnv *env, jobject pObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                       jstring pProp, jstring pValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
  unpacker*   uPtr  = get_unpacker(env, pObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
  const char* prop  = env->GetStringUTFChars(pProp, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
  const char* value = env->GetStringUTFChars(pValue, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
  jboolean   retval = uPtr->set_option(prop, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
  env->ReleaseStringUTFChars(pProp,  prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
  env->ReleaseStringUTFChars(pValue, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
  return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
Java_com_sun_java_util_jar_pack_NativeUnpack_getOption(JNIEnv *env, jobject pObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                       jstring pProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
  unpacker*   uPtr  = get_unpacker(env, pObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  const char* prop  = env->GetStringUTFChars(pProp, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
  const char* value = uPtr->get_option(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
  env->ReleaseStringUTFChars(pProp, prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
  if (value == null)  return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
  return env->NewStringUTF(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
}