Getting byte array of unknown length from Java storage

I already posted a question about this, but at the time I do not have an account. I got an answer, but I was still confused, and I can not continue this topic.

I am sending the question again with a link to the previous conversation.

Returning char array from java to string - JNI

Data stored in Java is serialized. I am making a java function call using the following code snippet.

The following code assumes that char of C is compatible with the Java byte, because char Java has 2 bytes, while char of C has 1 byte. Jbyte is also a signed char *

    //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);

}

, . , . API C, Java. C- Java. , Java .

C Java, C . , . ( java), GetByteArrayElements. , , , , NewByteArray, Java-.

+5
2

, . , .

java-, ( , ..)

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

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);
}

, . 2- . , , jobjectArray, jbyteArray.

+12

! C Java, java C. JNI, Java Fortran. Fortran Java. Java C Fortran C Java JNI. 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
+1

All Articles