content: /*
   * Copyright (C) 2016 alchemystar, Inc. All Rights Reserved.
   */
  package alchemystar.lancelot.common.net.proto.mysql;
  
  import alchemystar.lancelot.common.net.proto.MySQLPacket;
  import alchemystar.lancelot.common.net.proto.util.BufferUtil;
  import io.netty.buffer.ByteBuf;
  import io.netty.channel.ChannelHandlerContext;
  
  /**
   * 结果集头部Packet
   *
   * @Author lizhuyang
   */
  public class ResultSetHeaderPacket extends MySQLPacket {
  
      public int fieldCount;
      public long extra;
  
      public void read(byte[] data) {
          MySQLMessage mm = new MySQLMessage(data);
          this.packetLength = mm.readUB3();
          this.packetId = mm.read();
          this.fieldCount = (int) mm.readLength();
          if (mm.hasRemaining()) {
              this.extra = mm.readLength();
          }
      }
  
      @Override
      public ByteBuf writeBuf(ByteBuf buffer,ChannelHandlerContext ctx) {
          int size = calcPacketSize();
          BufferUtil.writeUB3(buffer, size);
          buffer.writeByte(packetId);
          BufferUtil.writeLength(buffer, fieldCount);
          if (extra > 0) {
              BufferUtil.writeLength(buffer, extra);
          }
          return buffer;
      }
  
  
      @Override
      public int calcPacketSize() {
          int size = BufferUtil.getLength(fieldCount);
          if (extra > 0) {
              size += BufferUtil.getLength(extra);
          }
          return size;
      }
  
      @Override
      protected String getPacketInfo() {
          return "MySQL ResultSetHeader Packet";
      }
  
  }