SWIG c++ -> java 转换byte[] 的关键代码

来源:互联网 发布:windows快速打开程序 编辑:程序博客网 时间:2024/06/03 17:02
/*  
 * 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};
原创粉丝点击