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 #ifndef _ANDROID_NIO_UTILS_H_ 18 #define _ANDROID_NIO_UTILS_H_ 19 20 #include <android_runtime/AndroidRuntime.h> 21 22 namespace 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 */ 40 void* 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 */ 54 void nio_releasePointer(JNIEnv *env, jarray array, void *pointer, 55 jboolean commit); 56 57 class AutoBufferPointer { 58 public: 59 AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit); 60 ~AutoBufferPointer(); 61 pointer()62 void* pointer() const { return fPointer; } 63 64 private: 65 JNIEnv* fEnv; 66 void* fPointer; 67 jarray fArray; 68 jboolean fCommit; 69 }; 70 71 } /* namespace android */ 72 73 #endif // _ANDROID_NIO_UTILS_H_ 74