content: package alchemystar.freedom.engine.net.proto.mysql;
  
  import alchemystar.freedom.engine.net.proto.MySQLPacket;
  import alchemystar.freedom.engine.net.proto.util.BufferUtil;
  import alchemystar.freedom.engine.net.proto.util.Capabilities;
  import io.netty.buffer.ByteBuf;
  import io.netty.channel.Channel;
  import io.netty.channel.ChannelHandlerContext;
  
  /**
   * @Author lizhuyang
   */
  public class AuthPacket extends MySQLPacket{
      private static final byte[] FILLER = new byte[23];
  
      public long clientFlags;
      public long maxPacketSize;
      public int charsetIndex;
      public byte[] extra;// from FILLER(23)
      public String user;
      public byte[] password;
      public String database;
  
      public void read(BinaryPacket bin) {
          packetLength = bin.packetLength;
          packetId = bin.packetId;
          MySQLMessage mm = new MySQLMessage(bin.data);
          clientFlags = mm.readUB4();
          maxPacketSize = mm.readUB4();
          charsetIndex = (mm.read() & 0xff);
          int current = mm.position();
          int len = (int) mm.readLength();
          if (len > 0 && len < FILLER.length) {
              byte[] ab = new byte[len];
              System.arraycopy(mm.bytes(), mm.position(), ab, 0, len);
              this.extra = ab;
          }
          mm.position(current + FILLER.length);
          user = mm.readStringWithNull();
          password = mm.readBytesWithLength();
          if (((clientFlags & Capabilities.CLIENT_CONNECT_WITH_DB) != 0) && mm.hasRemaining()) {
              database = mm.readStringWithNull();
          }
      }
  
  
  
      public void write(Channel c) {
          // default init 256,so it can avoid buff extract
          ByteBuf buffer = c.alloc().buffer();
          BufferUtil.writeUB3(buffer, calcPacketSize());
          buffer.writeByte(packetId);
          BufferUtil.writeUB4(buffer, clientFlags);
          BufferUtil.writeUB4(buffer, maxPacketSize);
          buffer.writeByte((byte) charsetIndex);
          buffer.writeBytes(FILLER);
          if (user == null) {
              buffer.writeByte((byte) 0);
          } else {
              byte[] userData = user.getBytes();
              BufferUtil.writeWithNull(buffer, userData);
          }
          if (password == null) {
              buffer.writeByte((byte) 0);
          } else {
              BufferUtil.writeWithLength(buffer, password);
          }
          if (database == null) {
              buffer.writeByte((byte) 0);
          } else {
              byte[] databaseData = database.getBytes();
              BufferUtil.writeWithNull(buffer, databaseData);
          }
          c.writeAndFlush(buffer);
      }
  
      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);
          BufferUtil.writeUB4(buffer, clientFlags);
          BufferUtil.writeUB4(buffer, maxPacketSize);
          buffer.writeByte((byte) charsetIndex);
          buffer.writeBytes(FILLER);
          if (user == null) {
              buffer.writeByte((byte) 0);
          } else {
              byte[] userData = user.getBytes();
              BufferUtil.writeWithNull(buffer, userData);
          }
          if (password == null) {
              buffer.writeByte((byte) 0);
          } else {
              BufferUtil.writeWithLength(buffer, password);
          }
          if (database == null) {
              buffer.writeByte((byte) 0);
          } else {
              byte[] databaseData = database.getBytes();
              BufferUtil.writeWithNull(buffer, databaseData);
          }
          ctx.writeAndFlush(buffer);
      }
  
      @Override
      public int calcPacketSize() {
          int size = 32;// 4+4+1+23;
          size += (user == null) ? 1 : user.length() + 1;
          size += (password == null) ? 1 : BufferUtil.getLength(password);
          size += (database == null) ? 1 : database.length() + 1;
          return size;
      }
  
      @Override
      protected String getPacketInfo() {
          return "MySQL Authentication Packet";
      }
  }
  