Hi all,
I may not get clear of using Charset correctly. Below is my code that trying convert a big5 String into utf-8 String. But it fails on using utf-8 decoder to decode big5 byte buffer. Does any can tell me the correct usage of using Charset to convert string from one encoding to another, especially asian character.
Below is the exception stack I got
java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:771)
Charset charsetBig5 = Charset.forName("Big5");
Charset charsetUtf8 = Charset.forName("UTF-8");
String big5 = "這是中文";
CharsetEncoder big5Encoder = charsetBig5.newEncoder();
CharBuffer big5cb = CharBuffer.wrap(big5.toString());
ByteBuffer big5bb = big5Encoder.encode(big5cb);
CharsetDecoder utfDecoder = charsetUtf8.newDecoder();
CharBuffer utf8cb = utfDecoder.decode(big5bb); //crashed here in runtime
System.out.println(utf8cb.toString());
thanks a lot