Howto create IntBufferd from JNI?

Hi there!

I just saw that according to this document http://java.sun.com/j2se/1.4.2/docs/guide/jni/jni-14.html that its just possible to create ByteBuffers from JNI.
Is it possible to create int-Buffers too from JNI or are there some magically tricks I can see an int[] out of an bytebuffer withought copying the data arround?

Thank you in advance, lg Clemens

It depends:


ByteBuffer byteBuff1 = ByteBuffer.wrap(new byte[]{1, 2, 3, 4});
ByteBuffer byteBuff2 = ByteBuffer.allocateDirect(16).order(ByteOrder.nativeOrder()).put(new byte[]{1, 2, 3, 4});
        
IntBuffer intBuff1 = ByteBuffer.wrap(byteBuff1.array()).asIntBuffer();
System.out.println(intBuff1.toString());
IntBuffer intBuff2 = ByteBuffer.wrap(byteBuff2.array()).asIntBuffer();
System.out.println(intBuff2.toString());

The stuff involving byteBuff1 works perfectly fine, but with byteBuff2 it fails with an UnsupportedOperationException becuase there is no explicit array backing the buffer.

-=EDIT=-


ByteBuffer byteBuff2 = ByteBuffer.allocate(16).order(ByteOrder.nativeOrder()).put(new byte[]{1, 2, 3, 4});

This works too, so it really just depends on how they allocate the buffer in their JNI stuff!

Create ByteBuffer from JNI, then view it in native order in Java as IntBuffer in native order.

If you’d like some assembly for manipulation on the native side then

ebx = GetDirectBufferAddress(JNIEnv* env, jobject buf)
next element = ebx + 4

:slight_smile: