diff --git a/Doc/Manual/Java.html b/Doc/Manual/Java.html index fb8e5d694..5e87e835e 100644 --- a/Doc/Manual/Java.html +++ b/Doc/Manual/Java.html @@ -5490,6 +5490,15 @@ These are listed below: Use for mapping NULL terminated arrays of C strings to Java String arrays + +unsigned char * +NIOBUFFER +various.i +input
output +java.nio.Buffer +Use for mapping directly allocated buffers to c/c++. useful with directors and long lived memory objects + +

25.9.6 Java typemap attributes

diff --git a/Examples/test-suite/java/java_lib_various_runme.java b/Examples/test-suite/java/java_lib_various_runme.java index 203a30ec2..6d9e13e31 100644 --- a/Examples/test-suite/java/java_lib_various_runme.java +++ b/Examples/test-suite/java/java_lib_various_runme.java @@ -83,6 +83,25 @@ public class java_lib_various_runme { if (byjove[i] != b[i]) throw new RuntimeException("By jove, it failed: [" + new String(b) + "]"); } + + // NIOBUFFER typemap check + java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocateDirect(10); + java_lib_various.niobuffer_fill_hello(buf); + if ( + (char)buf.get(0) != 'h' || + (char)buf.get(1) != 'e' || + (char)buf.get(2) != 'l' || + (char)buf.get(3) != 'l' || + (char)buf.get(4) != 'o' + ) + throw new RuntimeException( + "nio test failed: " + + (char)buf.get(0) + + (char)buf.get(1) + + (char)buf.get(2) + + (char)buf.get(3) + + (char)buf.get(4) + ); } } diff --git a/Examples/test-suite/java_lib_various.i b/Examples/test-suite/java_lib_various.i index 716ae9139..180d07569 100644 --- a/Examples/test-suite/java_lib_various.i +++ b/Examples/test-suite/java_lib_various.i @@ -8,6 +8,7 @@ %apply char **STRING_ARRAY { char **languages }; %apply char *BYTE { char *chars }; %apply char **STRING_OUT { char **string_ptr }; +%apply unsigned char *NIOBUFFER { unsigned char *buf }; %typemap(freearg) char **languages "" // don't delete memory when setting global variable %{ @@ -47,5 +48,8 @@ void char_ptr_ptr_out(char **string_ptr) { *string_ptr = ret; } +void niobuffer_fill_hello(unsigned char *buf) { + sprintf ((char*)buf,"hello"); +} %} diff --git a/Lib/java/various.i b/Lib/java/various.i index 7ba7a5eb3..47782586a 100644 --- a/Lib/java/various.i +++ b/Lib/java/various.i @@ -154,3 +154,40 @@ /* 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 array. it is useful for long standing pointers for callbacks + * and wherever performance is critical (and thus memory copy + marshaling is a burdon) + * Note: The Java buffer have to be allocated with allocateDirect. + * + * Example usage wrapping: + * void foo(unsigned char *buf); + * + * Java usage: + * byte b[] = new byte[20]A; + * java.nio.ByteBuffer b = ByteBuffer.allocateDirect(); + * 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 direct buffer. Buffer must be allocated direct."); + } +} +%typemap(memberin) unsigned char *NIOBUFFER { + if ($input) { + $1 = $input; + } else { + $1 = 0; + } +} +%typemap(freearg) unsigned char *NIOBUFFER "" +//define end