0

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

3
  • Not your question, but...Which character set and encoding does pf_fold_par require? It's unlikely to be modified UTF-8-encoded Unicode. Perhaps you should pass it an array of bytes from String.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. Commented Nov 20, 2013 at 4:20
  • The GetStringUTFChars method 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. Commented Nov 21, 2013 at 0:30
  • You can't have characters without a character set and encoding. A character set maps a concept to a number (codepoint) and an encoding maps a codepoint to bytes. (A typeface [font] maps a character to a glyph, but that's another story.) But you are in luck—Almost all character sets include the "Basic Latin" characters and many encodings encode them to the same single byte. This is by design. But for other characters, modified UTF-8 uses one, two, three or six bytes. Commented Nov 21, 2013 at 5:20

1 Answer 1

2

Do you know how many double pointers are returned? If not, you will first need to calculate that, then:

// export_bppm() returns pointer to double array
double* pr = export_bppm();

//Determine # of doubles @ pr; didn't test this, just guessing at result structure
int count = 0;
double *start = pr;
while( start++ != null )
    count++;

// Processing of double array
jdoubleArray dArray = env->NewDoubleArray( count );

env->SetDoubleArrayRegion( dArray , 0, count, pr );

return dArray;
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately the while-loop runs infinitely. I used the length of the jstring passed to the function to determine the number of elements in the array as they should be the same.
@dennis Like I said, I took a guess on determining the # of elements returned; Does it solve your need by using the length of the jstring as the count?
Technically yes. unfortunately I am not sure if the returned array is correct or not as most of its elements are 0 and I am not sure if the returned array shouldn't be different. But that is of course a different problem
@Samhain is there any way we can know the size of dArray. While loop is not returning anything, but if used some static size, it give the correct output. So if i dont know what is the size of array C method will throw out, is there any way we could this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.