class MySQLLengthFieldBasedFrameEncoder extends MessageToByteEncoder {

    @Override
    protected void encode(final ChannelHandlerContext ctx, final Object msg, final ByteBuf out) {
        ByteBuf byteBuf = ((AbstractPacket) msg).toByteBuf();
        HeaderPacket h = new HeaderPacket();
        h.setPacketBodyLength(byteBuf.readableBytes());
        h.setPacketSequenceNumber(((AbstractPacket) msg).getSequenceNumber());
        out.writeBytes(h.toByteBuf());
        out.writeBytes(byteBuf);
    }

}