Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write and read from bytebuffer passing from java to jni

I need help with my android project. I want to pass a buffer from java to jni, and my C++ code will populate the data. Then java will display them on the screen. I am not familiar much with C++ and have no idea how to write to write to the buffer.

this is what i got. in java

ByteBuffer bb = ByteBuffer.allocateDirect(216);
        IssmJni.processBuffer(bb);

native method

public static native void processBuffer(ByteBuffer bb);

I don't use jni_onload, so no javah in C++

static void fillBuffer(JNIEnv *env, jclass clazz, jobject buf)
    {
        double *dBuf = env->GetDirectBufferAddress(env, buf);

    }

I am stuck here, can I do double dbuf? or is it has to be a char?

Let said I want to write 1,2,3,4,5 to this dbuf, how do I do it? i am thinking of dbuf.put(1); ... dbuf.put(5) but it is not working. and after populating it, do I just call bb.get(position) in java?

Someone clarify this for me please, example would be appreciated Thank you

this is my table of native methods

static JNINativeMethod method_table[] = {

{"fac"      ,     "(J)J" , (void *) factorial},
    {"getBuffer",     "()[D" , (void *) getBufferNative},
    //{"processBuffer", "(Ljava/nio/ByteBuffer)V", (void *) fillBuffer}};

The other two works fine except the last one.

like image 771
Lan Nguyen Avatar asked Nov 14 '12 19:11

Lan Nguyen


1 Answers

I don't use jni_onload, so no javah in C++

It may be irrelevant to your question, but you sort of have to do one or the other. You either need to do your Java-to-Native method mapping in the onLoad function or use javah to generate the method signatures that JNI can pick up at runtime. Otherwise your native methods will never be invoked.

If your native method is properly being called, however, whatever you are doing is probably fine.

I am stuck here, can I do double dbuf? or is it has to be a char?

The return value of the function is actually a void*, so you can cast it to whatever pointer type you want since it is just a memory address. However, the actual data type of the direct buffer will be jbyte, which is a typedef of signed char, so the following is probably best:

jbyte *dBuf = env->GetDirectBufferAddress(env, buf);

Let said I want to write 1,2,3,4,5 to this dbuf, how do I do it?

The notation in C/C++ to access values in an array is with [] notations, i.e.:

jbyte *dBuf = env->GetDirectBufferAddress(env, buf);

dBuf[0] = 63;
dBuf[1] = 127;
// ...and so on...

and after populating it, do I just call bb.get(position) in java?

Yes, you can use the accessor methods on ByteBuffer to access the data you have written from your native code.

like image 90
devunwired Avatar answered Sep 22 '22 00:09

devunwired