Copying a byte buffer with JNI

前端 未结 2 561
不思量自难忘°
不思量自难忘° 2021-02-05 09:50

I\'ve found plenty of tutorials / questions on Stackoverflow that deal with copying char arrays from C/JNI side into something like a byte[] in Java, but not the other way aroun

2条回答
  •  情深已故
    2021-02-05 10:17

    Here's a working example I just lifted from my AS/400 JNI library to resolve a native user-queue pointer to test the queue's existence - it copies the queue library and name from a Java byte array (already translated to job's CCSID) into native code and uses it. Take note of the release function calls; these can be changed to copy the native array contents back into the Java byte arrays to move data the other way:

    JNIEXPORT jboolean JNICALL Java_com_mycompany_jni400_UserQueue_jniResolve(JNIEnv *jep,jobject thsObj,                
    jbyteArray queueLibrary,jbyteArray queueName) {                                                                             
        jbyte            *lib,*nam;                                                                                             
        bool             rtn;                                                                                                   
    
        thsObj=thsObj;                                                                                                          
        lib=(*jep)->GetByteArrayElements(jep,queueLibrary,0);                                                                   
        nam=(*jep)->GetByteArrayElements(jep,queueName,0);                                                                      
        rtn=(usrq_resolve((byte*)lib,(byte*)nam)!=NULL);                                                                        
        (*jep)->ReleaseByteArrayElements(jep,queueLibrary,lib,JNI_ABORT); /* abort to not copy back contents */                 
        (*jep)->ReleaseByteArrayElements(jep,queueName   ,nam,JNI_ABORT); /* abort to not copy back contents */                 
        if(rtn) { return JNI_TRUE;  }                                                                                           
        else    { return JNI_FALSE; }                                                                                           
        }                                                                                                                       
    

提交回复
热议问题