private static void writeUtf8 ( OutputAccessor out , CharSequence seq , int len ) { // We can use the _set methods as these not need to do any index checks and reference checks. // This is possible as we called ensureWritable(...) before. for ( int i = 0 ; i < len ; i ++ ) { char c = seq . charAt ( i ) ; switch ( c ) { case ' ' : out . write ( B ) ; continue ; case ' ' : out . write ( T ) ; continue ; case ' ' : out . write ( N ) ; continue ; case ' ' : out . write ( F ) ; continue ; case ' ' : out . write ( R ) ; continue ; case ' ' : out . write ( QUOT ) ; continue ; case ' ' : out . write ( BSLASH ) ; continue ; } if ( c < 0x20 ) { escape ( out , c ) ; } else if ( c < 0x80 ) { out . write ( ( byte ) c ) ; } else if ( c < 0x800 ) { out . write ( ( byte ) ( 0xc0 | ( c >> 6 ) ) ) ; out . write ( ( byte ) ( 0x80 | ( c & 0x3f ) ) ) ; } else if ( isSurrogate ( c ) ) { if ( ! Character . isHighSurrogate ( c ) ) { out . write ( WRITE_UTF_UNKNOWN ) ; continue ; } final char c2 ; try { // Surrogate Pair consumes 2 characters. Optimistically try to get the next character to avoid // duplicate bounds checking with charAt. If an IndexOutOfBoundsException is thrown we will // re-throw a more informative exception describing the problem. c2 = seq . charAt ( ++ i ) ; } catch ( IndexOutOfBoundsException e ) { out . write ( WRITE_UTF_UNKNOWN ) ; break ; } if ( ! Character . isLowSurrogate ( c2 ) ) { out . write ( WRITE_UTF_UNKNOWN ) ; out . write ( ( byte ) ( Character . isHighSurrogate ( c2 ) ? WRITE_UTF_UNKNOWN : c2 ) ) ; continue ; } int codePoint = Character . toCodePoint ( c , c2 ) ; // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G2630. escape ( out , c ) ; escape ( out , codePoint ) ; } else { out . write ( ( byte ) ( 0xe0 | ( c >> 12 ) ) ) ; out . write ( ( byte ) ( 0x80 | ( ( c >> 6 ) & 0x3f ) ) ) ; out . write ( ( byte ) ( 0x80 | ( c & 0x3f ) ) ) ; } } }
Fast - Path UTF - 8 implementation .