[英]Passing byte[] from Java to C++
I'm trying to pass a byte[] param with binary data and a string param from Java to C++ code using SWIG . 我正在尝试使用SWIG将带有二进制数据和字符串参数的byte []参数从Java传递到C ++代码。
Here are my .i and .cpp files: 这是我的.i和.cpp文件:
%module mymodule
%inline %{
extern void compress_buffer_to_file(unsigned char *buffer, char *ofname);
%}
my_module.cpp my_module.cpp
void compress_buffer_to_file(unsigned char *buffer, char *ofname){
.........
}
The generated method in the Java wrapper: Java包装器中生成的方法:
public static void compress_buffer_to_file(SWIGTYPE_p_unsigned_char buffer, String ofname) {
my_moduleJNI.compress_buffer_to_file(SWIGTYPE_p_unsigned_char.getCPtr(buffer), ofname);
}
How can I define a .i file that will generate a Java wrapper that will let me pass byte[] instead of SWIGTYPE_p_unsigned_char to the compress_buffer_to_file method or alternatively? 如何定义一个.i文件,它将生成一个Java包装器,让我将byte []而不是SWIGTYPE_p_unsigned_char传递给compress_buffer_to_file方法,或者? How do I associate an existing byte[] on the Java side with a SWIGTYPE_p_unsigned_char?
如何将Java端的现有byte []与SWIGTYPE_p_unsigned_char相关联?
I've tried to use typemap with no success. 我试过使用typemap但没有成功。
You want SWIG to perform a conversion between Java and C++ types, namely 您希望SWIG在Java和C ++类型之间执行转换,即
byte[]
to unsigned char *
and byte[]
到unsigned char *
和 String
to char *
. String
到char *
。 The general tools for such conversions are SWIG typemaps . 此类转换的常规工具是SWIG类型地图 。 Conveniently, many are already provided by SWIG and simply need to be applied .
方便的是,许多已经由SWIG提供,只需要应用 。 Have a look at the Java typemap documentation .
看一下Java typemap文档 。
The latter conversion ( String
to char *
) is done automatically by SWIG (using predefined typemaps that match char *
arguments). 后一种转换(
String
to char *
)由SWIG自动完成(使用与char *
参数匹配的预定义类型映射)。
A standard conversion very similar to what you want ( byte[]
to char *
) is handled by the " char * BYTE
" typemaps defined in the file various.i
. 一个标准的转换非常相似,你想要的(东西
byte[]
到char *
)由“处理char * BYTE
”的文件中定义typemaps various.i
。 To use it, all you need to do is add 要使用它,您需要做的就是添加
%include various.i
%apply char *BYTE { char *buffer_variable_name };
at the top of the swig interface file, where buffer_variable_name
is the name of the variable in the function argument (typemaps can be matched by name). 在swig接口文件的顶部,其中
buffer_variable_name
是函数参数中变量的名称 (类型映射可以按名称匹配)。 For details, see the general typemap documentation and the Java typemap documentation . 有关详细信息,请参阅常规类型映射文档和Java类型映射文档 。
However, this is not exactly what you want, since your function takes an unsigned char *
. 但是,这并不是您想要的,因为您的函数采用
unsigned char *
。 If you can use java.nio.Buffer
instead of byte[]
on the Java side (which needs to be allocated via allocateDirect
), there is a different set of typemaps predefined in various.i
that can be used via 如果可以使用
java.nio.Buffer
而不是byte[]
在Java端(其需要通过被分配allocateDirect
),有在预定义的一组不同的typemaps various.i
可通过使用
%apply unsigned char *NIOBUFFER { unsigned char *buffer_variable_name };
In your case you want essentially the same as the provided " char * BYTE
" typemaps, but for functions taking unsigned char *
. 在您的情况下,您希望与提供的“
char * BYTE
”类型映射基本相同,但对于使用unsigned char *
函数。 So you can just copy these from various.i
to a new file ubyte.i
and tweak them slightly: 所以,你可以从刚刚复制这些
various.i
到一个新的文件ubyte.i
稍微调整它们:
ubyte.i ubyte.i
%typemap(jni) unsigned char *UBYTE "jbyteArray"
%typemap(jtype) unsigned char *UBYTE "byte[]"
%typemap(jstype) unsigned char *UBYTE "byte[]"
%typemap(in) unsigned char *UBYTE {
$1 = (unsigned char *) JCALL2(GetByteArrayElements, jenv, $input, 0);
}
%typemap(argout) unsigned char *UBYTE {
JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0);
}
%typemap(javain) unsigned char *UBYTE "$javainput"
/* Prevent default freearg typemap from being used */
%typemap(freearg) unsigned char *UBYTE ""
Use these in the same way, by adding at the top of your swig interface file: 通过在swig界面文件的顶部添加,以相同的方式使用它们:
%include ubyte.i
%apply unsigned char *UBYTE { unsigned char *buffer_variable_name };
Using these predefined typemaps as a basic example, and reading the docs, you can then start writing your own custom typemaps if you need to. 使用这些预定义的文字图作为基本示例,并阅读文档,然后您可以根据需要开始编写自己的自定义文字图。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.