I am trying to design an API to allow my Java app to interface to a proprietary inter-process message passing environment. The messages pre-exist in unmanaged memory. Once I have received a message I want to have a stream to read from and write to the message. While I understand the basics of JNI I am struggling to understand which standard Java Classes can help to create a stream to the unmanaged memory.
I would be grateful for any pointers
Regards
-
2pointers ... do you get it ....Howard May– Howard May2012年01月04日 16:09:54 +00:00Commented Jan 4, 2012 at 16:09
2 Answers 2
I would use a direct ByteBuffer. You can change the address and limit via JNI. Once this is does you can read or change anything in this ByteBuffer and it will change on the "unmanaged" size.
ByteBuffer support little and big endian and read and write of all the primitive types.
A raw way of doing this is to use the Unsafe class. It supports accessing primitives at a random area of memory (just a like a pointer) It also reduces to a single machine code instruction in many cases. Unsafe isn't safe or portable, and if you can use ByteBuffer, its a better choice.
4 Comments
NewDirectByteBuffer JNI call?The JNI API has a method called NewDirectByteBuffer which is declared as:
jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity);
What it does is take a region of memory, described by a void* pointing to the top of it and a length, and creates a ByteBuffer wrapping it. This is not a copy; changes to the buffer will change the data in the region of memory.
ByteBuffer has a pretty rich API. There is no standard way to create an InputStream which reads from or an OutputStream which writes to one, but such things would be very easy to write.