I am trying to use an external C library in a Java program via JNI. I need to use two functions, the first is a calculation in C and stores some results in a double array, the second function then returns the pointer to this double array.
My questions is now how to access/use this double array in Java.
JNI Code:
JNIEXPORT jdoubleArray JNICALL Java_rna_jni_ViennaRNAInterface_getBasePairingProbabilities(JNIEnv *env, jobject thisObj, jstring sequence){
jdoubleArray dArray;
const char *seq= (*env)->GetStringUTFChars(env,sequence,0);
// Calculates a single value, stores important results in double array
pf_fold_par(seq,NULL,NULL,1,0,0);
// export_bppm() returns pointer to double array
double* pr = export_bppm();
// Processing of double array
return dArray;
}
The Code in the C library looks essentially like this,
FLT_OR_DBL is defined as either Float or Double:
PRIVATE FLT_OR_DBL *probs=NULL;
PRIVATE void get_arrays(unsigned int length){
size = sizeof(FLT_OR_DBL) * ((length+1)*(length+2)/2);
probs = (FLT_OR_DBL *) space(size);
}
PUBLIC float pf_fold_par(const char *sequence,char *structure, pf_paramT *parameters,int calculate_bppm,int is_constrained, int is_circular){
float result;
int n = (int) strlen(sequence);
get_arrays(n);
/*
* Calculating result
*/
// Fill double array
pf_create_bppm(sequence, structure);
pr = probs;
return result;
}
If necessary I can provide the exact implementation in the library as well.
\edit: Exact Implementation at http://pastebin.com/ayGg2HBR
pf_fold_parrequire? It's unlikely to be modified UTF-8-encoded Unicode. Perhaps you should pass it an array of bytes fromString.getBytes(Charset)or, for the Java-determined system default Charset,String.getBytes(). It does look like it needs to be 0-terminated, though so be sure to account for that.GetStringUTFCharsmethod works fine, I use a different function in the library as well which handles the encoding without any problem. I am not too familiar with encoding but it might help that the String can only contain A,C,G or U anyway.