8166339: Code conversion working behavior was changed for x-IBM834
authorsherman
Wed, 31 Jan 2018 08:42:59 -0800
changeset 48688 08b5eb52ccfd
parent 48687 d51e64840b4f
child 48689 e704f48d8277
8166339: Code conversion working behavior was changed for x-IBM834 Reviewed-by: coffeys
src/java.base/share/classes/sun/nio/cs/DoubleByte.java
test/jdk/sun/nio/cs/TestCp834_SBCS.java
--- a/src/java.base/share/classes/sun/nio/cs/DoubleByte.java	Wed Jan 31 12:04:53 2018 +0800
+++ b/src/java.base/share/classes/sun/nio/cs/DoubleByte.java	Wed Jan 31 08:42:59 2018 -0800
@@ -236,10 +236,8 @@
                         int b2 = src[sp++] & 0xff;
                         if (b2 < b2Min || b2 > b2Max ||
                             (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
-                            if (b2c[b1] == B2C_UNMAPPABLE ||  // isNotLeadingByte
-                                b2c[b2] != B2C_UNMAPPABLE ||  // isLeadingByte
-                                decodeSingle(b2) != UNMAPPABLE_DECODING) {
-                               sp--;
+                            if (crMalformedOrUnmappable(b1, b2).length() == 1) {
+                                sp--;
                             }
                         }
                     }
@@ -472,6 +470,13 @@
             b2cSB_UNMAPPABLE = new char[0x100];
             Arrays.fill(b2cSB_UNMAPPABLE, UNMAPPABLE_DECODING);
         }
+
+        // always returns unmappableForLenth(2) for doublebyte_only
+        @Override
+        protected CoderResult crMalformedOrUnmappable(int b1, int b2) {
+            return CoderResult.unmappableForLength(2);
+        }
+
         public Decoder_DBCSONLY(Charset cs, char[][] b2c, char[] b2cSB, int b2Min, int b2Max,
                                 boolean isASCIICompatible) {
             super(cs, 0.5f, 1.0f, b2c, b2cSB_UNMAPPABLE, b2Min, b2Max, isASCIICompatible);
--- a/test/jdk/sun/nio/cs/TestCp834_SBCS.java	Wed Jan 31 12:04:53 2018 +0800
+++ b/test/jdk/sun/nio/cs/TestCp834_SBCS.java	Wed Jan 31 08:42:59 2018 -0800
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 6379808
+ * @bug 6379808 8166339
  * @summary Check all Cp933 SBCS characters are not supported in Cp834
  * @modules jdk.charsets
  */
@@ -62,17 +62,34 @@
                 if ((c = cb.get()) != '\ufffd') {
                     // OK, this is a SBCS character in Cp933
                     if (dec834.decode(ByteBuffer.wrap(ba)).get() != '\ufffd')
-                        throw new Exception("SBCS is supported in IBM834 decoder");
+                        throw new RuntimeException("SBCS is supported in IBM834 decoder");
 
                     if (enc834.canEncode(c))
-                        throw new Exception("SBCS can be encoded in IBM834 encoder");
+                        throw new RuntimeException("SBCS can be encoded in IBM834 encoder");
 
                     ca[0] = c;
                     ByteBuffer bb = enc834.encode(CharBuffer.wrap(ca));
                     if (bb.get() != (byte)0xfe || bb.get() != (byte)0xfe)
-                        throw new Exception("SBCS is supported in IBM834 encoder");
+                        throw new RuntimeException("SBCS is supported in IBM834 encoder");
                 }
             }
         }
+
+        // 8166339: cp834 should handle unmappable bytes as dobule-byte pair.
+        if (! new String("\ufffd".getBytes("cp834"), "cp834").equals("\ufffd")) {
+            throw new RuntimeException("u+fffd roundtrip failed");
+        }
+
+        if (! new String("a".getBytes("cp834"), "cp834").equals("\ufffd") ||
+            ! new String(new byte[] { 0x41, 0x40}, "cp834").equals("\ufffd")) {
+            throw new RuntimeException("decoding unmappable don't return u+fffd");
+        }
+
+        CoderResult cr = Charset.forName("Cp834").newDecoder().decode(
+            ByteBuffer.wrap(new byte[] { 0x41, 0x40}), CharBuffer.wrap(new char[2]), true);
+        if (cr.isError() && cr.length() != 2) {
+            throw new RuntimeException("decoding unmappable don't return unmmappable(2)");
+        }
+
     }
 }