Java 在字节[]和整数[]上运行,内存相同
我希望加快我的程序。目前,我有一个函数可以执行以下操作:Java 在字节[]和整数[]上运行,内存相同,java,android,performance,Java,Android,Performance,我希望加快我的程序。目前,我有一个函数可以执行以下操作: public void updateBitmap(byte[] buf, int thisPacketLength, int standardOffset, int thisPacketOffset) { int pixelCoord = thisPacketOffset / 3 - 1; for (int bufCoord = standardOffset; bufCoord < thisPacketL
public void updateBitmap(byte[] buf, int thisPacketLength, int standardOffset, int thisPacketOffset) {
int pixelCoord = thisPacketOffset / 3 - 1;
for (int bufCoord = standardOffset; bufCoord < thisPacketLength; bufCoord += 3) {
pixelCoord++;
pixelData[pixelCoord] = 0xFF << 24 | (buf[bufCoord + 2] << 16) & 0xFFFFFF | (buf[bufCoord + 1] << 8) & 0xFFFF | buf[bufCoord] & 0xFF;
}
}
public void updateBitmap(字节[]buf,int thisPacketLength,int standardcoffset,int thisPacketOffset){
int-pixelCoord=thisPacketOffset/3-1;
对于(int-bufCoord=standardOffset;bufCoord pixelData[pixelCoord]=0xFF您应该改用字节缓冲区。然后您可以将其作为int缓冲区访问,并逐字节或逐int读取/写入字节
使用以下内容创建它:
ByteBuffer bb = ByteBuffer.wrap(buf);
并获取IntBuffer:
IntBuffer ib = bb.asIntBuffer();
您可以通过写入索引来设置此缓冲区中的值:
ib.put(2, 400);
WARE 2是索引,400是值。对ib
的任何更改都将得到bb
和buf
的支持。请不要忘记,这样做可能会导致端性问题。幸运的是,ByteBuffer
允许选择ByteOrder
@maaartinus。我从来不知道这一点。谢谢通知我们!不幸的是在我的android设备上,我在int[]test=ib.array()上得到java.lang.UnsupportedOperationException。我需要有一个整数数组本身的引用,而不仅仅是能够使用IntBuffer修改它。有什么建议吗?@Trevor你可以将它逐int读取到一个相同大小的数组中。是的,我可以这样做,但这会破坏我所做的目的。我想让int数组和字节数组指向相同的内存。那么做byte[0]=…将对int[0]产生影响,反之亦然