Charsetdecoder, используемый в InputStream в выпуске Java 8 и далее, похоже, не чтит кодингерроризации.JAVA

Программисты JAVA общаются здесь
Anonymous
Charsetdecoder, используемый в InputStream в выпуске Java 8 и далее, похоже, не чтит кодингерроризации.

Сообщение Anonymous »

Проблема можно найти в версии Java8. Charsetdecoder явно настроен с codingerroraction.replace ; Я ожидаю, что сменной символ \ ufffd будет применен при декодировании узолового ввода.final String z = "髙";
Charset charset = Charset.forName("x-windows-iso2022jp");

CharsetDecoder charsetDecoderForStr = charset.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);

CharsetDecoder charsetDecoderForStrm = charset.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);

ByteBuffer buffer = ByteBuffer.allocate(21);
byte[] bytes = {27, 36, 66, 124, 98, 27, 40, 66, 27};
buffer.put(bytes);

System.out.println("Using new String without Decoder"+new String(bytes, charset).trim());
System.out.println("Using Decoder "+charsetDecoderForStr.decode((ByteBuffer) buffer.flip()).toString().trim());

Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), charsetDecoderForStrm);
char[] chars = new char[10];
reader.read(chars);
System.out.println("Using StreamDecoder "+new String(chars).trim());

< /code>
Using new String without Decoder髙� //uses default decoder
Using Decoder 髙� // uses decoder with codingErrorAction.REPLACE
Using StreamDecoder 髙 //uses decoder with codingErrorAction.REPLACE
< /code>
StreamDecoder has no replacement character "\uFFFD"

Подробнее здесь: https://stackoverflow.com/questions/796 ... nt-seem-to

Вернуться в «JAVA»