我想從字節數組中提取特定位和字節。該字節數組使用文件輸入流填充,並且在特定長度的流中包含連續的重複數據分組格式,例如,首部的時間戳的數據類型 - 數據CRC。我需要填充數據包的列表,並能夠從中提取數據。使用java中的字節數組填充數據流中的數據
Packet()
{
byte header; // 1 BYTE: split into bit flags, and extracted using masks
int timestamp; // 4 BYTES
int dataType; // 1 BYTE
string data; // 10 BYTES
int crc; // 1 BYTE
}
static final int PACKET_SIZE 17 // BYTES
Packet[] packets;
byte[] input = new byte[(int)file.length()];
InputStream is = new BufferedInputStream(new FileInputStream(file));
int totalBytesRead = 0;
int totalPacketsRead = 0;
while(totalBytesRead < input.length)
{
int bytesRemaining = input.length - totalBytesRead;
int bytesRead = input.read(result, totalBytesRead, PACKET_SIZE);
totalBytesRead = totalBytesRead + bytesRead;
packet aPacket;
// How to populate a single packet in each iteration ???
...
packets[totalPacketsRead] = aPacket;
totalPacketsRead++;
}
,你的問題是什麼? –
包a包; //如何在每次迭代中填充單個數據包? ... – Bob