0

I am trying to use a C library (Pre-written and published) in an android app. I started my effort about writing a JNI wrapper around this C library. I can't figure out how to pass in jchararray properly to C.

int getData(char data[], const int dataLen, int unit[], const int unitLen) {
...
}

My JNI wrapper looks like

Java_eg_test_freqProcessor(JNIEnv *env, jclass type, jcharArray data_,
    jintArray unit_) {

    jchar *data = (*env)->GetCharArrayElements(env, data_, NULL);
    jsize dataLen = (*env)->GetArrayLength(env, data_);
    jint *unit = (*env)->GetIntArrayElements(env, unit_, NULL);
    jsize unitLen = (*env)->GetArrayLength(env, unit_);

    getData(data, dataLen, unit, unitLen);

    (*env)->ReleaseCharArrayElements(env, data_, data, 0);
    (*env)->ReleaseIntArrayElements(env, unit_, unit, 0);
}

I have verified that int[] are passing through to C library well by printing them inside getData() method. But the problem is with passing jcharArray data_ to char data[]

I have printed out the data_ after getting the length using (*env)->GetArrayLength(env, data_);. When I print inside Java_eg_test_freqProcessor method, I see

06-29 23:05:42.364 12278 12348 V EEG    : {
06-29 23:05:42.364 12278 12348 V EEG    : "
06-29 23:05:42.364 12278 12348 V EEG    : 0
06-29 23:05:42.364 12278 12348 V EEG    : "
06-29 23:05:42.364 12278 12348 V EEG    : :
06-29 23:05:42.364 12278 12348 V EEG    :  
06-29 23:05:42.364 12278 12348 V EEG    : 1
06-29 23:05:42.364 12278 12348 V EEG    : }

But inside the getData() method, if I iterate on the same set I only see half of the data.

06-29 23:08:07.801 12573 12651 V EEG Inside: 0 : {:
06-29 23:08:07.802 12573 12651 V EEG Inside: 1 :
06-29 23:08:07.802 12573 12651 V EEG Inside: 2 : ":
06-29 23:08:07.802 12573 12651 V EEG Inside: 3 :
06-29 23:08:07.802 12573 12651 V EEG Inside: 4 : 0:
06-29 23:08:07.802 12573 12651 V EEG Inside: 5 :
06-29 23:08:07.802 12573 12651 V EEG Inside: 6 : ":
06-29 23:08:07.802 12573 12651 V EEG Inside: 7 :

This library performs in place transformation of data[] which I need to access later. I understand that jchar is a byte larger than char but I still can't understand the correct way of approaching this.

Thanks

Ashok Bommisetti
  • 314
  • 1
  • 3
  • 15

1 Answers1

0

jchar is not char but rather uint16_t (or unsigned short). Java strings seem to have UTF-16 encoding.

Take a look at the accepted answer for this question

tgregory
  • 554
  • 3
  • 9