content: package alchemystar.freedom.engine.net.proto.mysql;
  
  import alchemystar.freedom.engine.net.proto.MySQLPacket;
  import alchemystar.freedom.engine.net.proto.util.BufferUtil;
  import io.netty.buffer.ByteBuf;
  import io.netty.channel.ChannelHandlerContext;
  
  /**
   * MySql OkPacket
   * @Author lizhuyang
   */
  public class OkPacket extends MySQLPacket {
      public static final byte FIELD_COUNT = 0x00;
  
      public static final byte[] OK = new byte[] { 7, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0 };
     public static final byte[] AUTH_OK = new byte[] { 7, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 };
      public byte fieldCount = FIELD_COUNT;
      public long affectedRows;
      public long insertId;
      public int serverStatus;
      public int warningCount;
      public byte[] message;
  
      public void read(BinaryPacket bin) {
          packetLength = bin.packetLength;
          packetId = bin.packetId;
          MySQLMessage mm = new MySQLMessage(bin.data);
          fieldCount = mm.read();
          affectedRows = mm.readLength();
          insertId = mm.readLength();
          serverStatus = mm.readUB2();
          warningCount = mm.readUB2();
          if (mm.hasRemaining()) {
              this.message = mm.readBytesWithLength();
          }
      }
  
      public void write(ChannelHandlerContext ctx) {
          // default init 256,so it can avoid buff extract
          ByteBuf buffer = ctx.alloc().buffer();
          BufferUtil.writeUB3(buffer, calcPacketSize());
          buffer.writeByte(packetId);
          buffer.writeByte(fieldCount);
          BufferUtil.writeLength(buffer, affectedRows);
          BufferUtil.writeLength(buffer, insertId);
          BufferUtil.writeUB2(buffer, serverStatus);
          BufferUtil.writeUB2(buffer, warningCount);
          if (message != null) {
              BufferUtil.writeWithLength(buffer, message);
          }
          ctx.writeAndFlush(buffer);
      }
  
      @Override
      public int calcPacketSize() {
          int i = 1;
          i += BufferUtil.getLength(affectedRows);
          i += BufferUtil.getLength(insertId);
          i += 4;
          if (message != null) {
              i += BufferUtil.getLength(message);
          }
          return i;
      }
  
      @Override
      protected String getPacketInfo() {
          return "MySQL OK Packet";
      }
  
  }