2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _ANDROID_NIO_UTILS_H_
18 #define _ANDROID_NIO_UTILS_H_
20 #include <android_runtime/AndroidRuntime.h>
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.
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
38 * @return The pointer to the memory in the buffer object
40 void* nio_getPointer(JNIEnv *env, jobject buffer, jarray *array);
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
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.
54 void nio_releasePointer(JNIEnv *env, jarray array, void *pointer,
57 class AutoBufferPointer {
59 AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit);
62 void* pointer() const { return fPointer; }
71 } /* namespace android */
73 #endif // _ANDROID_NIO_UTILS_H_