简体   繁体   English

如何写入和读取从java传递到jni的bytebuffer

[英]how to write and read from bytebuffer passing from java to jni

I need help with my android project. 我的android项目需要帮助。 I want to pass a buffer from java to jni, and my C++ code will populate the data. 我想将缓冲区从java传递给jni,我的C ++代码将填充数据。 Then java will display them on the screen. 然后java将在屏幕上显示它们。 I am not familiar much with C++ and have no idea how to write to write to the buffer. 我对C ++并不熟悉,也不知道如何写入写入缓冲区。

this is what i got. 这就是我得到的。 in java 在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++ 我不使用jni_onload,所以在C ++中没有javah

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

    }

I am stuck here, can I do double dbuf? 我被困在这里 ,我可以做双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? 我想说我想给这个dbuf写1,2,3,4,5,我该怎么做? i am thinking of dbuf.put(1); 我在想dbuf.put(1); ... dbuf.put(5) but it is not working. ... dbuf.put(5)但它不起作用。 and after populating it, do I just call bb.get(position) in java? 在填充之后,我只是在java中调用bb.get(position)吗?

Someone clarify this for me please, example would be appreciated Thank you 有人为我澄清这个,例如将不胜感激谢谢

this is my table of native methods 这是我的原生方法表

static JNINativeMethod method_table[] = { 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. 除了最后一个之外,其他两个工作正常。

I don't use jni_onload, so no javah in C++ 我不使用jni_onload,所以在C ++中没有javah

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. 您需要在onLoad函数中执行Java-to-Native方法映射,或者使用javah生成JNI可以在运行时获取的方法签名。 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? 我被困在这里,我可以做双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. 该函数的返回值实际上是一个void* ,因此您可以将它转换为您想要的任何指针类型,因为它只是一个内存地址。 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 ,它是signed char的typedef,因此以下可能是最好的:

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? 我想说我想给这个dbuf写1,2,3,4,5,我该怎么做?

The notation in C/C++ to access values in an array is with [] notations, ie: C / C ++中用于访问数组中值的符号是[]符号,即:

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? 在填充之后,我只是在java中调用bb.get(position)吗?

Yes, you can use the accessor methods on ByteBuffer to access the data you have written from your native code. 是的,您可以使用ByteBuffer上的访问器方法来访问您从本机代码中编写的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM