@Test public void toByteArrayAndBack() throws Exception { byte[] arrayA = responsePacket.toByteArray(); byte[] arrayB = new ResponsePacket(arrayA).toByteArray(); assertArrayEquals("The two arrays differ!", arrayA, arrayB); }
@Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); dataStream.write(statusCode.ordinal()); if (payload == null) dataStream.writeShort(0); else { byte[] payloadBytes = payload.toByteArray(); dataStream.writeShort(payloadBytes.length); dataStream.write(payloadBytes); } } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteStream.toByteArray(); }
ResponsePacket extends CommunicationPacket { @Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); dataStream.write(statusCode.ordinal()); if (payload == null) dataStream.writeShort(0); else { byte[] payloadBytes = payload.toByteArray(); dataStream.writeShort(payloadBytes.length); dataStream.write(payloadBytes); } } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteStream.toByteArray(); } }
ResponsePacket extends CommunicationPacket { @Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); dataStream.write(statusCode.ordinal()); if (payload == null) dataStream.writeShort(0); else { byte[] payloadBytes = payload.toByteArray(); dataStream.writeShort(payloadBytes.length); dataStream.write(payloadBytes); } } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteStream.toByteArray(); } private  ResponsePacket(DataPacket payload, StatusCode statusCode, UniqueId packetId);  ResponsePacket(byte[] data); }
ResponsePacket extends CommunicationPacket { @Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); dataStream.write(statusCode.ordinal()); if (payload == null) dataStream.writeShort(0); else { byte[] payloadBytes = payload.toByteArray(); dataStream.writeShort(payloadBytes.length); dataStream.write(payloadBytes); } } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteStream.toByteArray(); } private  ResponsePacket(DataPacket payload, StatusCode statusCode, UniqueId packetId);  ResponsePacket(byte[] data); static Collection<ResponsePacket> create(DataPacket payload, StatusCode statusCode, UniqueId packetId); DataPacket getPayload(); StatusCode getStatusCode(); @Override byte[] toByteArray(); @Override String toString(); }
ResponsePacket extends CommunicationPacket { @Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); dataStream.write(statusCode.ordinal()); if (payload == null) dataStream.writeShort(0); else { byte[] payloadBytes = payload.toByteArray(); dataStream.writeShort(payloadBytes.length); dataStream.write(payloadBytes); } } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteStream.toByteArray(); } private  ResponsePacket(DataPacket payload, StatusCode statusCode, UniqueId packetId);  ResponsePacket(byte[] data); static Collection<ResponsePacket> create(DataPacket payload, StatusCode statusCode, UniqueId packetId); DataPacket getPayload(); StatusCode getStatusCode(); @Override byte[] toByteArray(); @Override String toString();  }