Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8361936

StreamDecoder doesn't seem to honor CodingErrorAction.REPLACE

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Linux Unix-Workspace 5.15.0-125-generic #135~20.04.1-Ubuntu

      A DESCRIPTION OF THE PROBLEM :

      A CharsetDecoder is explicitly configured with CodingErrorAction.REPLACE ; I would expect the replacement character \uFFFD to be applied when decoding malformed input .

      On using StreamDecoder the replacement Character is not applied on malformed input, especially when it is towards the end of the byte stream.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Using new String without Decoder髙�
      Using Decoder 髙�
      Using StreamDecoder 髙� //expected replacement character when byte (27) is read
      ACTUAL -
      Using new String without Decoder髙�
      Using Decoder 髙�
      Using StreamDecoder 髙 // StreamDecoder has no replacement character "\uFFFD"

      ---------- BEGIN SOURCE ----------
      import java.nio.charset.Charset;
      import java.nio.charset.CharsetDecoder;
      import java.nio.charset.CodingErrorAction;
      import java.nio.ByteBuffer;
      import java.io.Reader;
      import java.io.InputStreamReader;
      import java.io.ByteArrayInputStream;

      public class Test {

          public static void main(String [] args) throws Exception{

              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());
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: