Using GetDirectBufferAddress from JNI(使用 JNI 中的 GetDirectBufferAddress)
问题描述
我试图了解如何从 JNI 层使用 GetDirectBufferAddress
.为了理解,我构建了一个非常简单的示例:
然后从原生 JNI 层:
如果我在这里使用 openjdk 运行这个示例(debian/linux wheezy amd64):
这是我看到的:
我了解索引 2 的值 &3. 但是所有其他值对我来说都没有任何意义,我本来希望是这样的:
我从 JNI 中的 ByteBuffer
用法中误解了什么?
我从文档中遗漏的是,默认情况下 java.nio.ByteBuffer
实际上是使用 BIG_ENDIAN
字节顺序.这解释了我在 LITTLE_ENDIAN
系统上看到的行为.请参阅参考 此处.p>
我的代码现在读作:
似乎默认情况下它始终是 BIG_ENDIAN
,并且到目前为止还没有努力为 LITTLE_ENDIAN
提供 API,如错误报告中所述 这里 (JDK-5043362 : (bf) NewDirectByteBuffer 总是有订单 ByteOrder.BIG_ENDIAN).
最近更新了文档以反映:
- JDK-8225152:发行说明:JNI NewDirectByteBuffer 创建直接缓冲区,即 java.nio.ByteOrder.BIG_ENDIAN
I am trying to understand how to use GetDirectBufferAddress
from the JNI layer. To understand I've build a very simple example:
And then from the native JNI layer:
If I run this example over here (debian/linux wheezy amd64) with openjdk :
Here is what I see:
I understand the values for index 2 & 3. But all other values do not make any sense to me, I would have expected something like:
What did I misunderstood from the ByteBuffer
usage in JNI?
What I missed from the documentation is that by default java.nio.ByteBuffer
is actually using BIG_ENDIAN
byte order. Which explains the behavior I was seeing on my LITTLE_ENDIAN
system. See ref here.
My code now reads as:
It appears that by default it is always BIG_ENDIAN
, and no effort has been made so far to provide an API for LITTLE_ENDIAN
, as explained in the bug report here (JDK-5043362 : (bf) NewDirectByteBuffer always has order ByteOrder.BIG_ENDIAN).
Documentation has been updated recently to reflect that:
- JDK-8225152 : Release Note: JNI NewDirectByteBuffer Creates Direct Buffer That Is java.nio.ByteOrder.BIG_ENDIAN
这篇关于使用 JNI 中的 GetDirectBufferAddress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!