1

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

asked Jan 4, 2012 at 15:58
1
  • 2
    pointers ... do you get it .... Commented Jan 4, 2012 at 16:09

2 Answers 2

4

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.

answered Jan 4, 2012 at 16:00
Sign up to request clarification or add additional context in comments.

4 Comments

That sounds positive, have you an example of it being used which I can refer to?
No, but I have used it that way before. The address you overwrite does cause a memory leak as the ByteBuffer has a Cleaner object which has the original address which is freed up with the ByteBuffer is GCed.
Are you suggesting creating a ByteBuffer in Java and then changing the address in C? Have you come across the NewDirectByteBuffer JNI call?
You could do that, provided you could easily reuse the ByteBuffer. Creating it in Java makes re-use (thus avoiding creating garbage) very simple.
2

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.

answered Jan 4, 2012 at 23:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.