简体   繁体   English

从Java存储中检索未知长度的字节数组

[英]Retrieving byte array of unknown length from Java store

I already posted a question regarding it, but at that time I haven't have the account. 我已经发布了一个关于它的问题,但那时我还没有账号。 I got a reply but I was still confused and I cannot continue on that thread. 我得到了答复,但我仍然感到困惑,我不能继续这个帖子。

I am re posting the question again along with a link to previous conversation. 我将再次发布问题以及之前对话的链接。

Returning char array from java to string - JNI 将char数组从java返回到字符串 - JNI

The data I am storing in Java is serialized. 我用Java存储的数据是序列化的。 I make a java function call using following piece of code. 我使用下面的代码进行java函数调用。

The following code assumes that char of C is compatible with byte of Java, because char of Java is of 2 bytes whereas char of C is of 1 byte. 下面的代码假定C的char与Java的字节兼容,因为Java的char是2字节而C的char是1字节。 The jbyte is also a signed char* jbyte也是一个签名的字符*

    //value will be the serialized data
void store(char* key, char* value, int val_len)

{ {

    //consider the jclass and methodid are already initialized

    jstring j_key = (*env)->NewStringUTF(env, key);
    jbyteArray j_value = (*env)->NewByteArray(env, val_len);

    (*env)->SetByteArrayRegion(env, j_value, 0, val_len, (jbyte *)value);

    //The store method on java side will save the value (as is) in memory
    (*env)->CallStaticVoidMethod(j_class, store_method, key, value);

    (*env)->ReleaseByteArrayElements(env, j_value, (jbyte *)value, JNI_ABORT);
    (*env)->ReleaseStringUTFChars(env, j_key, key);

} }

Once I have saved the data, I use another function to retrieve data from store. 保存数据后,我使用另一个函数从商店检索数据。 At that time i do not know the size of data I am going to retrieve. 那时我不知道我要检索的数据大小。 My API is in C and store is in Java. 我的API在C中,存储在Java中。 I will use my C functions to interact with Java. 我将使用我的C函数与Java交互。 And also there can be multiple threads retrieving data from Java store at same time. 并且可以有多个线程同时从Java存储中检索数据。

I am making calls from C to Java and my control should return to C program after retrieving data. 我正在从C调用Java到Java,我的控件应在检索数据后返回C程序。 I am a little confuse on how the code will work. 我对代码如何工作有点困惑。 How will I get pointer to array (retrieved from java) and then retrieve it using GetByteArrayElements. 我将如何获取指向数组的指针(从java检索),然后使用GetByteArrayElements检索它。 Remember I dont know the size of data I am going to retrieve before hand and therefore cannot create a byte array using NewByteArray function and later fill it with data in java code. 记得我不知道我要在手边检索的数据大小,因此不能使用NewByteArray函数创建一个字节数组,然后用java代码中的数据填充它。

Ok, I figuered it out. 好的,我把它弄清楚了。 I'll put it down here so others can also take advantage of it. 我会把它放在这里,以便其他人也可以利用它。

Consider the following java method that returns a byte array (just a dummy code, no checks etc) 考虑以下返回字节数组的java方法(只是一个虚拟代码,没有检查等)

public static byte[] GetData(){
    return myStore.getData();
}

and on C side, you can retrieve the byte[] as following 在C端,您可以检索字节[]如下

    void get_data()
{       
    int len = 0;
    char* value = NULL;
    /*Consider j_class, and j_methodid are already initialized*/
    jbyteArray j_value = (*env)->CallStaticObjectMethod(env, j_class, j_methodid);

    if(j_value != NULL)
    {
        len = (*env)->GetArrayLength(env, j_value);
        value = (*env)->GetByteArrayElements(env, j_value, NULL);
    }

    /*later on release the resource*/
    (*env)->ReleaseByteArrayElements(env, j_value, value, 0);
}

I have checked it and it works. 我检查了它,它的工作原理。 I am going to check it for 2-D array now. 我现在要检查二维阵列。 I think it'd be the same as this only you'd be getting jobjectArray and every element of this array is a jbyteArray. 我认为只有你得到jobjectArray,并且这个数组的每个元素都是jbyteArray。

Thank you very much! 非常感谢你! I was trying to pass a double array from C to Java and java in turn returns a renewed double array to C. This is part of JNI where I am trying to link a Java code to Fortran source code. 我试图将一个双数组从C传递给Java,而java又将一个更新的double数组返回给C.这是JNI的一部分,我试图将Java代码链接到Fortran源代码。 But Fortran code has to call one more Java code for some calculation. 但Fortran代码必须再调用一个Java代码进行一些计算。 So I am doing Java to C to Fortran to C to Java using JNI. 所以我使用JNI从Java到C语言到Fortran到C到Java。 Solution to sending a double array from C to Java and Java returning a double array to C is here. 将双数组从C发送到Java的解决方案和Java将双数组返回到C的解决方案就在这里。

jdoubleArray tempA = (jdoubleArray)(*envG)->NewDoubleArray(envG,3); //create an array with 3 elements to be sent to Java
jdoubleArray tempB = (jdoubleArray)(*envG)->NewDoubleArray(envG,3); //This is will be //assigned to returned java double array
(*envG)->SetDoubleArrayRegion(envG,tempA,0,3,(const jdouble *)arr);//need to send the //tempA array to Java. "arr" is the double array coming to C from Fortran!
int leng = 0;
for (i = 0; i < 1; i++) {
//sending an array "tempA" to Java. Java returns tempB, a double array
tempB = (*envG)->CallObjectMethod(envG, obj_print, id_print,(*A),(*B),(*C),tempA);
   if (tempB != NULL){
   for (k = 0; k < 3; k++){
      leng = (*envG)->GetArrayLength(envG, tempB);
     jdouble* value = (*envG)->GetDoubleArrayElements(envG, tempB, NULL);
      printf("FROM JAVA ARRAY %f\n", value[k]);
     } //end for
   } //end if

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

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