Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define and pass ByteBuffer using swig?

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.

  1. How can I pass the Bytebuffer to the code c (using swig)
  2. How the c code will read and write the data from the ByteBuffer ?

Thanks

like image 214
user3668129 Avatar asked Aug 30 '15 08:08

user3668129


1 Answers

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; 
} 
like image 104
V-master Avatar answered Oct 23 '22 07:10

V-master