gpt4 book ai didi

java - 为什么Java的CharsetEncoder定义.onMalformedInput()/CharsetDecoder定义.onUnmappableCharacter()?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:23 25 4
gpt4 key购买 nike

A CharsetDecoder基本上有助于将 bytes 序列解码为 char 序列(参见 Charset#newDecoder() )。在对面,一个CharsetEncoder (参见 Charset#newEncoder())相反:采用 char 序列,并将它们编码为 byte 序列。

CharsetDecoder 定义了 .onMalformedInput() 并且看起来合乎逻辑(某些字节序列可能无法转换为有效的 char 序列);但是为什么 .onUnmappableCharacter() 因为它的输入是一个字节序列呢?

类似地,CharsetEncoder 定义了 .onUnmappableCharacter(),这又是合乎逻辑的(例如,如果您的字符集是 ASCII,则不能编码 ö);但为什么它还定义了 .onMalformedInput() 因为它的输入是一个字符序列?

更有趣的是,您无法从解码器获得编码器,反之亦然,而且这两个类似乎都没有共同的祖先...


编辑 1

确实可以在 CharsetEncoder 上触发 .onMalformedInput()。您“只需”提供非法的 charchar 序列。下面的程序依赖于这样一个事实,即在 UTF-16 中,高代理项必须跟在低代理项之后;此处,使用两个高代理项构建了一个二元素 char 数组,并尝试对其进行编码。 注意从这样一个格式错误的字符序列创建一个String是如何完全不抛出异常的:

代码:

public static void main(final String... args)
throws CharacterCodingException
{
boolean found = false;
char c = '.';

for (int i = 0; i < 65536; i++) {
if (Character.isHighSurrogate((char) i)) {
c = (char) i;
found = true;
break;
}
}
if (!found)
throw new IllegalStateException();

System.out.println("found: " + Integer.toHexString(c));
final char[] foo = { c, c };

new String(foo); // <-- DOES NOT THROW AN EXCEPTION!!!

final CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder()
.onMalformedInput(CodingErrorAction.REPORT);

encoder.encode(CharBuffer.wrap(foo));
}

输出:

found: d800
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:277)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:798)
at com.github.fge.largetext.LargeText.main(LargeText.java:166)

EDIT 2 但是现在,反过来呢?从下面@Kairos 的回答中引用联机帮助页:

UnmappableCharacterException - If the byte sequence starting at the input buffer's current position cannot be mapped to an equivalent character sequence and the current unmappable-character action is CodingErrorAction.REPORT

现在,什么是“无法映射到等效字符序列”?

我在 this project 中经常使用 CharsetDecoder并且还没有产生这样的错误。我知道如何重现错误,例如,三字节 UTF-8 序列中只有两个字节,但这会触发 MalformedInputException。在这种情况下,您所要做的就是从 ByteBuffer 的最后已知位置重新开始解码。

触发 UnmappableCharacterException 基本上意味着 字符编码本身 将允许生成非法的 char;或非法的 Unicode 代码点。

这可能吗?

最佳答案

根据 CharsetEncoder.encode() 的文档它声明它抛出 MalformedInputException

If the character sequence starting at the input buffer's current position is not a legal sixteen-bit Unicode sequence and the current malformed-input action is CodingErrorAction.REPORT

因此,您可以选择提供 CodingErrorAction利用onMalformedInput因此,如果您遇到这些非法的 16 位 Unicode 序列之一,将执行提供的操作。

同样适用于 CharsetDecoder.decode()

UnmappableCharacterException - If the byte sequence starting at the input buffer's current position cannot be mapped to an equivalent character sequence and the current unmappable-character action is CodingErrorAction.REPORT

关于java - 为什么Java的CharsetEncoder定义.onMalformedInput()/CharsetDecoder定义.onUnmappableCharacter()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22886669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com