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
GetBufferPosition(JNIEnv * env,jobject nioBuffer)23 static int GetBufferPosition(JNIEnv* env, jobject nioBuffer) {
24 return(*env)->GetIntField(env, nioBuffer, JniConstants_NioBuffer_position(env));
25 }
26
GetBufferLimit(JNIEnv * env,jobject nioBuffer)27 static int GetBufferLimit(JNIEnv* env, jobject nioBuffer) {
28 return(*env)->GetIntField(env, nioBuffer, JniConstants_NioBuffer_limit(env));
29 }
30
GetBufferElementSizeShift(JNIEnv * env,jobject nioBuffer)31 static int GetBufferElementSizeShift(JNIEnv* env, jobject nioBuffer) {
32 return(*env)->GetIntField(env, nioBuffer, JniConstants_NioBuffer__elementSizeShift(env));
33 }
34
jniCreateFileDescriptor(JNIEnv * env,int fd)35 jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
36 jobject fileDescriptor = (*env)->NewObject(env,
37 JniConstants_FileDescriptorClass(env),
38 JniConstants_FileDescriptor_init(env));
39 // NOTE: NewObject ensures that an OutOfMemoryError will be seen by the Java
40 // caller if the alloc fails, so we just return nullptr when that happens.
41 if (fileDescriptor != NULL) {
42 jniSetFileDescriptorOfFD(env, fileDescriptor, fd);
43 }
44 return fileDescriptor;
45 }
46
jniGetFDFromFileDescriptor(JNIEnv * env,jobject fileDescriptor)47 int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
48 if (fileDescriptor != NULL) {
49 return (*env)->GetIntField(env, fileDescriptor,
50 JniConstants_FileDescriptor_descriptor(env));
51 } else {
52 return -1;
53 }
54 }
55
jniSetFileDescriptorOfFD(JNIEnv * env,jobject fileDescriptor,int value)56 void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
57 if (fileDescriptor == NULL) {
58 jniThrowNullPointerException(env, "null FileDescriptor");
59 } else {
60 (*env)->SetIntField(env,
61 fileDescriptor, JniConstants_FileDescriptor_descriptor(env), value);
62 }
63 }
64
jniGetNioBufferBaseArray(JNIEnv * env,jobject nioBuffer)65 jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
66 jclass nioAccessClass = JniConstants_NIOAccessClass(env);
67 jmethodID getBaseArrayMethod = JniConstants_NIOAccess_getBaseArray(env);
68 jobject object = (*env)->CallStaticObjectMethod(env,
69 nioAccessClass, getBaseArrayMethod, nioBuffer);
70 return (jarray) object;
71 }
72
jniGetNioBufferBaseArrayOffset(JNIEnv * env,jobject nioBuffer)73 int jniGetNioBufferBaseArrayOffset(JNIEnv* env, jobject nioBuffer) {
74 jclass nioAccessClass = JniConstants_NIOAccessClass(env);
75 jmethodID getBaseArrayOffsetMethod = JniConstants_NIOAccess_getBaseArrayOffset(env);
76 return (*env)->CallStaticIntMethod(env, nioAccessClass, getBaseArrayOffsetMethod, nioBuffer);
77 }
78
jniGetNioBufferPointer(JNIEnv * env,jobject nioBuffer)79 jlong jniGetNioBufferPointer(JNIEnv* env, jobject nioBuffer) {
80 jlong baseAddress = (*env)->GetLongField(env, nioBuffer, JniConstants_NioBuffer_address(env));
81 if (baseAddress != 0) {
82 const int position = GetBufferPosition(env, nioBuffer);
83 const int shift = GetBufferElementSizeShift(env, nioBuffer);
84 baseAddress += position << shift;
85 }
86 return baseAddress;
87 }
88
jniGetNioBufferFields(JNIEnv * env,jobject nioBuffer,jint * position,jint * limit,jint * elementSizeShift)89 jlong jniGetNioBufferFields(JNIEnv* env, jobject nioBuffer,
90 jint* position, jint* limit, jint* elementSizeShift) {
91 *position = GetBufferPosition(env, nioBuffer);
92 *limit = GetBufferLimit(env, nioBuffer);
93 *elementSizeShift = GetBufferElementSizeShift(env, nioBuffer);
94 return (*env)->GetLongField(env, nioBuffer, JniConstants_NioBuffer_address(env));
95 }
96