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;
  
  /**
   * Error Packet
   *
   * @Author lizhuyang
   */
  public class ErrorPacket extends MySQLPacket {
      public static final byte FIELD_COUNT = (byte) 0xff;
      private static final byte SQLSTATE_MARKER = (byte) '#';
      private static final byte[] DEFAULT_SQLSTATE = "HY000".getBytes();
  
      public byte fieldCount = FIELD_COUNT;
      public int errno;
      public byte mark = SQLSTATE_MARKER;
      public byte[] sqlState = DEFAULT_SQLSTATE;
      public byte[] message;
  
      public void read(BinaryPacket bin) {
          packetLength = bin.packetLength;
          packetId = bin.packetId;
          MySQLMessage mm = new MySQLMessage(bin.data);
          fieldCount = mm.read();
          errno = mm.readUB2();
          if (mm.hasRemaining() && (mm.read(mm.position()) == SQLSTATE_MARKER)) {
              mm.read();
              sqlState = mm.readBytes(5);
          }
          message = mm.readBytes();
      }
  
      public void read(byte[] data) {
          MySQLMessage mm = new MySQLMessage(data);
          packetLength = mm.readUB3();
          packetId = mm.read();
          fieldCount = mm.read();
          errno = mm.readUB2();
          if (mm.hasRemaining() && (mm.read(mm.position()) == SQLSTATE_MARKER)) {
              mm.read();
              sqlState = mm.readBytes(5);
          }
          message = mm.readBytes();
      }
  
      @Override
      public void write(ChannelHandlerContext ctx) {
          int size = calcPacketSize();
          // default 256 , no need to check and auto expand
          ByteBuf buffer = ctx.alloc().buffer();
          BufferUtil.writeUB3(buffer, size);
          buffer.writeByte(packetId);
          buffer.writeByte(fieldCount);
          BufferUtil.writeUB2(buffer, errno);
          buffer.writeByte(mark);
          buffer.writeBytes(sqlState);
          if (message != null) {
              buffer.writeBytes(message);
          }
          ctx.writeAndFlush(buffer);
      }
  
      @Override
      public int calcPacketSize() {
          int size = 9;// 1 + 2 + 1 + 5
          if (message != null) {
              size += message.length;
          }
          return size;
      }
  
      @Override
      protected String getPacketInfo() {
          return "MySQL Error Packet";
      }
  
  }