test/jdk/sun/nio/cs/TestStringCodingUTF8.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -20,21 +20,24 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /* @test
-   @bug 7040220 8054307
+   @bug 7040220 8054307 8021560
    @summary Test if StringCoding and NIO result have the same de/encoding result for UTF-8
  * @run main/othervm/timeout=2000 TestStringCodingUTF8
  * @key randomness
  */
 
 import java.util.*;
 import java.nio.*;
 import java.nio.charset.*;
 
 public class TestStringCodingUTF8 {
+
+    private static Random rnd = new Random();
+
     public static void main(String[] args) throws Throwable {
         test("UTF-8");
         test("CESU-8");
         // security manager on
         System.setSecurityManager(new PermissiveSecurityManger());

@@ -74,11 +77,10 @@
         }
         assert (j == bmpsupp.length);
         test(cs, bmpsupp, 0, bmpsupp.length);
 
         // randomed "off" and "len" on shuffled data
-        Random rnd = new Random();
         int maxlen = 1000;
         int itr = 5000;
         for (int i = 0; i < itr; i++) {
             int off = rnd.nextInt(bmpsupp.length - maxlen);
             int len = rnd.nextInt(maxlen);

@@ -99,31 +101,158 @@
                 throw new RuntimeException("new String(cs) failed");
         }
         System.out.println("done!");
     }
 
+    static void assertEquals(int len1, int len2, String msg) {
+        if (len1 != len2)
+            throw new RuntimeException("FAILED: " + msg);
+    }
+
+    static void assertEquals(byte[] a, int afrom, int ato,
+                             byte[] b, int bfrom, int bto,
+                             String msg) {
+        if (!Arrays.equals(a, afrom, ato, b, bfrom, bto)) {
+            throw new RuntimeException("FAILED: " + msg);
+        }
+    }
+
+    static void assertEquals(ByteBuffer bb, int pos, byte[] b, int from, int to,
+                             byte[] buf, String msg) {
+        bb.flip().position(pos);
+        int len = bb.remaining();
+        bb.get(buf, 0, len);
+        if (!Arrays.equals(buf, 0, len, b, from, to)) {
+            throw new RuntimeException("FAILED: " + msg);
+        }
+    }
+
     static void test(Charset cs, char[] ca, int off, int len) throws Throwable {
         String str = new String(ca, off, len);
         byte[] ba = encode(cs, ca, off, len);
+        String csn = cs.name();
 
         //getBytes(csn);
-        byte[] baStr = str.getBytes(cs.name());
+        byte[] baStr = str.getBytes(csn);
         if (!Arrays.equals(ba, baStr))
             throw new RuntimeException("getBytes(csn) failed");
 
         //getBytes(cs);
         baStr = str.getBytes(cs);
         if (!Arrays.equals(ba, baStr))
             throw new RuntimeException("getBytes(cs) failed");
 
+
+        byte[] buf = new byte[ba.length + 10];
+        int shortenLen = Math.max(0, ba.length - 10);
+        int slen = str.length();
+
+        // getBytes(ByteBuffer, cs);
+        ByteBuffer bb = ByteBuffer.wrap(buf);
+        int n = str.getBytes(0, slen, bb, cs);
+        int pos = bb.position();
+        assertEquals(n, slen, "getBytes(bb cs)");
+        assertEquals(buf, 0, pos, ba, 0, ba.length, "getBytes(bb, cs)");
+
+        bb.clear().position(3);
+        n = str.getBytes(0, str.length(), bb, cs);
+        pos = bb.position();
+        assertEquals(n, slen, "getBytes(bb, cs)");
+        assertEquals(buf, 3, pos, ba, 0, ba.length, "getBytes(bb/3, cs)");
+
+        bb.clear().limit(shortenLen);
+        n = str.getBytes(0, slen, bb, cs);
+        pos = bb.position();
+        assertEquals(buf, 0, pos, ba, 0, pos, "getBytes(bb, cs)/shortenLen");
+
+        // loop
+        off = 0;
+        int m = 0;
+        bb.clear();
+        while (off < slen) {
+            int nn = slen - off;
+            if (m++ < 10 && nn > 10) {
+                nn = rnd.nextInt(nn);
+            }
+            n = str.getBytes(off, off + nn, bb, cs);
+            off += n;
+        }
+        bb.flip();
+        if (!new String(bb, cs).equals(new String(ba, cs))) {
+
+bb.rewind();
+String s1 = new String(bb, cs);
+String s2 = new String(ba, cs);
+
+System.out.printf("  bb.str.len=%d, nio.str.len=%d%n",  s1.length(), s2.length());
+
+for (int i = 0 ;i < s1.length(); i++) {
+    if (s1.charAt(i) != s2.charAt(i)) {
+        System.out.printf(" [%d] %x   %x%n", i, s1.charAt(i) & 0xffff, s2.charAt(i) & 0xffff);
+    }
+}
+throw new RuntimeException("-----------");
+        }
+
+        // getBytes(ByteBuffer/direct, cs);
+        bb = ByteBuffer.allocateDirect(buf.length);
+        n = str.getBytes(0, slen, bb, cs);
+        pos = bb.position();
+
+        // assertEquals(n, ba.length, "getBytes(bb cs)");
+        assertEquals(bb, 0, ba, 0, ba.length, buf, "getBytes(bb, cs)");
+
+        bb.clear().position(3);
+        n = str.getBytes(0, slen, bb, cs);
+        pos = bb.position();
+        // assertEquals(n, ba.length, "getBytes(bb, cs)");
+        assertEquals(bb, 3, ba, 0, ba.length, buf, "getBytes(bb/3, cs)");
+
+        bb.clear().limit(shortenLen);
+        n = str.getBytes(0, slen, bb, cs);
+        pos = bb.position();
+        assertEquals(bb, 0, ba, 0, pos, buf, "getBytes(bb, cs)/shortenLen");
+
+
+
+        //////////////////////////////////////////////////////////////
+
+        String expected = new String(decode(cs, ba, 0, ba.length));
+
         //new String(csn);
-        if (!new String(ba, cs.name()).equals(new String(decode(cs, ba, 0, ba.length))))
+        if (!expected.equals(new String(ba, csn)))
             throw new RuntimeException("new String(csn) failed");
 
         //new String(cs);
-        if (!new String(ba, cs).equals(new String(decode(cs, ba, 0, ba.length))))
+        if (!expected.equals(new String(ba, cs)))
             throw new RuntimeException("new String(cs) failed");
+
+        //new String(bytebuffer, cs);
+        bb = ByteBuffer.allocate(ba.length + 10);
+        bb.put(ba);
+        bb.flip();
+        if (!expected.equals(new String(bb, cs))) {
+            throw new RuntimeException("new String(bb, cs) failed");
+        }
+
+        bb.clear().position(3).put(ba).flip();
+        if (!expected.equals(new String(bb.position(3), cs))) {
+            throw new RuntimeException("new String(cs)/pos=3 failed");
+        }
+
+        //new String(bytebuffer/direct, cs);
+        bb = ByteBuffer.allocateDirect(ba.length + 10);
+        bb.put(ba);
+        bb.flip();
+        if (!expected.equals(new String(bb, cs))) {
+            throw new RuntimeException("new String(cs)/bmp failed");
+        }
+
+        bb.clear().position(3).put(ba).flip();
+        if (!expected.equals(new String(bb.position(3), cs))) {
+            throw new RuntimeException("new String(cs)/pos=3 failed");
+        }
     }
 
     // copy/paste of the StringCoding.decode()
     static char[] decode(Charset cs, byte[] ba, int off, int len) {
         CharsetDecoder cd = cs.newDecoder();