6730652: CharsetEncoder.canEncode(char) returns incorrect values for some charsets
authorsherman
Fri, 14 Aug 2009 14:29:45 -0700
changeset 3623 4e71b4e83158
parent 3622 9676ac8a9bf1
child 3624 e214b718aeef
6730652: CharsetEncoder.canEncode(char) returns incorrect values for some charsets Summary: override the canEncode() in ISO2022_CN_CNS Reviewed-by: martin
jdk/src/share/classes/sun/nio/cs/ext/ISO2022.java
jdk/src/share/classes/sun/nio/cs/ext/ISO2022_CN_CNS.java
jdk/test/sun/nio/cs/FindCanEncodeBugs.java
--- a/jdk/src/share/classes/sun/nio/cs/ext/ISO2022.java	Fri Aug 14 11:23:01 2009 -0700
+++ b/jdk/src/share/classes/sun/nio/cs/ext/ISO2022.java	Fri Aug 14 14:29:45 2009 -0700
@@ -388,9 +388,9 @@
 
     protected static class Encoder extends CharsetEncoder {
         private final Surrogate.Parser sgp = new Surrogate.Parser();
-        private final byte SS2 = (byte)0x8e;
-        private final byte PLANE2 = (byte)0xA2;
-        private final byte PLANE3 = (byte)0xA3;
+        public static final byte SS2 = (byte)0x8e;
+        public static final byte PLANE2 = (byte)0xA2;
+        public static final byte PLANE3 = (byte)0xA3;
         private final byte MSB = (byte)0x80;
 
         protected final byte maximumDesignatorLength = 4;
--- a/jdk/src/share/classes/sun/nio/cs/ext/ISO2022_CN_CNS.java	Fri Aug 14 11:23:01 2009 -0700
+++ b/jdk/src/share/classes/sun/nio/cs/ext/ISO2022_CN_CNS.java	Fri Aug 14 14:29:45 2009 -0700
@@ -76,6 +76,15 @@
             } catch (Exception e) { }
         }
 
+        private byte[] bb = new byte[4];
+        public boolean canEncode(char c) {
+            int n = 0;
+            return (c <= '\u007f' ||
+                    (n = ((EUC_TW.Encoder)ISOEncoder).toEUC(c, bb)) == 2 ||
+                    (n == 4 && bb[0] == SS2 &&
+                     (bb[1] == PLANE2 || bb[1] == PLANE3)));
+        }
+
         /*
          * Since ISO2022-CN-CNS possesses a CharsetEncoder
          * without the corresponding CharsetDecoder half the
--- a/jdk/test/sun/nio/cs/FindCanEncodeBugs.java	Fri Aug 14 11:23:01 2009 -0700
+++ b/jdk/test/sun/nio/cs/FindCanEncodeBugs.java	Fri Aug 14 14:29:45 2009 -0700
@@ -22,7 +22,7 @@
  */
 
 /* @test
-   @bug 5066863 5066867 5066874 5066879 5066884 5066887 5065777
+   @bug 5066863 5066867 5066874 5066879 5066884 5066887 5065777 6730652
    @summary canEncode() false iff encode() throws CharacterCodingException
    @run main/timeout=1200 FindCanEncodeBugs
    @author Martin Buchholz
@@ -52,9 +52,7 @@
             String csn = e.getKey();
             Charset cs = e.getValue();
 
-            if (! cs.canEncode() ||
-                csn.matches("x-COMPOUND_TEXT")   ||
-                csn.matches("x-ISO-2022-CN-CNS"))  // ISO2022_CN_CNS supports less
+            if (! cs.canEncode() || csn.matches("x-COMPOUND_TEXT"))
                 continue;
 
             //System.out.println(csn);