How to define and pass ByteBuffer using swig?
I need to call a C function from Java. The function has the following API:
void convert(char* pchInput, int inputSize, int convertValue, char* pchOutput, int* outputSize);
I am using swig to create wrappers.
I read the post: ByteBuffer.allocate () and ByteBuffer.allocateDirect ()
And the best thing is to create the result (pchOutput)
as DirectByteBuffer
.
- How to pass
Bytebuffer
in c code (using swig) - How will c code read and write data from ByteBuffer?
thank
+2
source to share
1 answer
slightly modified sample from http://swig.10945.n7.nabble.com/Re-How-to-specify-access-to-a-java-nio-ByteBuffer-td6696.html which should work after minor changes (especially % typemap (in) (char * pchOutput, int * outputSize)) since I didn't compile this, just run swig to check if the java side is created correctly.
%typemap(in) (char* pchInput, int inputSize) {
$1 = jenv->GetDirectBufferAddress($input);
$2 = (int)(jenv->GetDirectBufferCapacity($input));
}
%typemap(in) (char* pchOutput, int* outputSize) {
$1 = jenv->GetDirectBufferAddress($input);
$2 = &((int)(jenv->GetDirectBufferCapacity($input)));
}
/* These 3 typemaps tell SWIG what JNI and Java types to use */
%typemap(jni) (char* pchInput, int inputSize), (char* pchOutput, int* outputSize) "jobject"
%typemap(jtype) (char* pchInput, int inputSize), (char* pchOutput, int* outputSize) "java.nio.ByteBuffer"
%typemap(jstype) (char* pchInput, int inputSize), (char* pchOutput, int* outputSize) "java.nio.ByteBuffer"
%typemap(javain) (char* pchInput, int inputSize), (char* pchOutput, int* outputSize) "$javainput"
%typemap(javaout) (char* pchInput, int inputSize), (char* pchOutput, int* outputSize) {
return $jnicall;
}
+3
source to share