question: 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 ; }
target:  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.