• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include <nativehelper/JNIPlatformHelp.h>
20 
21 namespace android {
22 
AutoBufferPointer(JNIEnv * env,jobject nioBuffer,jboolean commit)23 AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit)
24         : fEnv(env), fCommit(commit) {
25     jlong pointer = jniGetNioBufferPointer(fEnv, nioBuffer);
26     if (pointer != 0L) {
27         // Buffer is backed by a direct buffer.
28         fArray = nullptr;
29         fElements = nullptr;
30         fPointer = reinterpret_cast<void*>(pointer);
31     } else {
32         // Buffer is backed by a managed array.
33         jint byteOffset = jniGetNioBufferBaseArrayOffset(fEnv, nioBuffer);
34         fArray = jniGetNioBufferBaseArray(fEnv, nioBuffer);
35         fElements = fEnv->GetPrimitiveArrayCritical(fArray, /* isCopy= */ nullptr);
36         fPointer = reinterpret_cast<void*>(reinterpret_cast<char*>(fElements) + byteOffset);
37     }
38 }
39 
~AutoBufferPointer()40 AutoBufferPointer::~AutoBufferPointer() {
41     if (nullptr != fArray) {
42         fEnv->ReleasePrimitiveArrayCritical(fArray, fElements, fCommit ? 0 : JNI_ABORT);
43     }
44 }
45 
46 }  // namespace android
47