blob: ed8c6038f69d83b635328579c2682bc3bd5b3530 [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
Andreas Gampe987f79f2014-11-18 17:29:46 -080019#include "core_jni_helpers.h"
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021struct NioJNIData {
22 jclass nioAccessClass;
23
24 jmethodID getBasePointerID;
25 jmethodID getBaseArrayID;
26 jmethodID getBaseArrayOffsetID;
27};
28
29static NioJNIData gNioJNI;
30
31void* android::nio_getPointer(JNIEnv *_env, jobject buffer, jarray *array) {
32 assert(array);
33
34 jlong pointer;
35 jint offset;
36 void *data;
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 pointer = _env->CallStaticLongMethod(gNioJNI.nioAccessClass,
39 gNioJNI.getBasePointerID, buffer);
40 if (pointer != 0L) {
41 *array = NULL;
Ashok Bhatf5df7002014-03-25 20:51:35 +000042 return reinterpret_cast<void *>(pointer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 }
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 *array = (jarray) _env->CallStaticObjectMethod(gNioJNI.nioAccessClass,
46 gNioJNI.getBaseArrayID, buffer);
47 offset = _env->CallStaticIntMethod(gNioJNI.nioAccessClass,
48 gNioJNI.getBaseArrayOffsetID, buffer);
49 data = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0);
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 return (void *) ((char *) data + offset);
52}
53
54
55void android::nio_releasePointer(JNIEnv *_env, jarray array, void *data,
56 jboolean commit) {
57 _env->ReleasePrimitiveArrayCritical(array, data,
58 commit ? 0 : JNI_ABORT);
59}
60
61///////////////////////////////////////////////////////////////////////////////
62
63android::AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer,
64 jboolean commit) {
65 fEnv = env;
66 fCommit = commit;
67 fPointer = android::nio_getPointer(env, nioBuffer, &fArray);
68}
69
70android::AutoBufferPointer::~AutoBufferPointer() {
71 if (NULL != fArray) {
72 android::nio_releasePointer(fEnv, fArray, fPointer, fCommit);
73 }
74}
75
76///////////////////////////////////////////////////////////////////////////////
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078namespace android {
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080int register_android_nio_utils(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -080081 jclass localClass = FindClassOrDie(env, "java/nio/NIOAccess");
82 gNioJNI.getBasePointerID = GetStaticMethodIDOrDie(env, localClass, "getBasePointer",
83 "(Ljava/nio/Buffer;)J");
84 gNioJNI.getBaseArrayID = GetStaticMethodIDOrDie(env, localClass, "getBaseArray",
85 "(Ljava/nio/Buffer;)Ljava/lang/Object;");
86 gNioJNI.getBaseArrayOffsetID = GetStaticMethodIDOrDie(env, localClass, "getBaseArrayOffset",
87 "(Ljava/nio/Buffer;)I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89 // now record a permanent version of the class ID
Andreas Gampe987f79f2014-11-18 17:29:46 -080090 gNioJNI.nioAccessClass = MakeGlobalRefOrDie(env, localClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 return 0;
93}
94
95}