blob: 19a1c7212fae0fac9fb5a78e2a0e35c5ba9abafa [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 -080021void* android::nio_getPointer(JNIEnv *_env, jobject buffer, jarray *array) {
22 assert(array);
23
Orion Hodson02440d32019-02-28 15:15:34 +000024 jint position;
25 jint limit;
26 jint elementSizeShift;
27 jlong pointer = jniGetNioBufferFields(_env, buffer, &position, &limit, &elementSizeShift);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028 if (pointer != 0L) {
Orion Hodson02440d32019-02-28 15:15:34 +000029 pointer += position << elementSizeShift;
30 return reinterpret_cast<void*>(pointer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 }
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070032
Orion Hodson02440d32019-02-28 15:15:34 +000033 jint offset = jniGetNioBufferBaseArrayOffset(_env, buffer);
34 *array = jniGetNioBufferBaseArray(_env, buffer);
35 void * data = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0);
36 return reinterpret_cast<void*>(reinterpret_cast<char*>(data) + offset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037}
38
39
40void android::nio_releasePointer(JNIEnv *_env, jarray array, void *data,
41 jboolean commit) {
42 _env->ReleasePrimitiveArrayCritical(array, data,
43 commit ? 0 : JNI_ABORT);
44}
45
46///////////////////////////////////////////////////////////////////////////////
47
48android::AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer,
49 jboolean commit) {
50 fEnv = env;
51 fCommit = commit;
52 fPointer = android::nio_getPointer(env, nioBuffer, &fArray);
53}
54
55android::AutoBufferPointer::~AutoBufferPointer() {
56 if (NULL != fArray) {
57 android::nio_releasePointer(fEnv, fArray, fPointer, fCommit);
58 }
59}