Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "android_nio_utils.h"
     18 
     19 struct NioJNIData {
     20     jclass nioAccessClass;
     21 
     22     jmethodID getBasePointerID;
     23     jmethodID getBaseArrayID;
     24     jmethodID getBaseArrayOffsetID;
     25 };
     26 
     27 static NioJNIData gNioJNI;
     28 
     29 void* android::nio_getPointer(JNIEnv *_env, jobject buffer, jarray *array) {
     30     assert(array);
     31 
     32     jlong pointer;
     33     jint offset;
     34     void *data;
     35 
     36     pointer = _env->CallStaticLongMethod(gNioJNI.nioAccessClass,
     37                                          gNioJNI.getBasePointerID, buffer);
     38     if (pointer != 0L) {
     39         *array = NULL;
     40         return (void *) (jint) pointer;
     41     }
     42 
     43     *array = (jarray) _env->CallStaticObjectMethod(gNioJNI.nioAccessClass,
     44                                                gNioJNI.getBaseArrayID, buffer);
     45     offset = _env->CallStaticIntMethod(gNioJNI.nioAccessClass,
     46                                        gNioJNI.getBaseArrayOffsetID, buffer);
     47     data = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0);
     48 
     49     return (void *) ((char *) data + offset);
     50 }
     51 
     52 
     53 void android::nio_releasePointer(JNIEnv *_env, jarray array, void *data,
     54                                 jboolean commit) {
     55     _env->ReleasePrimitiveArrayCritical(array, data,
     56                                         commit ? 0 : JNI_ABORT);
     57 }
     58 
     59 ///////////////////////////////////////////////////////////////////////////////
     60 
     61 android::AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer,
     62                                               jboolean commit) {
     63     fEnv = env;
     64     fCommit = commit;
     65     fPointer = android::nio_getPointer(env, nioBuffer, &fArray);
     66 }
     67 
     68 android::AutoBufferPointer::~AutoBufferPointer() {
     69     if (NULL != fArray) {
     70         android::nio_releasePointer(fEnv, fArray, fPointer, fCommit);
     71     }
     72 }
     73 
     74 ///////////////////////////////////////////////////////////////////////////////
     75 
     76 static jclass findClass(JNIEnv* env, const char name[]) {
     77     jclass c = env->FindClass(name);
     78     LOG_FATAL_IF(!c, "Unable to find class %s", name);
     79     return c;
     80 }
     81 
     82 static jmethodID findStaticMethod(JNIEnv* env, jclass c, const char method[],
     83                                   const char params[]) {
     84     jmethodID m = env->GetStaticMethodID(c, method, params);
     85     LOG_FATAL_IF(!m, "Unable to find method %s", method);
     86     return m;
     87 }
     88 
     89 static jfieldID getFieldID(JNIEnv* env, jclass c, const char name[],
     90                            const char type[]) {
     91     jfieldID f = env->GetFieldID(c, name, type);
     92     LOG_FATAL_IF(!f, "Unable to find field %s", name);
     93     return f;
     94 }
     95 
     96 namespace android {
     97 
     98 int register_android_nio_utils(JNIEnv* env) {
     99     jclass localClass = findClass(env, "java/nio/NIOAccess");
    100     gNioJNI.getBasePointerID = findStaticMethod(env, localClass,
    101                                     "getBasePointer", "(Ljava/nio/Buffer;)J");
    102     gNioJNI.getBaseArrayID = findStaticMethod(env, localClass,
    103                     "getBaseArray", "(Ljava/nio/Buffer;)Ljava/lang/Object;");
    104     gNioJNI.getBaseArrayOffsetID = findStaticMethod(env, localClass,
    105                                 "getBaseArrayOffset", "(Ljava/nio/Buffer;)I");
    106 
    107     // now record a permanent version of the class ID
    108     gNioJNI.nioAccessClass = (jclass) env->NewGlobalRef(localClass);
    109 
    110     return 0;
    111 }
    112 
    113 }
    114