blob: 584e7a43ccd92c008938c98007f7b77886fab78a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
19struct NioJNIData {
20 jclass nioAccessClass;
21
22 jmethodID getBasePointerID;
23 jmethodID getBaseArrayID;
24 jmethodID getBaseArrayOffsetID;
25};
26
27static NioJNIData gNioJNI;
28
29void* 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
53void 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
61android::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
68android::AutoBufferPointer::~AutoBufferPointer() {
69 if (NULL != fArray) {
70 android::nio_releasePointer(fEnv, fArray, fPointer, fCommit);
71 }
72}
73
74///////////////////////////////////////////////////////////////////////////////
75
76static 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
82static 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
89static 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
96namespace android {
97
98int register_android_nio_utils(JNIEnv* env);
99int register_android_nio_utils(JNIEnv* env) {
100 jclass localClass = findClass(env, "java/nio/NIOAccess");
101 gNioJNI.getBasePointerID = findStaticMethod(env, localClass,
102 "getBasePointer", "(Ljava/nio/Buffer;)J");
103 gNioJNI.getBaseArrayID = findStaticMethod(env, localClass,
104 "getBaseArray", "(Ljava/nio/Buffer;)Ljava/lang/Object;");
105 gNioJNI.getBaseArrayOffsetID = findStaticMethod(env, localClass,
106 "getBaseArrayOffset", "(Ljava/nio/Buffer;)I");
107
108 // now record a permanent version of the class ID
109 gNioJNI.nioAccessClass = (jclass) env->NewGlobalRef(localClass);
110
111 return 0;
112}
113
114}