private static String escapeJavaString ( String input ) { int len = input . length ( ) ; // assume (for performance, not for correctness) that string will not expand by more than 10 chars StringBuilder out = new StringBuilder ( len + 10 ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = input . charAt ( i ) ; if ( c >= 32 && c <= 0x7f ) { if ( c == ' ' ) { out . append ( ' ' ) ; out . append ( ' ' ) ; } else if ( c == ' ' ) { out . append ( ' ' ) ; out . append ( ' ' ) ; } else { out . append ( c ) ; } } else { out . append ( ' ' ) ; out . append ( ' ' ) ; // one liner hack to have the hex string of length exactly 4 out . append ( Integer . toHexString ( c | 0x10000 ) . substring ( 1 ) ) ; } } return out . toString ( ) ; }
in Guava 15 when released