GetDirectBufferCapacity
jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf);
Fetches and returns the capacity in bytes of the memory region referenced by the given direct java.nio.Buffer.
LINKAGE of GetDirectBufferCapacity
Index 231 in the JNIEnv interface function table.
PARAMETERS of GetDirectBufferCapacity
env: the JNIEnv interface pointer
buf: a direct java.nio.Buffer object (must not be NULL)
RETURNS of GetDirectBufferCapacity
Returns the capacity in bytes of the memory region associated with the buffer. Returns -1 if the given object is not a directjava.nio.Buffer, if the object is an unaligned view buffer and the processor architecture does not support unaligned access, or if JNI access to direct buffers is not supported by this virtual machine.
Example of GetDirectBufferCapacity
public class SomeClass {
/**
* @param buffer input/output buffer
* @return number of bytes written to buffer
*/
private native int nativeMethod(ByteBuffer buffer);
public void myMethod() {
ByteBuffer buffer = ByteBuffer.allocateDirect(100);
int written = nativeMethod(buffer);
if (written > 0) {
buffer.limit(written);
}
...
}
}
static jint nativeMethod(JNIEnv *env, jobject obj, jobject buffer) {
jclass cls = env->GetObjectClass(buffer);
jmethodID mid = env->GetMethodID(cls, "limit", "(I)Ljava/nio/Buffer;");
char *buf = (char*)env->GetDirectBufferAddress(buffer);
jlong capacity = env->GetDirectBufferCapacity(buffer);
int written = 0;
// Do something spectacular with the buffer...
env->CallObjectMethod(buffer, mid, written);
return written;
}