Albert Strasheim wrote:
Unless I'm mistaken, ByteBuffer#asFloatBuffer() returns a FloatBuffer
that throws UnsupportedOperationException when you call array(), so
there's no way for me to get a float[] from this kind of buffer without
copying.
There's no way to get a float[] without copying.
The JVM doesn't appear to support mapping a region of native memory to
primitive arrays - it all has to go through a DirectByteBuffer, and be
accessed via ByteBuffer.get(), ByteBuffer.asFloatBuffer() and friends.
Thus, it would be useful if I could pass these kinds of buffers directly
to JNA.
The best I can do is make something like this work:
interface Foo extends Library {
void some_function(FloatBuffer fb);
}
which would be equivalent to:
interface Foo extends Library {
void some_function(float[] fb);
}
That _should_ pass the array directly to JNI - depending on whether the
VM supports/can pin the array. If not it will copy to/from a native
buffer and pass that on. If your native code depends on the address of
the array not changing, that will break, but otherwise it should be fine.