Getting Array out of ByteBuffer

278 views
Skip to first unread message

arun gupta

unread,
Mar 16, 2010, 5:40:59 PM3/16/10
to android-ndk
In our C code, the image data is getting processed and the outcome is
an array of bytes. The C code gets called from Java code, and the Java
code will make use of the array of bytes returned. Since the data that
is being handled is big, I am using ByteBuffer.allocateDirect(10MB).
The data seems to be getting processed ok in C. However, how do I get
the array of the bytes associated with ByteBuffer.

ByteBuffer.hasArray() is returning false and I cannot get anything out
of ByteBuffer.array(). Is there a way to get the array from
ByteBuffer. I saw that this method has been implemented only in
classes extended from ByteBuffer like ShortBuffer, etc.

What is the best way to have bytes allocated directly from Java,
passed on to C and get the data in a byteArray?

Thanks for your help.

mic _

unread,
Mar 17, 2010, 6:23:24 AM3/17/10
to andro...@googlegroups.com
char *buffer = (char*)(env->GetDirectBufferAddress(byteBuffer));

/Michael

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>

HeHe

unread,
Mar 17, 2010, 11:50:08 AM3/17/10
to android-ndk
i believe what blocks arun is not in C space but in Java space on how
to 'cast' the direct buffer in java byte[]. am i correct?

fadden

unread,
Mar 18, 2010, 4:16:39 PM3/18/10
to android-ndk
On Mar 17, 8:50 am, HeHe <cnm...@gmail.com> wrote:
> i believe what blocks arun is not in C space but in Java space on how
> to 'cast' the direct buffer in java byte[]. am i correct?

"Direct" byte buffers don't have a byte[] associated with them; the
storage is external to the VM. The array() operation doesn't work.

To access from C, Michael's answer is correct (though I'd tend to
leave them as a jbyte* just to remind you whence they came).

To access from Java as an array, you'd need to allocate a new byte[]
and copy the data over. For 10MB of data this isn't a great plan --
large allocation, expensive copy. If the number of accesses is small,
the various get/position calls will suffice.

Reply all
Reply to author
Forward
0 new messages