jvm - Is there a way to create a direct ByteBuffer from a pointer solely in Java? -


or have have jni helper function calls env->newdirectbytebuffer(buffer, size)?

what create normal directbytebuffer , change it's address.

field address = buffer.class.getdeclaredfield("address"); address.setaccessible(true); field capacity = buffer.class.getdeclaredfield("capacity"); capacity.setaccessible(true);  bytebuffer bb = bytebuffer.allocatedirect(0).order(byteorder.nativeorder()); address.setlong(bb, addressyouwanttoset); capacity.setint(bb, thesizeof); 

from point can access bytebuffer referencing underlying address. have done accessing memory on network adapters 0 copy , worked fine.

you can create directbytebuffer address directly more obscure.


an alternative use unsafe (this works on openjdk/hotspot jvms , in native byte order)

unsafe.getbyte(address); unsafe.getshort(address); unsafe.getint(address); unsafe.getlong(address); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -