SPUD extends BasePacket { @Override public IPacket deserialize(byte[] data, int offset, int length) throws PacketParsingException { ByteBuffer bb = ByteBuffer.wrap(data, offset, length); byte[] magicConstant = new byte[MAGIC_CONSTANT.length]; bb.get(magicConstant, 0, MAGIC_CONSTANT.length); if (!Arrays.equals(magicConstant, MAGIC_CONSTANT)) { throw new PacketParsingException("Magic constant is incorrect."); } tubeID = bb.getLong(); byte lastByte = bb.get(); command = (byte) ((lastByte & 0xc0) >>> 6); adec = ((lastByte & 0x20) != 0); pdec = ((lastByte & 0x10) != 0); reserved = (byte) (lastByte & 0xF); this.payload = new Data(); this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position()); this.payload.setParent(this); return this; }  long getTubeID(); SPUD setTubeID(long tubeID); byte getCommand(); SPUD setCommand(byte command); boolean getADEC(); SPUD setADEC(boolean adec); boolean getPDEC(); SPUD setPDEC(boolean pdec); byte getReserved(); SPUD setReserved(byte reserved); @Override byte[] serialize(); @Override IPacket deserialize(byte[] data, int offset, int length); @Override int hashCode(); @Override boolean equals(Object obj); static final byte[] MAGIC_CONSTANT; static final int HEADER_LENGTH; static final byte COMMAND_DATA; static final byte COMMAND_OPEN; static final byte COMMAND_CLOSE; static final byte COMMAND_ACK; }
@Test public void testDeserialize() throws PacketParsingException { byte[] spudPacket = { (byte) 0xd8, 0x00, 0x00, (byte) 0xd8, (byte) 0xb6, 0x40, 0x17, (byte) 0x88, 0x0a, 0x51, 0x01, 0x07, 0x00, (byte) 0xa1, 0x00, (byte) 0xa2, 0x68, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x65, 0x4a, 0x61, 0x63, 0x6f, 0x62, 0x67, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x68, 0x61, 0x73, 0x20, 0x6a, 0x6f, 0x69, 0x6e, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x6f, 0x6f, 0x6d }; SPUD packet = new SPUD(); packet.deserialize(spudPacket, 0, spudPacket.length); byte[] packetSerialized = packet.serialize(); assertTrue(Arrays.equals(spudPacket, packetSerialized)); }