开发者

How to write/read the direct ByteBuffer in the native?

I want to receive data from a socket in native part and then read the data in the Java code. I know that the direct ByteBuffer might be a good choice. So I prepare a ByteBuffer in the Java something like

ByteBuffer myBuffer = ByteBuffer.allocateDirect(128); 

And I get this ByteBuffer in the native by the following code

JNIEXPORT jboolean JNICALL Java_nfore_android_bt_pro_nfhfp_rcvSco(JNIEnv *env, jobject this, jint fd, jobject buff){

int buff_size;
int socketfd;

jbyte *BUFF = (*env)->GetDirectBufferAddress(env, buff); 
buff_size = (*env)->GetDirectBufferCapacity(env, buff);

socketfd = fd; .....}

Now, my stupid problem are

(1) Is it correct that I could get int type variable, socketfd, just from the statement "socketfd = fd" ??? (fd is jint type, and it is really a socket fd in Linux) (2) How could I write the da开发者_运维知识库ta to this ByteBuffer in C ???

Indeed,... My situation is that I get a Linux socket fd in another native C++ function, however, I could not add functions I want in it. I want to use this fd to read/write data, but I know it is impossible in Java code. So I decide to pass it to native and use a direct ByteBuffer to store the temp data.

Any suggestion ?

Thank you.


You can read write to FileDescriptors in Java.

However to read/write to the direct buffer all you need to is read/write from your BUFF and update the position and limit as appropriate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜