1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 7040220
  26    @summary Test if StringCoding and NIO result have the same de/encoding result for UTF-8
  27  * @run main/othervm/timeout=2000 TestStringCodingUTF8
  28  */
  29 
  30 import java.util.*;
  31 import java.nio.*;
  32 import java.nio.charset.*;
  33 
  34 public class TestStringCodingUTF8 {
  35     public static void main(String[] args) throws Throwable {
  36         test("UTF-8");
  37         test("CESU-8");
  38         // security manager on
  39         System.setSecurityManager(new PermissiveSecurityManger());
  40         test("UTF-8");
  41         test("CESU-8");
  42     }
  43 
  44     static void test(String csn) throws Throwable {
  45         Charset cs = Charset.forName(csn);
  46         char[] bmp = new char[0x10000];
  47         for (int i = 0; i < 0x10000; i++) {
  48             bmp[i] = (char)i;
  49         }
  50         test(cs, bmp, 0, bmp.length);
  51 
  52         ArrayList<Integer> list = new ArrayList<>(0x20000);
  53         for (int i = 0; i < 0x20000; i++) {
  54             list.add(i, i);
  55         }
  56         Collections.shuffle(list);
  57         int j = 0;
  58         char[] bmpsupp = new char[0x30000];
  59         for (int i = 0; i < 0x20000; i++) {
  60             j += Character.toChars(list.get(i), bmpsupp, j);
  61         }
  62         assert (j == bmpsupp.length);
  63         test(cs, bmpsupp, 0, bmpsupp.length);
  64 
  65         // randomed "off" and "len" on shuffled data
  66         Random rnd = new Random();
  67         int maxlen = 1000;
  68         int itr = 5000;
  69         for (int i = 0; i < itr; i++) {
  70             int off = rnd.nextInt(bmpsupp.length - maxlen);
  71             int len = rnd.nextInt(maxlen);
  72             test(cs, bmpsupp, off, len);
  73         }
  74 
  75         // random length of bytes, test the edge corner case
  76         for (int i = 0; i < itr; i++) {
  77             byte[] ba = new byte[rnd.nextInt(maxlen)];
  78             rnd.nextBytes(ba);
  79             //new String(csn);
  80             if (!new String(ba, cs.name()).equals(
  81                  new String(decode(cs, ba, 0, ba.length))))
  82                 throw new RuntimeException("new String(csn) failed");
  83             //new String(cs);
  84             if (!new String(ba, cs).equals(
  85                  new String(decode(cs, ba, 0, ba.length))))
  86                 throw new RuntimeException("new String(cs) failed");
  87         }
  88         System.out.println("done!");
  89     }
  90 
  91     static void test(Charset cs, char[] ca, int off, int len) throws Throwable {
  92         String str = new String(ca, off, len);
  93         byte[] ba = encode(cs, ca, off, len);
  94 
  95         //getBytes(csn);
  96         byte[] baStr = str.getBytes(cs.name());
  97         if (!Arrays.equals(ba, baStr))
  98             throw new RuntimeException("getBytes(csn) failed");
  99 
 100         //getBytes(cs);
 101         baStr = str.getBytes(cs);
 102         if (!Arrays.equals(ba, baStr))
 103             throw new RuntimeException("getBytes(cs) failed");
 104 
 105         //new String(csn);
 106         if (!new String(ba, cs.name()).equals(new String(decode(cs, ba, 0, ba.length))))
 107             throw new RuntimeException("new String(csn) failed");
 108 
 109         //new String(cs);
 110         if (!new String(ba, cs).equals(new String(decode(cs, ba, 0, ba.length))))
 111             throw new RuntimeException("new String(cs) failed");
 112     }
 113 
 114     // copy/paste of the StringCoding.decode()
 115     static char[] decode(Charset cs, byte[] ba, int off, int len) {
 116         CharsetDecoder cd = cs.newDecoder();
 117         int en = (int)(len * cd.maxCharsPerByte());
 118         char[] ca = new char[en];
 119         if (len == 0)
 120             return ca;
 121         cd.onMalformedInput(CodingErrorAction.REPLACE)
 122           .onUnmappableCharacter(CodingErrorAction.REPLACE)
 123           .reset();
 124 
 125         ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
 126         CharBuffer cb = CharBuffer.wrap(ca);
 127         try {
 128             CoderResult cr = cd.decode(bb, cb, true);
 129             if (!cr.isUnderflow())
 130                 cr.throwException();
 131             cr = cd.flush(cb);
 132             if (!cr.isUnderflow())
 133                 cr.throwException();
 134         } catch (CharacterCodingException x) {
 135             throw new Error(x);
 136         }
 137         return Arrays.copyOf(ca, cb.position());
 138     }
 139 
 140     // copy/paste of the StringCoding.encode()
 141     static byte[] encode(Charset cs, char[] ca, int off, int len) {
 142         CharsetEncoder ce = cs.newEncoder();
 143         int en = (int)(len * ce.maxBytesPerChar());
 144         byte[] ba = new byte[en];
 145         if (len == 0)
 146             return ba;
 147         ce.onMalformedInput(CodingErrorAction.REPLACE)
 148           .onUnmappableCharacter(CodingErrorAction.REPLACE)
 149           .reset();
 150         ByteBuffer bb = ByteBuffer.wrap(ba);
 151         CharBuffer cb = CharBuffer.wrap(ca, off, len);
 152         try {
 153             CoderResult cr = ce.encode(cb, bb, true);
 154             if (!cr.isUnderflow())
 155                 cr.throwException();
 156             cr = ce.flush(bb);
 157             if (!cr.isUnderflow())
 158                 cr.throwException();
 159         } catch (CharacterCodingException x) {
 160             throw new Error(x);
 161         }
 162         return Arrays.copyOf(ba, bb.position());
 163     }
 164 
 165     static class PermissiveSecurityManger extends SecurityManager {
 166         @Override public void checkPermission(java.security.Permission p) {}
 167     }
 168 }