public byte[] construct() throws PacketKitException { byte[][] fragments = new byte[headers.size()][]; // if (body != null) // return body; // construct each component int i = 0; int totallength = 0; for (PacketHeader header : headers) { header.associate(networkinterface); fragments[i] = header.construct(); totallength += fragments[i].length; i++; } // shove the fragments together byte[] body = new byte[totallength]; int index = 0; for (byte[] fragment : fragments) for (byte b : fragment) body[index++] = b; // cache // this.body = body; return body; }
public boolean deconstruct(List<PacketHeader> headers, byte[] buffer, int start, int length) { ByteParser container = new ByteParser(buffer, start); this.headers = headers; for (PacketHeader header : headers) { try { if (!header.deconstruct(container, length)) return false; } catch (PacketKitException e) { return false; } } return true; }