通过JNI获取Java ByteBuffer的指针
如何获取指向 Java ByteBuffer 内部数组的指针?
JNIEXPORT void JNICALL test(JNIEnv *env, jobject thiso) {
jclass cls = env->FindClass("java/nio/ByteBuffer");
jmethodID aloc = env->GetStaticMethodID(cls, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
jobject obj = env->CallStaticObjectMethod(cls, aloc, 1000);
}
PS:我这样做是为了共享 Java 和 C++ 使用的内存。
How can I get a pointer to the inner array of a Java ByteBuffer?
JNIEXPORT void JNICALL test(JNIEnv *env, jobject thiso) {
jclass cls = env->FindClass("java/nio/ByteBuffer");
jmethodID aloc = env->GetStaticMethodID(cls, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
jobject obj = env->CallStaticObjectMethod(cls, aloc, 1000);
}
PS: I'm doing that to share the memory used by Java and C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ByteBuffer 必须是直接的才能正常工作。
The ByteBuffer must be a direct one for this to work.
如果您尝试返回
m_buffer
中第一个元素的地址,那么您可以这样做:return m_buffer;
..或者:
return &m_buffer [0]
If you're trying to return the address of the first element within
m_buffer
, then you can just do:return m_buffer;
..or:
return &m_buffer[0]