private void escapeUtf8(){
  int charsLength=stringValue.length();
  charsOffset=0;
  position=0;
  if (charsLength == 0) {
    position=2;
    escapedArray=new byte[]{(byte)'\'',(byte)'\''};
    return;
  }
  escapedArray=new byte[(charsLength * 3) + 2];
  escapedArray[position++]=(byte)'\'';
  char charValue;
  if (noBackslashEscapes) {
    do {
      charValue=stringValue.charAt(charsOffset);
      if (charValue < 0x80) {
        if (charValue == '\'')         escapedArray[position++]=(byte)'\'';
        escapedArray[position++]=(byte)charValue;
        charsOffset++;
      }
 else {
        break;
      }
    }
 while (charsOffset < charsLength);
  }
 else {
    do {
      charValue=stringValue.charAt(charsOffset);
      if (charValue < 0x80) {
        if (charValue == '\'' || charValue == '\\' || charValue == '"' || charValue == 0)         escapedArray[position++]=(byte)'\\';
        escapedArray[position++]=(byte)charValue;
        charsOffset++;
      }
 else {
        break;
      }
    }
 while (charsOffset < charsLength);
  }
  while (charsOffset < charsLength) {
    char currChar=stringValue.charAt(charsOffset++);
    if (currChar < 0x80) {
      if (currChar == '\'') {
        escapedArray[position++]=noBackslashEscapes ? (byte)'\'' : (byte)'\\';
      }
 else       if (!noBackslashEscapes && (currChar == '\\' || currChar == '"' || currChar == 0)) {
        escapedArray[position++]=(byte)'\\';
      }
      escapedArray[position++]=(byte)currChar;
    }
 else     getNonAsciiByte(currChar,stringValue,charsLength);
  }
  escapedArray[position++]=(byte)'\'';
  stringValue=null;
  binary=false;
}

private void escapeUtf8 ( ) { int charsLength = stringValue . length ( ) ; charsOffset = 0 ; position = 0 ; if ( charsLength == 0 ) { position = 2 ; escapedArray = new byte [ ] { ( byte ) '\'' , ( byte ) '\'' } ; return ; } escapedArray = new byte [ ( charsLength * 3 ) + 2 ] ; escapedArray [ position ++ ] = ( byte ) '\'' ; char charValue ; if ( noBackslashEscapes ) { do { charValue = stringValue . charAt ( charsOffset ) ; if ( charValue < 0x80 ) { if ( charValue == '\'' ) escapedArray [ position ++ ] = ( byte ) '\'' ; escapedArray [ position ++ ] = ( byte ) charValue ; charsOffset ++ ; } else { break ; } } while ( charsOffset < charsLength ) ; } else { do { charValue = stringValue . charAt ( charsOffset ) ; if ( charValue < 0x80 ) { if ( charValue == '\'' || charValue == '\\' || charValue == '"' || charValue == 0 ) escapedArray [ position ++ ] = ( byte ) '\\' ; escapedArray [ position ++ ] = ( byte ) charValue ; charsOffset ++ ; } else { break ; } } while ( charsOffset < charsLength ) ; } while ( charsOffset < charsLength ) { char currChar = stringValue . charAt ( charsOffset ++ ) ; if ( currChar < 0x80 ) { if ( currChar == '\'' ) { escapedArray [ position ++ ] = noBackslashEscapes ? ( byte ) '\'' : ( byte ) '\\' ; } else if ( ! noBackslashEscapes && ( currChar == '\\' || currChar == '"' || currChar == 0 ) ) { escapedArray [ position ++ ] = ( byte ) '\\' ; } escapedArray [ position ++ ] = ( byte ) currChar ; } else getNonAsciiByte ( currChar , stringValue , charsLength ) ; } escapedArray [ position ++ ] = ( byte ) '\'' ; stringValue = null ; binary = false ; }
encode char array to utf - 8 byte array , with sql escape . driver exchange with server use exclusively utf - 8 and lot of parameters are string . most the time spend using driver is spend transforming string parameters in utf - 8 escaped bytes . every string as to be escaped to avoid sql injection , so whe must loop through the string char array to add escape chars , creating a new char array , and after decoding this char array to utf - 8 bytes . using standard java , this result in : - recreating a lot of char array ( and copy to new array ) during escape . - utf - 8 encoding is using internally a byte array initialized to 3 * the char length and will be spliced when final length is known . when dealing with big string , that is using a lot of array for nothing ( and so memory issues ) resulting byte array will be send to outputstream , so : - escape characters are ascii characters ( one byte ) - > the escaped characters can be put directly in array - driver don ' t need the final byte array shrink , the byte array can be send directly in outputstream . example for a 1k character string : if using standard java - getting tochararray ( ) will create a new 1k char array ( and copy existing array into it ) . - escaping ( using a stringbuffer ) will use a new 1k char array and the internal array may expand ( = > new allocation + copy ) if there is any char to be escape - getbytes ( " utf - 8 " ) : a new 3k byte buffer will be created , and finally a new one ( + copy into it ) when real length is known . current implementation : - creating a 3k bytes array , send directly these array with length into outputstream . = > only one array .
