protected void writeUTF ( int code , String string ) throws IOException { OutputStream os = getOutputStream ( ) ; os . write ( code ) ; int charLength = string . length ( ) ; int length = 0 ; for ( int i = 0 ; i < charLength ; i ++ ) { char ch = string . charAt ( i ) ; if ( ch < 0x80 ) length ++ ; else if ( ch < 0x800 ) length += 2 ; else length += 3 ; } os . write ( length >> 8 ) ; os . write ( length ) ; for ( int i = 0 ; i < length ; i ++ ) { char ch = string . charAt ( i ) ; if ( ch < 0x80 ) os . write ( ch ) ; else if ( ch < 0x800 ) { os . write ( 0xc0 + ( ch >> 6 ) & 0x1f ) ; os . write ( 0x80 + ( ch & 0x3f ) ) ; } else { os . write ( 0xe0 + ( ch >> 12 ) & 0xf ) ; os . write ( 0x80 + ( ( ch >> 6 ) & 0x3f ) ) ; os . write ( 0x80 + ( ch & 0x3f ) ) ; } } }
Writes a UTF - 8 string .