Jnative可以通过getvalueFromPointer方法来获得传出参数,但是如果传出参数是数组,又如何获得呢。首先你想到可能是通过如下方式获得:
- <span style="font-size: small;"> public static void getIntValsFromPointer(int intVals[],Pointer pointer,int count) throws NativeException
- {
- int offset = 0;
- for(int i = 0; i < count; i++)
- {
- pointer.getAsInt(offset);
- offset += 4;
- }
- }</span>
以上的代码是可行的,但是函数getAsInt()会是你的速度非常低下,如果你的速度上要求比
较高的话。那么我们有怎么来提高其速度呢?通过不断的摸索和测试,我给出如下的一段代
码可以较大的提高速度。
- <span style="font-size: small;">public static void getIntValsFromPointer(int intVals[],Pointer pointer,int count) throws NativeException
- {
- int offset = 0;
- byte bytes[] = pointer.getMemory();
- for(int i = 0; i < count; i++)
- {
- intVals[i] = bytesToInt(bytes, offset);
- offset += 4;
- }
- }
-
- public static int bytesToInt(byte[] bytes,int i)
- {
- int intval = bytes[i]&0xff;
- intval |=(bytes[i+1]<<8)&0xff00;
- intval |=(bytes[i+2]<<16)&0xff0000;
- intval |=(bytes[i+3]<<24)&0xff000000;
- return intval;
- }</span>
通过自己一次性获得指针内存,而把内存按字节去转化,效率得到了较大的提高。通过对
比测试,第二种方案比第一种方案在速度上有成千上万倍的提高,而且数组的结构越复杂,
数组越大,效率提高越明显。
(###)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。