public void send(String sql,byte commandType) throws IOException, QueryException {
  startPacket(0,true);
  int charsLength=sql.length();
  int charsOffset=0;
  int position=4;
  assureBufferCapacity((charsLength * 3) + 1);
  byte[] arr=buffer.array();
  arr[position++]=commandType;
  while (charsOffset < charsLength) {
    char currChar=sql.charAt(charsOffset++);
    if (currChar < 0x80) {
      arr[position++]=(byte)currChar;
    }
 else     if (currChar < 0x800) {
      arr[position++]=(byte)(0xc0 | (currChar >> 6));
      arr[position++]=(byte)(0x80 | (currChar & 0x3f));
    }
 else     if (currChar >= 0xD800 && currChar < 0xE000) {
      if (currChar >= 0xD800 && currChar < 0xDC00) {
        if (charsOffset + 1 > charsLength) {
          arr[position++]=(byte)0x63;
        }
 else {
          char nextChar=sql.charAt(charsOffset);
          if (nextChar >= 0xDC00 && nextChar < 0xE000) {
            int surrogatePairs=((currChar << 10) + nextChar) + (0x010000 - (0xD800 << 10) - 0xDC00);
            arr[position++]=(byte)(0xf0 | ((surrogatePairs >> 18)));
            arr[position++]=(byte)(0x80 | ((surrogatePairs >> 12) & 0x3f));
            arr[position++]=(byte)(0x80 | ((surrogatePairs >> 6) & 0x3f));
            arr[position++]=(byte)(0x80 | (surrogatePairs & 0x3f));
            charsOffset++;
          }
 else {
            arr[position++]=(byte)0x63;
          }
        }
      }
 else {
        arr[position++]=(byte)0x63;
      }
    }
 else {
      arr[position++]=(byte)(0xe0 | ((currChar >> 12)));
      arr[position++]=(byte)(0x80 | ((currChar >> 6) & 0x3f));
      arr[position++]=(byte)(0x80 | (currChar & 0x3f));
    }
  }
  if (position - 4 < maxPacketSize && !useCompression) {
    arr[0]=(byte)((position - 4) & 0xff);
    arr[1]=(byte)((position - 4) >>> 8);
    arr[2]=(byte)((position - 4) >>> 16);
    arr[3]=(byte)0;
    if (logger.isTraceEnabled()) {
      logger.trace("send packet seq:" + seqNo + " length:"+ (position - 4)+ " data:"+ Utils.hexdump(arr,maxQuerySizeToLog,4,position - 4));
    }
    outputStream.write(arr,0,position);
  }
 else {
    sendDirect(arr,5,position - 5,commandType);
  }
}

public void send ( String sql , byte commandType ) throws IOException , QueryException { startPacket ( 0 , true ) ; int charsLength = sql . length ( ) ; int charsOffset = 0 ; int position = 4 ; assureBufferCapacity ( ( charsLength * 3 ) + 1 ) ; byte [ ] arr = buffer . array ( ) ; arr [ position ++ ] = commandType ; while ( charsOffset < charsLength ) { char currChar = sql . charAt ( charsOffset ++ ) ; if ( currChar < 0x80 ) { arr [ position ++ ] = ( byte ) currChar ; } else if ( currChar < 0x800 ) { arr [ position ++ ] = ( byte ) ( 0xc0 | ( currChar > > 6 ) ) ; arr [ position ++ ] = ( byte ) ( 0x80 | ( currChar & 0x3f ) ) ; } else if ( currChar >= 0xD800 && currChar < 0xE000 ) { if ( currChar >= 0xD800 && currChar < 0xDC00 ) { if ( charsOffset + 1 > charsLength ) { arr [ position ++ ] = ( byte ) 0x63 ; } else { char nextChar = sql . charAt ( charsOffset ) ; if ( nextChar >= 0xDC00 && nextChar < 0xE000 ) { int surrogatePairs = ( ( currChar << 10 ) + nextChar ) + ( 0x010000 - ( 0xD800 << 10 ) - 0xDC00 ) ; arr [ position ++ ] = ( byte ) ( 0xf0 | ( ( surrogatePairs > > 18 ) ) ) ; arr [ position ++ ] = ( byte ) ( 0x80 | ( ( surrogatePairs > > 12 ) & 0x3f ) ) ; arr [ position ++ ] = ( byte ) ( 0x80 | ( ( surrogatePairs > > 6 ) & 0x3f ) ) ; arr [ position ++ ] = ( byte ) ( 0x80 | ( surrogatePairs & 0x3f ) ) ; charsOffset ++ ; } else { arr [ position ++ ] = ( byte ) 0x63 ; } } } else { arr [ position ++ ] = ( byte ) 0x63 ; } } else { arr [ position ++ ] = ( byte ) ( 0xe0 | ( ( currChar > > 12 ) ) ) ; arr [ position ++ ] = ( byte ) ( 0x80 | ( ( currChar > > 6 ) & 0x3f ) ) ; arr [ position ++ ] = ( byte ) ( 0x80 | ( currChar & 0x3f ) ) ; } } if ( position - 4 < maxPacketSize && ! useCompression ) { arr [ 0 ] = ( byte ) ( ( position - 4 ) & 0xff ) ; arr [ 1 ] = ( byte ) ( ( position - 4 ) > > > 8 ) ; arr [ 2 ] = ( byte ) ( ( position - 4 ) > > > 16 ) ; arr [ 3 ] = ( byte ) 0 ; if ( logger . isTraceEnabled ( ) ) { logger . trace ( "send packet seq:" + seqNo + " length:" + ( position - 4 ) + " data:" + Utils . hexdump ( arr , maxQuerySizeToLog , 4 , position - 4 ) ) ; } outputStream . write ( arr , 0 , position ) ; } else { sendDirect ( arr , 5 , position - 5 , commandType ) ; } }
send sql string to outputstream . sql will be transform to utf - 8 byte buffer , and if possible this buffer will be send to stream directly .
