我的文件的相关部分是这样的:
82 0a 96 c9 82 0a 96 d3 00 66 13 08
我在映射字节缓冲区中打开文件并将位置设置为开头。然后我这样做:
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
...
buffer.position(packetInfos.get(idPacket).getStartPos());
//getting the time from the packet header
time = Math.addExact(Math.multiplyExact((long) buffer.order(ByteOrder.LITTLE_ENDIAN).getInt(), 1000), Math.floorDiv(buffer.order(ByteOrder.LITTLE_ENDIAN).getInt(), 1000));
//getting the source ip from the ip frame
buffer.position(packetInfos.get(idPacket).getStartPos() + PACKET_IPSOURCE_OFFS); // puts the buffers position at the part of the file shown above
source = byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get());
//getting the destination ip from the ip frame
destination = byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get());
byteToUnsigned 方法只是做:
public static int byteToUnsigned(byte b){
return b & 0xFF;
}
源最终是:“130.10.150.211”,而它应该是“130.10.150.201”。由于某种原因,get() 方法在大多数情况下将缓冲区的位置增加 1,但在第三次之后增加 5?正如您可能已经猜到的那样,我试图在之后解码目标 ip,它在“D3”之后开始读取,导致“0.102.19.8”
即使在 byteToUnsigned 调用之前,源 Ip 也是“-126.10.-106.-45”。
通过这一行一步步调试后:
source = byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get()) + "." + byteToUnsigned(buffer.get());
观察 buffer.position() 和 buffer.get(),我可以看到以下内容:
- 第一个get():buffer.position()=70,buffer.get()=-126
- 第二个get():buffer.position()=71,buffer.get()=10
- 第三个get():buffer.position()=72,buffer.get()=-106
- 第四个 get(): buffer.position()= 73 , buffer.get()=-45
所以位置是正确递增的,但是第 72 和第 77 之间的字节不知何故对缓冲区不可见?
Api 明确指出:
public abstract byte get()
Relative get method. Reads the byte at this buffer's current position, and then increments the position.
我错过了什么?