}
int length = packet.getHeader().length();
int prelength = length;
int pos = 0;
Packet last = null;
for (Packet p: packet.getPayload()) {
if (p.getHeader() != null) {
prelength = length;
length += p.getHeader().length();
pos++;
if (length > size) {
last = p;
break;
}
}
else {
prelength = length;
length += p.length();
pos++;
last = p;
break;
}
}
Packet.Builder resultBuilder = packet.getBuilder();
for (Packet.Builder b: resultBuilder) {
if (b instanceof LengthBuilder) {
((LengthBuilder<?>)b).correctLengthAtBuild(false);
}
if (b instanceof ChecksumBuilder) {
((ChecksumBuilder<?>)b).correctChecksumAtBuild(false);
}
pos--;
if (pos == 0) {
if (size - prelength > 0) {
b.payloadBuilder(
new UnknownPacket.Builder()
.rawData(
ByteArrays.getSubArray(last.getRawData(), 0, size - prelength)
)
);
}
else {
b.payloadBuilder(null);