accessing DirectByteBuffer
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Monday, March 11, 2002 01:52 PM

i've created a Direct Byte Buffer by calling this command in my native c code: jobject byteBuffer = (*env) ->NewDirectByteBuffer(env, &buffer[0], 4096); on space that I malloced using: char* buffer = (char*) malloc((unsigned)4096); the Direct Byte Buffer is returned to Java as type Buffer. Now the problem is that I can't access the data from the buffer as an array with the .array() method. For some reason I have to copy the information out of the Buffer into another array. I am trying to access the data without having to copy it into another array. Is this a limitation in creating the Direct Byte Buffer in native C code?    More>>

i've created a Direct Byte Buffer by calling this command in my native c code:

jobject byteBuffer = (*env) ->NewDirectByteBuffer(env, &buffer[0], 4096);

on space that I malloced using:
char* buffer = (char*) malloc((unsigned)4096);

the Direct Byte Buffer is returned to Java as type Buffer.

Now the problem is that I can't access the data from the buffer as an array with the .array() method. For some reason I have to copy the information out of the Buffer into another array. I am trying to access the data without having to copy it into another array.

Is this a limitation in creating the Direct Byte Buffer in native C code?

   <<Less

Re: accessing DirectByteBuffer

Posted By:   Stephanie_Legault  
Posted On:   Monday, March 25, 2002 07:53 AM

If you check the Documentation on the ByteBuffer (http://java.sun.com/j2se/1.4/docs/api/index.html) you will see that next to the array() function description there is an "optional operation" comment. The JNI implementation of the ByteBuffer does not implement this optional operation, so you have to do a copy. Call hasArray() to find out if the optional array() function is implemented.

Also, if you are using the same buffer over and over, don't forget to rewind() before get() operations, or exceptions will be generated.

About | Sitemap | Contact