public static String guessEncoding ( byte [ ] message ) { // this function is inspired by a similarly named function in ZXing, but was written from scratch // using specifications for each encoding since I didn't understand what they were doing boolean isUtf8 = true ; boolean isJis = true ; boolean isIso = true ; for ( int i = 0 ; i < message . length ; i ++ ) { int v = message [ i ] & 0xFF ; if ( isUtf8 ) isUtf8 = isValidUTF8 ( v ) ; if ( isJis ) isJis = isValidJIS ( v ) ; if ( isIso ) isIso = isValidIso8869_1 ( v ) ; } // System.out.printf("UTF-8=%s ISO=%s JIS=%s\n",isUtf8,isIso,isJis); // If there is ambiguity do it based on how common it is and what the specification says if ( isUtf8 ) return UTF8 ; if ( isIso ) return ISO8859_1 ; if ( isJis ) return JIS ; return UTF8 ; }
The encoding for byte messages should be ISO8859_1 or JIS depending on which version of the specification you follow . In reality people use whatever they want and expect it to magically work . This attempts to figure out if it s ISO8859_1 JIS or UTF8 . UTF - 8 is the most common and is used if its ambiguous .