SWIG c++ -> java 转换byte[] 的关键代码
2017-12-12 17:23
316 查看
/*
* char *BYTE typemaps.
* These are input typemaps for mapping a Java byte[] array to a C char array.
* Note that as a Java array is used and thus passeed by reference, the C routine
* can return data to Java via the parameter.
*
* Example usage wrapping:
* void foo(char *array);
*
* Java usage:
* byte b[] = new byte[20];
* modulename.foo(b);
*/
%typemap(jni) char *BYTE "jbyteArray"
%typemap(jtype) char *BYTE "byte[]"
%typemap(jstype) char *BYTE "byte[]"
%typemap(in) char *BYTE {
$1 = (char *) JCALL2(GetByteArrayElements, jenv, $input, 0);
}
%typemap(argout) char *BYTE {
JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0);
}
%typemap(javain) char *BYTE "$javainput"
/* Prevent default freearg typemap from being used */
%typemap(freearg) char *BYTE ""
/*
* unsigned char *NIOBUFFER typemaps.
* This is for mapping Java nio buffers to C char arrays.
* It is useful for performance critical code as it reduces the memory copy an marshaling overhead.
* Note: The Java buffer has to be allocated with allocateDirect.
*
* Example usage wrapping:
* %apply unsigned char *NIOBUFFER { unsigned char *buf };
* void foo(unsigned char *buf);
*
* Java usage:
* java.nio.ByteBuffer b = ByteBuffer.allocateDirect(20);
* modulename.foo(b);
*/
%typemap(jni) unsigned char *NIOBUFFER "jobject"
%typemap(jtype) unsigned char *NIOBUFFER "java.nio.ByteBuffer"
%typemap(jstype) unsigned char *NIOBUFFER "java.nio.ByteBuffer"
%typemap(javain,
pre=" assert $javainput.isDirect() : \"Buffer must be allocated direct.\";") unsigned char *NIOBUFFER "$javainput"
%typemap(javaout) unsigned char *NIOBUFFER {
return $jnicall;
}
%typemap(in) unsigned char *NIOBUFFER {
$1 = (unsigned char *) JCALL1(GetDirectBufferAddress, jenv, $input);
if ($1 == NULL) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unable to get address of a java.nio.ByteBuffer direct byte buffer. Buffer must be a direct buffer and not a non-direct buffer.");
}
}
%typemap(memberin) unsigned char *NIOBUFFER {
if ($input) {
$1 = $input;
} else {
$1 = 0;
}
}
%typemap(freearg) unsigned char *NIOBUFFER ""
%apply char *BYTE { char *buf };
%apply char *BYTE { char *data };
%apply unsigned char *NIOBUFFER { unsigned char *data};
* char *BYTE typemaps.
* These are input typemaps for mapping a Java byte[] array to a C char array.
* Note that as a Java array is used and thus passeed by reference, the C routine
* can return data to Java via the parameter.
*
* Example usage wrapping:
* void foo(char *array);
*
* Java usage:
* byte b[] = new byte[20];
* modulename.foo(b);
*/
%typemap(jni) char *BYTE "jbyteArray"
%typemap(jtype) char *BYTE "byte[]"
%typemap(jstype) char *BYTE "byte[]"
%typemap(in) char *BYTE {
$1 = (char *) JCALL2(GetByteArrayElements, jenv, $input, 0);
}
%typemap(argout) char *BYTE {
JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0);
}
%typemap(javain) char *BYTE "$javainput"
/* Prevent default freearg typemap from being used */
%typemap(freearg) char *BYTE ""
/*
* unsigned char *NIOBUFFER typemaps.
* This is for mapping Java nio buffers to C char arrays.
* It is useful for performance critical code as it reduces the memory copy an marshaling overhead.
* Note: The Java buffer has to be allocated with allocateDirect.
*
* Example usage wrapping:
* %apply unsigned char *NIOBUFFER { unsigned char *buf };
* void foo(unsigned char *buf);
*
* Java usage:
* java.nio.ByteBuffer b = ByteBuffer.allocateDirect(20);
* modulename.foo(b);
*/
%typemap(jni) unsigned char *NIOBUFFER "jobject"
%typemap(jtype) unsigned char *NIOBUFFER "java.nio.ByteBuffer"
%typemap(jstype) unsigned char *NIOBUFFER "java.nio.ByteBuffer"
%typemap(javain,
pre=" assert $javainput.isDirect() : \"Buffer must be allocated direct.\";") unsigned char *NIOBUFFER "$javainput"
%typemap(javaout) unsigned char *NIOBUFFER {
return $jnicall;
}
%typemap(in) unsigned char *NIOBUFFER {
$1 = (unsigned char *) JCALL1(GetDirectBufferAddress, jenv, $input);
if ($1 == NULL) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unable to get address of a java.nio.ByteBuffer direct byte buffer. Buffer must be a direct buffer and not a non-direct buffer.");
}
}
%typemap(memberin) unsigned char *NIOBUFFER {
if ($input) {
$1 = $input;
} else {
$1 = 0;
}
}
%typemap(freearg) unsigned char *NIOBUFFER ""
%apply char *BYTE { char *buf };
%apply char *BYTE { char *data };
%apply unsigned char *NIOBUFFER { unsigned char *data};
相关文章推荐
- java/android 使用swig编译c/c++ 代码类型转换
- 问题1:java中没有实现这种“byte a = 0xB2 --> String b = “B2””转换的简单实现需要自己实现。 答:自己编写的转换函数,思路将byte的高低4位分开,分别转换为对应的字符然后合成返回的字符串。 java 代码 1.
- 我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具
- 我刚翻译的一些用Swig将c/c++程序转为java代码的英文文档
- 【转】Javabyte[]数组和十六进制String之间的转换Util------包含案例和代码
- 字节数组byte[]和整型,浮点型数据的转换——Java代码
- Javabyte[]数组和十六进制String之间的转换Util------包含案例和代码
- 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map
- Java中List<Byte>转换为byte[]方法比较
- android jni开发 把一段java代码转换成c++方式调用
- linux SWIG C++转换为JAVA
- <java代码> 实现Unix时间戳(Unix timestamp)与普通时间 之间的相互转换
- JAVA byte[], int, long三者之间的相互转换代码
- java 将byte中的有效长度转换为String的实例代码
- java中utf-8编码的byte数组转换成String类型代码
- openoffice中一个将java代码转换为c++代码的例子
- 用Swig将c/c++程序转为java代码(使用swig实现java调用c、c++的方法)
- 利用 SWIG 转换 C/C++ 接口到 Java 接口
- 使用SWIG将C++接口转换成Java接口
- java中utf-8编码的byte数组转换成String类型代码