The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame^] | 19 | #include <nativehelper/JNIHelp.h> |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 20 | |
Orion Hodson | b0461eb | 2019-04-15 22:13:59 +0100 | [diff] [blame] | 21 | namespace android { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Orion Hodson | b0461eb | 2019-04-15 22:13:59 +0100 | [diff] [blame] | 23 | AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit) |
| 24 | : fEnv(env), fCommit(commit) { |
| 25 | jlong pointer = jniGetNioBufferPointer(fEnv, nioBuffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | if (pointer != 0L) { |
Orion Hodson | b0461eb | 2019-04-15 22:13:59 +0100 | [diff] [blame] | 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); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Orion Hodson | b0461eb | 2019-04-15 22:13:59 +0100 | [diff] [blame] | 40 | AutoBufferPointer::~AutoBufferPointer() { |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame] | 41 | if (nullptr != fArray) { |
Orion Hodson | b0461eb | 2019-04-15 22:13:59 +0100 | [diff] [blame] | 42 | fEnv->ReleasePrimitiveArrayCritical(fArray, fElements, fCommit ? 0 : JNI_ABORT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
Orion Hodson | b0461eb | 2019-04-15 22:13:59 +0100 | [diff] [blame] | 45 | |
| 46 | } // namespace android |