@java.lang.SuppressWarnings("squid:S128") // suppress switch fall through warning
    @Override
    public byte[] serialize() {
        byte[] data = new byte[8915];

        ByteBuffer bb = ByteBuffer.wrap(data);
        bb.put(this.getIgmpType());

        // reserved or max resp code depending on type.
        bb.put(this.resField);

        // Must calculate checksum
        bb.putShort((short) 0);

        if (this instanceof IGMPv3) {
            switch (this.igmpType) {

                case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT:
                    // reserved
                    bb.putShort((short) 0);
                    // Number of groups
                    bb.putShort((short) groups.size());
                    // Fall through

                case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY:

                    for (IGMPGroup grp : groups) {
                        grp.serialize(bb);
                    }
                    break;

                default:
                    bb.put(this.unsupportTypeData);
                    break;
            }
        } else if (this instanceof IGMPv2) {
            if (this.groups.isEmpty()) {
                bb.putInt(0);
            } else {
                bb.putInt(groups.get(0).getGaddr().getIp4Address().toInt());
            }
        } else {
            throw new UnsupportedOperationException();
        }

        int size = bb.position();

        // compute checksum if needed
        if (this.checksum == 0) {
            bb.rewind();
            int accumulation = 0;
            for (int i = 0; i < size * 2; ++i) {
                accumulation += 0xffff & bb.getShort();
            }
            accumulation = (accumulation >> 16 & 0xffff)
                    + (accumulation & 0xffff);
            this.checksum = (short) (~accumulation & 0xffff);
            bb.putShort(2, this.checksum);
        }


        bb.position(0);
        byte[] rdata = new byte[size];
        bb.get(rdata, 0, size);
        return rdata;
    }
@Test
    public void testDeserializeTruncated() throws Exception {
        byte[] bits = igmpQuery.serialize();
        PacketTestUtils.testDeserializeTruncated(deserializer, bits);

        bits = igmpMembership.serialize();
        PacketTestUtils.testDeserializeTruncated(deserializer, bits);
    }