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

(cs) CharsetEncoder.encode(ByteBuffer) should call flush(ByteBuffer)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 6
    • 1.4.2, 5.0, 5.0u21
    • core-libs
    • b53
    • generic, x86
    • generic, windows_xp

         

        Attached JISTest.java to reproduce NIO problem.

        On my environment Java(TM) 2 Runtime Environment, Standard Edition (build

        1.5.0-b64), i got the following result.

         

        Original:

        0000: 1B 24 42 46 7C 4B 5C 38 6C 1B 28 42

         

        Decoded and Encoded by NIO:

        0000: 1B 24 42 46 7C 4B 5C 38 6C 00 00 00

         

        This result means that NIO iso-2022-jp converter does not add

        the escape sequence '1B 28 42' to the end of japanese characters

        '46 7C 4B 5C 38 6C', just fills '00 00 00'.

        According to iso-2022-jp specification(rfc1468), the escape

        sequence '1B 28 42', i.e. 'ESC ( B' is used to switch back to

        ASCII mode. and so, '00 00 00' is still handled japanese characters,

        then is interpreted broken japanese characters.
        ###@###.### 2005-1-25 01:51:29 GMT

        Here is a test case
        =================================JISTest.java=========
        import java.nio.*;
        import java.nio.charset.*;
        import sun.misc.HexDumpEncoder;

        public class JISTest {

          public static void main(String args[]) {

            HexDumpEncoder dumper = new HexDumpEncoder();

            String jisx0208 = "\u65e5\u672c\u8a9e";

            try{
              byte[] b = jisx0208.getBytes("ISO-2022-JP");
              System.out.println("Original: ");
              System.out.println(dumper.encode(b));

              System.out.println("");

              System.out.println("Decoded and Encoded by NIO: ");
              Charset iso2022jp = Charset.forName("ISO-2022-JP");
              CharsetDecoder decoder = iso2022jp.newDecoder();
              ByteBuffer jpBytes = ByteBuffer.wrap(b);
              CharBuffer jpChars = decoder.decode(jpBytes);
              CharsetEncoder encoder = iso2022jp.newEncoder();
              ByteBuffer jpBytes2 = encoder.encode(jpChars);
              System.out.println(dumper.encode(jpBytes2.array()));

            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }

        ###@###.### 2005-1-26 00:47:21 GMT

              martin Martin Buchholz
              mmma Marvin Ma (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: