class TCP extends BasePacket {

    public byte[] serialize() {
        int length;
        if (dataOffset == 0)
            dataOffset = 5;  // default header length
        length = dataOffset << 2;
        byte[] payloadData = null;
        if (payload != null) {
            payload.setParent(this);
            payloadData = payload.serialize();
            length += payloadData.length;
        }

        byte[] data = new byte[length];
        ByteBuffer bb = ByteBuffer.wrap(data);

        bb.putShort((short)this.sourcePort.getPort()); //TCP ports are defined to be 16 bits
        bb.putShort((short)this.destinationPort.getPort());
        bb.putInt(this.sequence);
        bb.putInt(this.acknowledge);
        bb.putShort((short) (this.flags | (dataOffset << 12)));
        bb.putShort(this.windowSize);
        bb.putShort(this.checksum);
        bb.putShort(this.urgentPointer);
        if (dataOffset > 5) {
            int padding;
            bb.put(options);
            padding = (dataOffset << 2) - 20 - options.length;
            for (int i = 0; i < padding; i++)
                bb.put((byte) 0);
        }
        if (payloadData != null)
            bb.put(payloadData);

        if (this.parent != null && this.parent instanceof IPv4)
            ((IPv4)this.parent).setProtocol(IpProtocol.TCP);

        // compute checksum if needed
        if (this.checksum == 0) {
            bb.rewind();
            int accumulation = 0;

            // compute pseudo header mac
            if (this.parent != null && this.parent instanceof IPv4) {
                IPv4 ipv4 = (IPv4) this.parent;
                accumulation += ((ipv4.getSourceAddress().getInt() >> 16) & 0xffff)
                        + (ipv4.getSourceAddress().getInt() & 0xffff);
                accumulation += ((ipv4.getDestinationAddress().getInt() >> 16) & 0xffff)
                        + (ipv4.getDestinationAddress().getInt() & 0xffff);
                accumulation += ipv4.getProtocol().getIpProtocolNumber() & 0xff;
                accumulation += length & 0xffff;
            }

            for (int i = 0; i < length / 2; ++i) {
                accumulation += 0xffff & bb.getShort();
            }
            // pad to an even number of shorts
            if (length % 2 > 0) {
                accumulation += (bb.get() & 0xff) << 8;
            }

            accumulation = ((accumulation >> 16) & 0xffff)
                    + (accumulation & 0xffff);
            this.checksum = (short) (~accumulation & 0xffff);
            bb.putShort(16, this.checksum);
        }
        return data;
    }

}

class TCPTest {

    private byte[] pktSerialized;

    @Test
    public void testSerialize() {

        IPacket packet = new IPv4()
        .setDiffServ((byte) 0x20)
        .setIdentification((short) 0x1d85)
        .setFlags((byte) 0x00)
        .setTtl((byte) 50)
        .setSourceAddress("74.125.45.109")
        .setDestinationAddress("192.168.1.111")
        .setPayload(new TCP()
                        .setSourcePort(993)
                        .setDestinationPort(49202)
                        .setSequence(0xe3adee88)
                        .setAcknowledge(0xb7dad824)
                        .setDataOffset((byte) 8)
                        .setFlags((short) 0x10)
                        .setWindowSize((short) 267)
                        .setOptions(new byte[] {0x01, 0x01, 0x08, 0x0a, 0x20, (byte) 0x9a,
                                                0x41, 0x04, 0x07, 0x76, 0x53, 0x1f})
                        .setPayload(null)
                   );
        byte[] actual = packet.serialize();
        assertTrue(Arrays.equals(pktSerialized, actual));
    }

}