5

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

Community
  • 1
  • 1
user3668129
  • 4,318
  • 6
  • 45
  • 87

1 Answers1

6

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; 
} 
Mark
  • 5,994
  • 5
  • 42
  • 55
V-master
  • 1,957
  • 15
  • 18