1 /*
2  * Copyright (C) 2006 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 "include_platform/nativehelper/JNIPlatformHelp.h"
18 
19 #include <stddef.h>
20 
21 #include "JniConstants.h"
22 
23 static int GetBufferPosition(JNIEnv* env, jobject nioBuffer) {
24     return(*env)->GetIntField(env, nioBuffer, JniConstants_NioBuffer_position(env));
25 }
26 
27 static int GetBufferLimit(JNIEnv* env, jobject nioBuffer) {
28     return(*env)->GetIntField(env, nioBuffer, JniConstants_NioBuffer_limit(env));
29 }
30 
31 static int GetBufferElementSizeShift(JNIEnv* env, jobject nioBuffer) {
32     return(*env)->GetIntField(env, nioBuffer, JniConstants_NioBuffer__elementSizeShift(env));
33 }
34 
35 jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
36     jclass nioAccessClass = JniConstants_NIOAccessClass(env);
37     jmethodID getBaseArrayMethod = JniConstants_NIOAccess_getBaseArray(env);
38     jobject object = (*env)->CallStaticObjectMethod(env,
39                                                     nioAccessClass, getBaseArrayMethod, nioBuffer);
40     return (jarray) object;
41 }
42 
43 int jniGetNioBufferBaseArrayOffset(JNIEnv* env, jobject nioBuffer) {
44     jclass nioAccessClass = JniConstants_NIOAccessClass(env);
45     jmethodID getBaseArrayOffsetMethod = JniConstants_NIOAccess_getBaseArrayOffset(env);
46     return (*env)->CallStaticIntMethod(env, nioAccessClass, getBaseArrayOffsetMethod, nioBuffer);
47 }
48 
49 jlong jniGetNioBufferPointer(JNIEnv* env, jobject nioBuffer) {
50     jlong baseAddress = (*env)->GetLongField(env, nioBuffer, JniConstants_NioBuffer_address(env));
51     if (baseAddress != 0) {
52         const int position = GetBufferPosition(env, nioBuffer);
53         const int shift = GetBufferElementSizeShift(env, nioBuffer);
54         baseAddress += position << shift;
55     }
56     return baseAddress;
57 }
58 
59 jlong jniGetNioBufferFields(JNIEnv* env, jobject nioBuffer,
60                             jint* position, jint* limit, jint* elementSizeShift) {
61     *position = GetBufferPosition(env, nioBuffer);
62     *limit = GetBufferLimit(env, nioBuffer);
63     *elementSizeShift = GetBufferElementSizeShift(env, nioBuffer);
64     return (*env)->GetLongField(env, nioBuffer, JniConstants_NioBuffer_address(env));
65 }
66