如何使用swig定义和传递ByteBuffer?

5
我需要从Java调用C函数。 该函数具有以下API:
void convert(char* pchInput, int inputSize, int convertValue, char* pchOutput, int* outputSize);

我正在使用swig来制作包装器。
我阅读了这篇帖子: ByteBuffer.allocate() vs. ByteBuffer.allocateDirect() 看起来最好将结果(pchOutput)创建为DirectByteBuffer
1. 我如何通过swig将Bytebuffer传递给c代码? 2. c代码如何从ByteBuffer中读取和写入数据?
谢谢。
1个回答

6
这是一个修改后的示例,来源于http://swig.10945.n7.nabble.com/Re-How-to-specify-access-to-a-java-nio-ByteBuffer-td6696.html,需要进行小幅更改才能正常运行(特别是%typemap(in) (char* pchOutput, int* outputSize)))。我没有编译它,只是运行了一下 SWIG 来检查 Java 端是否生成正确。
%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; 
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接