I need to call to C function from Java. The function has the following API:
void convert(char* pchInput, int inputSize, int convertValue, char* pchOutput, int* outputSize);
I'm using swig in order to make the wrappers.
I read the post: ByteBuffer.allocate() vs. ByteBuffer.allocateDirect()
And it seems best to create the result (pchOutput)
as DirectByteBuffer
.
Bytebuffer
to the code c (using swig) Thanks
a little modified sample from http://swig.10945.n7.nabble.com/Re-How-to-specify-access-to-a-java-nio-ByteBuffer-td6696.html that should work after small changes (especially %typemap(in) (char* pchOutput, int* outputSize))
as I did not compiled this, just run swig to check if java side is properly generated.
%typemap(in) (char* pchInput, int inputSize) {
$1 = JCALL1(GetDirectBufferAddress, jenv, $input);
$2 = (int)JCALL1(GetDirectBufferCapacity, jenv, $input);
}
%typemap(in) (char* pchOutput, int* outputSize) {
$1 = JCALL1(GetDirectBufferAddress, jenv, $input);
$2 = &((int)JCALL1(GetDirectBufferCapacity, jenv, $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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With