您的位置:首页 > 编程语言 > C语言/C++

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};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swig java c++
相关文章推荐