それとも、env->NewDirectByteBuffer(buffer, size) を呼び出す JNI ヘルパー関数が必要ですか?
質問する
2839 次
1 に答える
19
私がしているのは、通常の DirectByteBuffer を作成し、そのアドレスを変更することです。
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);
この時点から、基になるアドレスを参照する ByteBuffer にアクセスできます。ゼロコピーのためにネットワークアダプターのメモリにアクセスするためにこれを行いましたが、うまくいきました。
アドレスの DirectByteBuffer を直接作成できますが、これはよりあいまいです。
別の方法として、Unsafe を使用することもできます (これは、OpenJDK/HotSpot JVM およびネイティブ バイト オーダーでのみ機能します)。
Unsafe.getByte(address);
Unsafe.getShort(address);
Unsafe.getInt(address);
Unsafe.getLong(address);
于 2013-05-12T20:46:12.463 に答える