将本机指针存储在Java对象中的“正确”方法是什么? [英] What is the 'correct' way to store a native pointer inside a Java object?
问题描述
将原生指针存储在Java对象中的正确方法是什么?
What is the 'correct' way to store a native pointer inside a Java object?
我可以将指针视为Java int
,如果我碰巧知道原生指针的大小是< = 32位,或者Java long
,如果我碰巧知道原生指针是< = 64位的大小。但有更好或更清晰的方法吗?
I could treat the pointer as a Java int
, if I happen to know that native pointers are <= 32 bits in size, or a Java long
if I happen to know that native pointers are <= 64 bits in size. But is there a better or cleaner way to do this?
编辑:从JNI函数返回本机指针正是我不要想做。我宁愿返回一个代表本机资源的Java对象。但是,我返回的Java对象大概必须有一个包含指针的字段,这会让我回到原来的问题。
Edit: Returning a native pointer from a JNI function is exactly what I don't want to do. I would rather return a Java object that represents the native resource. However, the Java object that I return must presumably have a field containing a pointer, which brings me back to the original question.
或者,还有更好的方法用于返回对本机资源的引用的JNI函数?
Or, alternatively, is there some better way for a JNI function to return a reference to a native resource?
推荐答案
IIRC, java.util .zip
和 java.nio
只需使用 long
。
IIRC, both java.util.zip
and java.nio
just use long
.
这篇关于将本机指针存储在Java对象中的“正确”方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!