My requirement is to pass a char* (character array pointer) from JNI to Java with out actually copying the array. i.e., i want to avoid creating NewByteArray and copying elements in to this byteArray (byte by byte) and then returning the array.
I saw some documentation which says we can allocate direct buffer using NewDirectByteBuffer(). I have tried using it, but when i try to access the array from the ByteBuffer newly created, it always gives UnsupportedOperationException at java level. My code is as follows:
gElement = gMyCppImplementation->getByteBuffer(size);// char* pointer will be returned by my C++ code and size i will fill in the getByteBuffer method
LOGE("After get bytebuffer call address %d, size %d",gElement, size);
LOGE("size of array is=%d ", (int)size);
myByteBuffer = env->NewDirectByteBuffer((void*)gElement, (jlong)size);
return myByteBuffer ;