Java 字符数组是否始终是有效的 UTF-16(Big Endian)编码?

Is a Java char array always a valid UTF-16 (Big Endian) encoding?

假设我将 Java 字符数组 (char[]) 实例编码为字节:

这会始终创建有效的 UTF-16BE 编码吗?如果不是,哪些代码点会导致无效编码?


这个问题与this question about the Java char type and this question about the internal representation of Java strings非常相关。

没有。您可以创建 char 个包含您想要的任何 16 位值的实例——没有任何东西可以将它们限制为有效的 UTF-16 代码单元,也没有将它们的数组限制为有效的 UTF-16 序列.即使 String 也不要求其数据是有效的 UTF-16:

char data[] = {'\uD800', 'b', 'c'};  // Unpaired lead surrogate
String str = new String(data);

Unicode 标准 Chapter 3 中规定了有效 UTF-16 数据的要求(基本上,所有内容都必须是 Unicode 标量值,并且所有代理项都必须正确配对)。您可以测试 char 数组是否为有效的 UTF-16 序列,并通过使用 CharsetEncoder:

将其转换为 UTF-16BE(或 LE)字节序列
CharsetEncoder encoder = Charset.forName("UTF-16BE").newEncoder();
ByteBuffer bytes = encoder.encode(CharBuffer.wrap(data)); // throws MalformedInputException

(如果你有字节,同样使用 CharsetDecoder。)