lib/java: add core services to work with native buffer in Java and JNI
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 15 Nov 2015 22:08:14 +0000 (17:08 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 16 Nov 2015 22:45:00 +0000 (17:45 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/java/base.nit

index 274533d..10950a2 100644 (file)
@@ -243,3 +243,45 @@ extern class JavaException in "Java" `{ java.lang.Exception `}
                return (*jni_env)->PopLocalFrame(jni_env, self);
        `}
 end
+
+redef class JniEnv
+       # Create new `Java.nio.ByteBuffer` referring to `address` with the given `capacity` in bytes
+       #
+       # JNI function: NewDirectByteBuffer
+       fun new_direct_byte_buffer(address: Pointer, capacity: Int): Java_nio_ByteBuffer `{
+               return (*self)->NewDirectByteBuffer(self, address, capacity);
+       `}
+end
+
+# Container for data of a specific primitive type
+#
+# Java class: java.nio.Buffer
+extern class Java_nio_Buffer in "Java" `{ java.nio.Buffer `}
+       super JavaObject
+
+       # Address pointed by this buffer
+       #
+       # JNI function: GetDirectBufferAddress
+       fun direct_buffer_address(jni_env: JniEnv): Pointer `{
+               return (*jni_env)->GetDirectBufferAddress(jni_env, self);
+       `}
+
+       # Capacity of this this buffer
+       #
+       # JNI function: GetDirectBufferCapacity
+       fun direct_buffer_capacity(jni_env: JniEnv): Int `{
+               return (*jni_env)->GetDirectBufferCapacity(jni_env, self);
+       `}
+end
+
+# A byte buffer
+#
+# Java class: java.nio.ByteBuffer
+extern class Java_nio_ByteBuffer in "Java" `{ java.nio.ByteBuffer `}
+       super Java_nio_Buffer
+
+       # Allocate a new `java.nio.ByteBuffer` with `allocateDirect`
+       new direct(size: Int) in "Java" `{
+               return java.nio.ByteBuffer.allocateDirect((int)size);
+       `}
+end