public static void stringToUnicodeBytes(HsqlByteArrayOutputStream b,
            String s, boolean doubleSingleQuotes) {

        final int len = s.length();
        char[]    chars;
        int       extras = 0;

        if (s == null || len == 0) {
            return;
        }

        chars = s.toCharArray();

        b.ensureRoom(len * 2 + 5);

        for (int i = 0; i < len; i++) {
            char c = chars[i];

            if (c == '\\') {
                if ((i < len - 1) && (chars[i + 1] == 'u')) {
                    b.writeNoCheck(c);    // encode the \ as unicode, so 'u' is ignored
                    b.writeNoCheck('u');
                    b.writeNoCheck('0');
                    b.writeNoCheck('0');
                    b.writeNoCheck('5');
                    b.writeNoCheck('c');

                    extras += 5;
                } else {
                    b.write(c);
                }
            } else if ((c >= 0x0020) && (c <= 0x007f)) {
                b.writeNoCheck(c);        // this is 99%

                if (c == '\'' && doubleSingleQuotes) {
                    b.writeNoCheck(c);

                    extras++;
                }
            } else {
                b.writeNoCheck('\\');
                b.writeNoCheck('u');
                b.writeNoCheck(HEXBYTES[(c >> 12) & 0xf]);
                b.writeNoCheck(HEXBYTES[(c >> 8) & 0xf]);
                b.writeNoCheck(HEXBYTES[(c >> 4) & 0xf]);
                b.writeNoCheck(HEXBYTES[c & 0xf]);

                extras += 5;
            }

            if (extras > len) {
                b.ensureRoom(len + extras + 5);

                extras = 0;
            }
        }
    }
public static void stringToUnicodeBytes ( HsqlByteArrayOutputStream b , String s , boolean doubleSingleQuotes ) { final int len = s . length ( ) ; char [ ] chars ; int extras = 0 ; if ( s == null || len == 0 ) { return ; } chars = s . toCharArray ( ) ; b . ensureRoom ( len * 2 + 5 ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = chars [ i ] ; if ( c == '\\' ) { if ( ( i < len - 1 ) && ( chars [ i + 1 ] == 'u' ) ) { b . writeNoCheck ( c ) ; b . writeNoCheck ( 'u' ) ; b . writeNoCheck ( '0' ) ; b . writeNoCheck ( '0' ) ; b . writeNoCheck ( '5' ) ; b . writeNoCheck ( 'c' ) ; extras += 5 ; } else { b . write ( c ) ; } } else if ( ( c >= 0x0020 ) && ( c <= 0x007f ) ) { b . writeNoCheck ( c ) ; if ( c == '\'' && doubleSingleQuotes ) { b . writeNoCheck ( c ) ; extras ++ ; } } else { b . writeNoCheck ( '\\' ) ; b . writeNoCheck ( 'u' ) ; b . writeNoCheck ( HEXBYTES [ ( c > > 12 ) & 0xf ] ) ; b . writeNoCheck ( HEXBYTES [ ( c > > 8 ) & 0xf ] ) ; b . writeNoCheck ( HEXBYTES [ ( c > > 4 ) & 0xf ] ) ; b . writeNoCheck ( HEXBYTES [ c & 0xf ] ) ; extras += 5 ; } if ( extras > len ) { b . ensureRoom ( len + extras + 5 ) ; extras = 0 ; } } }
Hsqldb specific encoding used only for log files. The SQL statements that
need to be written to the log file (input) are Java Unicode strings.
input is converted into a 7bit escaped ASCII string (output)with the
following transformations. All characters outside the 0x20-7f range are
converted to a escape sequence and added to output. If a backslash
character is immdediately followed by 'u', the backslash character is
converted to escape sequence and added to output. All the remaining
characters in input are added to output without conversion. The escape
sequence is backslash, letter u, xxxx, where xxxx is the hex
representation of the character code. (fredt@users)<p>

Method based on Hypersonic Code

@param b output stream to wite to
@param s Java string
@param doubleSingleQuotes boolean