我正在将数组解析为多个较小的数组,但返回到main的结果是原始数组中的最后一个对象。我理解为什么会这样,但我不知道如何解决它。
这是我的主类调用我的类parseUDP来解析数组:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MarketDataReader {
public static void main(String[] args) {
try {
byte[] i = null;
//byte[] decodedArray = null;
String readableMessage = "Null Null";
ipaddrConnection ip = new ipaddrConnection();
parseUDP p = new parseUDP();
DecodeMessage message = new DecodeMessage();
while(1==1){
i = ip.getPacket();
byte[] decodedArray = p.parseUDP(i);
//decodedArray = p.
StringBuilder sb = new StringBuilder();
for (byte b : decodedArray)
{
sb.append(String.format("%02X ", b));
}
System.out.println("new: " + sb.toString());
//readableMessage = message.decode(decodedArray);
//System.out.println(readableMessage);
}
} catch (IOException ex) {
Logger.getLogger(MarketDataReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是我的类将原始Array解析为更小的数组:
public class parseUDP {
public byte[] parseUDP(byte[] i) {
final ByteBuffer buffer = ByteBuffer.wrap(i);
byte[] newArr = null;
byte[] headerArr = new byte[12];
byte[] head;
byte[] completeArray= null;
head = Arrays.copyOf(i, 12);
buffer.get(headerArr);
while (buffer.hasRemaining()) {
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.mark();
int length = buffer.getShort() & 0xffff;
if (length == 0) {
break;
}
//System.out.println(length);
newArr = new byte[length];
buffer.reset();
buffer.get(newArr, 0, length);
completeArray = new byte[head.length + newArr.length];
for(int x = 0; x < head.length; x++)
{
completeArray[x] = head[x];
}
for(int x = head.length, j = 0; j < newArr.length; x++, j++)
{
completeArray[x] = newArr[j];
}
return completeArray;
}
return completeArray;
}
}