I want to pass the memory location of a byte array to a jni call so that the array does not have to be copied. There is a question here that hints this is possible with sun.misc.Unsafe. Is there an example of how this can be done?
The use case is that I have a BufferedImage and I have a OpenGL method that takes in a pointer. I know that the obvious thing to do is to allocate a direct bytebuffer but I want to save on the copy.
First off, if at all feasible, migrate to JNA, it's much more user friendly (doesn't require you to compile c files)
If you want to pass data directly to native code, don't use an array, but use a ByteBuffer
instead. To create it, use ByteBuffer.allocateDirect()
(the direct part signifies that it doesn't use an internal array but 'direct' memory.) JNI and JNA should automatically detect this and pass the pointer to the native library.