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

2023-12-01

我需要从 Java 调用 C 函数。 该函数具有以下 API:

void convert(char* pchInput, int inputSize, int convertValue, char* pchOutput, int* outputSize);

我用大口喝来制作包装纸。

我读了帖子:ByteBuffer.allocate() 与 ByteBuffer.allocateDirect()

似乎最好创造结果(pchOutput) as DirectByteBuffer.

  1. 我怎样才能通过Bytebuffer到代码 c (使用 swig)
  2. C 代码如何从 ByteBuffer 读取和写入数据?

Thanks


稍微修改过的样本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; 
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 swig 定义和传递 ByteBuffer? 的相关文章

随机推荐