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

ISO-2022-JP encoder doesn't output tail escape sequence

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.4.2_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
      Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      Problems faced is described below
      1) flush() and close() methods of the OutputStreamWriter are inconsistent
      2) encode(CharBuffer in) method in the CharsetEncoder is misbehaving.

      CharsetEncoder#flush wasn't call appropriate in OutputStreamWriter#flush and in CharsetEncoder#encode(CharBuffer) (?).

      test code below.

      import java.io.*;
      import java.nio.*;
      import java.nio.charset.*;

      class Test {
          public static void main(String[] args) throws Exception {
              String m = "\u3042\u3043\u3044";
              System.out.println("expect result was:");
              System.out.println("1B 24 42 24 22 24 23 24 24 1B 28 42");
              System.out.println();

              System.out.println("test getBytes");
              System.out.println(dump(m.getBytes("ISO-2022-JP"))); // OK
              System.out.println();

              System.out.println("test OutputStreamWriter with close");
              ByteArrayOutputStream b = new ByteArrayOutputStream();
              OutputStreamWriter w = new OutputStreamWriter(b, "ISO-2022-JP");
              w.write(m.toCharArray());
              w.close();
              System.out.println(dump(b.toByteArray())); // OK
              System.out.println();

              System.out.println("test OutputStreamWriter with flush");
              b = new ByteArrayOutputStream();
              w = new OutputStreamWriter(b, "ISO-2022-JP");
              w.write(m.toCharArray());
              w.flush();
              System.out.println(dump(b.toByteArray())); // NG: not call CharsetEncoder#flush
              System.out.println();

              System.out.println("test CharsetEncoder#encode(CharBuffer)");
              CharsetEncoder e = Charset.forName("ISO-2022-JP").newEncoder();
              ByteBuffer buf = e.encode(CharBuffer.wrap(m));
              System.out.println(dump(buf.array())); // NG
              System.out.println();

              System.out.println("test CharsetEncoder#encode(CharBuffer, ByteBuffer, boolean)");
              /*
              e.reset(); // don't work(another bug in sun.nio.cs.ext.ISO2022_JP#reset)
              /*/
              e = Charset.forName("ISO-2022-JP").newEncoder();
              //*/
              buf = ByteBuffer.allocate(64);
              System.out.println(e.encode(CharBuffer.wrap(m), buf, true));
              System.out.println(e.flush(buf)); // <- required
              System.out.println(dump(buf.array())); // OK
          }


          public static String dump(byte[] bytes) {
              StringBuffer s = new StringBuffer();
              for (int i = 0; i < bytes.length; i++) {
                  s.append("0123456789ABCDEF".charAt(bytes[i] >> 4 & 0x0f));
                  s.append("0123456789ABCDEF".charAt(bytes[i] & 0x0f));
                  s.append(' ');
              }
              return new String(s);
          }
      }


      P.S. CharsetEncoder#reset() doesn't work at ISO-2022-JP encoder.



      REPRODUCIBILITY :
      This bug can be reproduced always.
      ###@###.### 2004-12-21 11:13:14 GMT

            martin Martin Buchholz
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: