I would use direct ByteBuffer. You can change the address and limit through JNI. Once you do this, you can read or change anything in this ByteBuffer, and it will change to an โunmanagedโ size.
ByteBuffer supports small and large endian and reads and writes all primitive types.
An unusual way to do this is to use the Unsafe class. It supports access to primitives in an arbitrary memory area (just like a pointer). In many cases, it also boils down to one machine code instruction. Unsafe is not safe or portable, and if you can use ByteBuffer, this is the best choice.
Peter Lawrey
source share