blob: 69c360c7b56917810518a75c4d806c25e9435ef2 [file] [log] [blame]
* Copyright (C) 2008 The Android Open Source Project
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
#ifndef android_nio_utils_DEFINED
#define android_nio_utils_DEFINED
#include <android_runtime/AndroidRuntime.h>
namespace android {
* Given an nio.Buffer, return a pointer to it, beginning at its current
* position. The returned pointer is only valid for the current JNI stack-frame.
* For performance, it does not create any global references, so the getPointer
* (and releasePointer if array is returned non-null) must be done in the
* same JNI stack-frame.
* @param env The current JNI env
* @param buffer The nio.Buffer object
* @param array REQUIRED. Output. If on return it is set to non-null, then
* nio_releasePointer must be called with the array
* and the returned pointer when the caller is through with it.
* If on return it is set to null, do not call
* nio_releasePointer.
* @return The pointer to the memory in the buffer object
void* nio_getPointer(JNIEnv *env, jobject buffer, jarray *array);
* Call this if android_nio_getPointer returned non-null in its array parameter.
* Pass that array and the returned pointer when you are done accessing the
* pointer. If called (i.e. array is non-null), it must be called in the same
* JNI stack-frame as getPointer
* @param env The current JNI env
* @param buffer The array returned from android_nio_getPointer (!= null)
* @param pointer The pointer returned by android_nio_getPointer
* @param commit JNI_FALSE if the pointer was just read, and JNI_TRUE if
* the pointer was written to.
void nio_releasePointer(JNIEnv *env, jarray array, void *pointer,
jboolean commit);
class AutoBufferPointer {
AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit);
void* pointer() const { return fPointer; }
JNIEnv* fEnv;
void* fPointer;
jarray fArray;
jint fRemaining;
jboolean fCommit;
} /* namespace android */