blob: c634cb9177196f429e61a4020fa5a5367a0ff7bb [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
Andreas Gampeed6b9df2014-11-20 22:02:20 -080017#ifndef _ANDROID_NIO_UTILS_H_
18#define _ANDROID_NIO_UTILS_H_
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
20#include <android_runtime/AndroidRuntime.h>
21
22namespace android {
23
24/**
25 * Given an nio.Buffer, return a pointer to it, beginning at its current
26 * position. The returned pointer is only valid for the current JNI stack-frame.
27 * For performance, it does not create any global references, so the getPointer
28 * (and releasePointer if array is returned non-null) must be done in the
29 * same JNI stack-frame.
30 *
31 * @param env The current JNI env
32 * @param buffer The nio.Buffer object
33 * @param array REQUIRED. Output. If on return it is set to non-null, then
34 * nio_releasePointer must be called with the array
35 * and the returned pointer when the caller is through with it.
36 * If on return it is set to null, do not call
37 * nio_releasePointer.
38 * @return The pointer to the memory in the buffer object
39 */
40void* nio_getPointer(JNIEnv *env, jobject buffer, jarray *array);
41
42/**
43 * Call this if android_nio_getPointer returned non-null in its array parameter.
44 * Pass that array and the returned pointer when you are done accessing the
45 * pointer. If called (i.e. array is non-null), it must be called in the same
46 * JNI stack-frame as getPointer
47 *
48 * @param env The current JNI env
49 * @param buffer The array returned from android_nio_getPointer (!= null)
50 * @param pointer The pointer returned by android_nio_getPointer
51 * @param commit JNI_FALSE if the pointer was just read, and JNI_TRUE if
52 * the pointer was written to.
53 */
54void nio_releasePointer(JNIEnv *env, jarray array, void *pointer,
55 jboolean commit);
56
57class AutoBufferPointer {
58public:
59 AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit);
60 ~AutoBufferPointer();
Bernhard Rosenkränzer4048a4b2014-11-23 22:24:32 +010061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 void* pointer() const { return fPointer; }
Bernhard Rosenkränzer4048a4b2014-11-23 22:24:32 +010063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064private:
65 JNIEnv* fEnv;
66 void* fPointer;
67 jarray fArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 jboolean fCommit;
69};
70
71} /* namespace android */
72
Andreas Gampeed6b9df2014-11-20 22:02:20 -080073#endif // _ANDROID_NIO_UTILS_H_