出于某种原因,我想从同一个InputStream中解码不同编码的字符(比如UTF-8和UTF-16)。请参阅以下(示例)代码:
public static void main(String[] args) throws IOException {
byte[] b = new byte[] { (byte) 0xE4, (byte) 0xB8, (byte) 0x96,
(byte) 0xE7, (byte) 0x95, (byte) 0x8C};
byte[] b1 = new byte[] {(byte) 0x5E, (byte) 0xE3,
(byte) 0x84, (byte) 0x49,
(byte) 0x84, (byte) 0x2C, (byte) 0x9E,(byte) 0x97 };
byte[] b2 = new byte[] { (byte) 0xE4, (byte) 0xB8, (byte) 0x96,
(byte) 0xE7, (byte) 0x95, (byte) 0x8C, (byte) 0x5E, (byte) 0xE3,
(byte) 0x84, (byte) 0x49,
(byte) 0x84, (byte) 0x2C, (byte) 0x9E,(byte) 0x97 };
ByteArrayInputStream bs = null;
InputStreamReader ir = null;
System.out.println("Part 1:");
bs = new ByteArrayInputStream(b);
ir = new InputStreamReader(bs, "UTF-8");
System.out.println((char) ir.read());
System.out.println((char) ir.read());
bs = new ByteArrayInputStream(b1);
ir = new InputStreamReader(bs, "UTF-16");
System.out.println((char) ir.read());
System.out.println((char) ir.read());
System.out.println((char) ir.read());
System.out.println((char) ir.read());
System.out.println("Part 2:");
bs = new ByteArrayInputStream(b2);
ir = new InputStreamReader(bs, "UTF-8");
System.out.println((char) ir.read());
System.out.println((char) ir.read());
ir = new InputStreamReader(bs, "UTF-16");
System.out.println((char) ir.read());
System.out.println((char) ir.read());
System.out.println((char) ir.read());
System.out.println((char) ir.read());
}
第一部分正确显示字符,但第二部分不解码变量b2中的剩余UTF-16字符(使用单个InputStream读取UTF-8和UTF-16字符的混合)。因此,b2是b和b1变量的串联。
有什么想法吗?或者正在接管禁止的部分阅读流?
编辑: 我把它的价值粘贴在输出上。它不会在这里显示实际的字符,但是当我运行eclipse时它会显示它们:
Part 1:
\u4e16
\u754c
\u5ee3
\u8449
\u842c
\u9e97
Part 2:
\u4e16
\u754c
\uffff
\uffff
\uffff
\uffff