2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "android_nio_utils.h"
20 jclass nioAccessClass;
22 jmethodID getBasePointerID;
23 jmethodID getBaseArrayID;
24 jmethodID getBaseArrayOffsetID;
27 static NioJNIData gNioJNI;
29 void* android::nio_getPointer(JNIEnv *_env, jobject buffer, jarray *array) {
36 pointer = _env->CallStaticLongMethod(gNioJNI.nioAccessClass,
37 gNioJNI.getBasePointerID, buffer);
40 return (void *) (jint) pointer;
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);
49 return (void *) ((char *) data + offset);
53 void android::nio_releasePointer(JNIEnv *_env, jarray array, void *data,
55 _env->ReleasePrimitiveArrayCritical(array, data,
56 commit ? 0 : JNI_ABORT);
59 ///////////////////////////////////////////////////////////////////////////////
61 android::AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer,
65 fPointer = android::nio_getPointer(env, nioBuffer, &fArray);
68 android::AutoBufferPointer::~AutoBufferPointer() {
70 android::nio_releasePointer(fEnv, fArray, fPointer, fCommit);
74 ///////////////////////////////////////////////////////////////////////////////
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);
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);
89 static jfieldID getFieldID(JNIEnv* env, jclass c, const char name[],
91 jfieldID f = env->GetFieldID(c, name, type);
92 LOG_FATAL_IF(!f, "Unable to find field %s", name);
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");
107 // now record a permanent version of the class ID
108 gNioJNI.nioAccessClass = (jclass) env->NewGlobalRef(localClass);