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
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.