### Question:
HandshakePacketParser extends PacketParser { @Override public byte[] serialize() throws PacketParserException { byte[] outputArray = null; try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bos)) { out.writeByte(PACKETS.HANDSHAKE_OPEN.ordinal()); out.write(owner.getKey().getFingerPrint()); out.write(owner.getFlags().toByte()); if (!owner.getFlags().isRegistered()) { byte[] pubExp = owner.getKey().getPem().getPublicExponent().getBytes(); if (pubExp.length > 0x0000ffff) throw new PacketParserException("Invalid Handshake Packet has unsupported public exponent size."); out.write((byte) ((pubExp.length & 0xff00) >> 8)); out.write((byte) (pubExp.length & 0x00ff)); out.write(pubExp); byte[] modulus = owner.getKey().getPem().getModulus().getBytes(); if (modulus.length > 0x0000ffff) throw new PacketParserException("Invalid Handshake Packet has unsupported modulus size."); out.write((byte) ((modulus.length & 0xff00) >> 8)); out.write((byte) (modulus.length & 0x00ff)); out.write(modulus); } outputArray = bos.toByteArray(); } catch (IOException ignored) { } return outputArray; } HandshakePacketParser(HandshakePacket owner); @Override Packet parse(PacketBuffer packetBuffer, Remote remote); @Override byte[] serialize();  }### Answer:
@Test public void testSerialize() throws Exception { PacketParser parser = new HandshakePacket().getParser(); HandshakePacket.Flags flags = new HandshakePacket.Flags(); flags.setRegistered(true); flags.setHandshakeRequest(true); flags.setRequestsReturnService(true); byte[] buffer = new byte[] { (byte) PACKETS.HANDSHAKE_OPEN.ordinal(), 0,2,4,8,16,32,64,2,4,8,16,32,64,2,4,8,16,32,64,2,4,8,16,32,64,2,4,8,16,32,64,0, flags.toByte() }; HandshakePacket packet = (HandshakePacket) parser.parse(new PacketBuffer().write(buffer), null); byte[] bytes = packet.getParser().serialize(); assertArrayEquals(buffer, bytes); }