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 8054307 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 * @key randomness 29 */ 30 31 import java.util.*; 32 import java.nio.*; 33 import java.nio.charset.*; 34 35 public class TestStringCodingUTF8 { 36 public static void main(String[] args) throws Throwable { 37 test("UTF-8"); 38 test("CESU-8"); 39 // security manager on 40 System.setSecurityManager(new PermissiveSecurityManger()); 41 test("UTF-8"); 42 test("CESU-8"); 43 } 44 45 static void test(String csn) throws Throwable { 46 Charset cs = Charset.forName(csn); 47 char[] bmp = new char[0x10000]; 48 for (int i = 0; i < 0x10000; i++) { 49 bmp[i] = (char)i; 50 } 51 test(cs, bmp, 0, bmp.length); 52 53 char[] ascii = new char[0x80]; 54 for (int i = 0; i < 0x80; i++) { 55 ascii[i] = (char)i; 59 char[] latin1 = new char[0x100]; 60 for (int i = 0; i < 0x100; i++) { 61 latin1[i] = (char)i; 62 } 63 test(cs, latin1, 0, latin1.length); 64 65 ArrayList<Integer> list = new ArrayList<>(0x20000); 66 for (int i = 0; i < 0x20000; i++) { 67 list.add(i, i); 68 } 69 Collections.shuffle(list); 70 int j = 0; 71 char[] bmpsupp = new char[0x30000]; 72 for (int i = 0; i < 0x20000; i++) { 73 j += Character.toChars(list.get(i), bmpsupp, j); 74 } 75 assert (j == bmpsupp.length); 76 test(cs, bmpsupp, 0, bmpsupp.length); 77 78 // randomed "off" and "len" on shuffled data 79 Random rnd = new Random(); 80 int maxlen = 1000; 81 int itr = 5000; 82 for (int i = 0; i < itr; i++) { 83 int off = rnd.nextInt(bmpsupp.length - maxlen); 84 int len = rnd.nextInt(maxlen); 85 test(cs, bmpsupp, off, len); 86 } 87 88 // random length of bytes, test the edge corner case 89 for (int i = 0; i < itr; i++) { 90 byte[] ba = new byte[rnd.nextInt(maxlen)]; 91 rnd.nextBytes(ba); 92 //new String(csn); 93 if (!new String(ba, cs.name()).equals( 94 new String(decode(cs, ba, 0, ba.length)))) 95 throw new RuntimeException("new String(csn) failed"); 96 //new String(cs); 97 if (!new String(ba, cs).equals( 98 new String(decode(cs, ba, 0, ba.length)))) 99 throw new RuntimeException("new String(cs) failed"); 100 } 101 System.out.println("done!"); 102 } 103 104 static void test(Charset cs, char[] ca, int off, int len) throws Throwable { 105 String str = new String(ca, off, len); 106 byte[] ba = encode(cs, ca, off, len); 107 108 //getBytes(csn); 109 byte[] baStr = str.getBytes(cs.name()); 110 if (!Arrays.equals(ba, baStr)) 111 throw new RuntimeException("getBytes(csn) failed"); 112 113 //getBytes(cs); 114 baStr = str.getBytes(cs); 115 if (!Arrays.equals(ba, baStr)) 116 throw new RuntimeException("getBytes(cs) failed"); 117 118 //new String(csn); 119 if (!new String(ba, cs.name()).equals(new String(decode(cs, ba, 0, ba.length)))) 120 throw new RuntimeException("new String(csn) failed"); 121 122 //new String(cs); 123 if (!new String(ba, cs).equals(new String(decode(cs, ba, 0, ba.length)))) 124 throw new RuntimeException("new String(cs) failed"); 125 } 126 127 // copy/paste of the StringCoding.decode() 128 static char[] decode(Charset cs, byte[] ba, int off, int len) { 129 CharsetDecoder cd = cs.newDecoder(); 130 int en = (int)(len * cd.maxCharsPerByte()); 131 char[] ca = new char[en]; 132 if (len == 0) 133 return ca; 134 cd.onMalformedInput(CodingErrorAction.REPLACE) 135 .onUnmappableCharacter(CodingErrorAction.REPLACE) 136 .reset(); 137 138 ByteBuffer bb = ByteBuffer.wrap(ba, off, len); 139 CharBuffer cb = CharBuffer.wrap(ca); 140 try { 141 CoderResult cr = cd.decode(bb, cb, true); 142 if (!cr.isUnderflow()) 143 cr.throwException(); 144 cr = cd.flush(cb); | 1 /* 2 * Copyright (c) 2011, 2018, 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 8054307 8021560 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 * @key randomness 29 */ 30 31 import java.util.*; 32 import java.nio.*; 33 import java.nio.charset.*; 34 35 public class TestStringCodingUTF8 { 36 37 private static Random rnd = new Random(); 38 39 public static void main(String[] args) throws Throwable { 40 test("UTF-8"); 41 test("CESU-8"); 42 // security manager on 43 System.setSecurityManager(new PermissiveSecurityManger()); 44 test("UTF-8"); 45 test("CESU-8"); 46 } 47 48 static void test(String csn) throws Throwable { 49 Charset cs = Charset.forName(csn); 50 char[] bmp = new char[0x10000]; 51 for (int i = 0; i < 0x10000; i++) { 52 bmp[i] = (char)i; 53 } 54 test(cs, bmp, 0, bmp.length); 55 56 char[] ascii = new char[0x80]; 57 for (int i = 0; i < 0x80; i++) { 58 ascii[i] = (char)i; 62 char[] latin1 = new char[0x100]; 63 for (int i = 0; i < 0x100; i++) { 64 latin1[i] = (char)i; 65 } 66 test(cs, latin1, 0, latin1.length); 67 68 ArrayList<Integer> list = new ArrayList<>(0x20000); 69 for (int i = 0; i < 0x20000; i++) { 70 list.add(i, i); 71 } 72 Collections.shuffle(list); 73 int j = 0; 74 char[] bmpsupp = new char[0x30000]; 75 for (int i = 0; i < 0x20000; i++) { 76 j += Character.toChars(list.get(i), bmpsupp, j); 77 } 78 assert (j == bmpsupp.length); 79 test(cs, bmpsupp, 0, bmpsupp.length); 80 81 // randomed "off" and "len" on shuffled data 82 int maxlen = 1000; 83 int itr = 5000; 84 for (int i = 0; i < itr; i++) { 85 int off = rnd.nextInt(bmpsupp.length - maxlen); 86 int len = rnd.nextInt(maxlen); 87 test(cs, bmpsupp, off, len); 88 } 89 90 // random length of bytes, test the edge corner case 91 for (int i = 0; i < itr; i++) { 92 byte[] ba = new byte[rnd.nextInt(maxlen)]; 93 rnd.nextBytes(ba); 94 //new String(csn); 95 if (!new String(ba, cs.name()).equals( 96 new String(decode(cs, ba, 0, ba.length)))) 97 throw new RuntimeException("new String(csn) failed"); 98 //new String(cs); 99 if (!new String(ba, cs).equals( 100 new String(decode(cs, ba, 0, ba.length)))) 101 throw new RuntimeException("new String(cs) failed"); 102 } 103 System.out.println("done!"); 104 } 105 106 static void assertEquals(int len1, int len2, String msg) { 107 if (len1 != len2) 108 throw new RuntimeException("FAILED: " + msg); 109 } 110 111 static void assertEquals(byte[] a, int afrom, int ato, 112 byte[] b, int bfrom, int bto, 113 String msg) { 114 if (!Arrays.equals(a, afrom, ato, b, bfrom, bto)) { 115 throw new RuntimeException("FAILED: " + msg); 116 } 117 } 118 119 static void assertEquals(ByteBuffer bb, int pos, byte[] b, int from, int to, 120 byte[] buf, String msg) { 121 bb.flip().position(pos); 122 int len = bb.remaining(); 123 bb.get(buf, 0, len); 124 if (!Arrays.equals(buf, 0, len, b, from, to)) { 125 throw new RuntimeException("FAILED: " + msg); 126 } 127 } 128 129 static void test(Charset cs, char[] ca, int off, int len) throws Throwable { 130 String str = new String(ca, off, len); 131 byte[] ba = encode(cs, ca, off, len); 132 String csn = cs.name(); 133 134 //getBytes(csn); 135 byte[] baStr = str.getBytes(csn); 136 if (!Arrays.equals(ba, baStr)) 137 throw new RuntimeException("getBytes(csn) failed"); 138 139 //getBytes(cs); 140 baStr = str.getBytes(cs); 141 if (!Arrays.equals(ba, baStr)) 142 throw new RuntimeException("getBytes(cs) failed"); 143 144 145 byte[] buf = new byte[ba.length + 10]; 146 int shortenLen = Math.max(0, ba.length - 10); 147 int slen = str.length(); 148 149 // getBytes(ByteBuffer, cs); 150 ByteBuffer bb = ByteBuffer.wrap(buf); 151 int n = str.getBytes(0, slen, bb, cs); 152 int pos = bb.position(); 153 assertEquals(n, slen, "getBytes(bb cs)"); 154 assertEquals(buf, 0, pos, ba, 0, ba.length, "getBytes(bb, cs)"); 155 156 bb.clear().position(3); 157 n = str.getBytes(0, str.length(), bb, cs); 158 pos = bb.position(); 159 assertEquals(n, slen, "getBytes(bb, cs)"); 160 assertEquals(buf, 3, pos, ba, 0, ba.length, "getBytes(bb/3, cs)"); 161 162 bb.clear().limit(shortenLen); 163 n = str.getBytes(0, slen, bb, cs); 164 pos = bb.position(); 165 assertEquals(buf, 0, pos, ba, 0, pos, "getBytes(bb, cs)/shortenLen"); 166 167 // loop 168 off = 0; 169 int m = 0; 170 bb.clear(); 171 while (off < slen) { 172 int nn = slen - off; 173 if (m++ < 10 && nn > 10) { 174 nn = rnd.nextInt(nn); 175 } 176 n = str.getBytes(off, off + nn, bb, cs); 177 off += n; 178 } 179 bb.flip(); 180 if (!new String(bb, cs).equals(new String(ba, cs))) { 181 182 bb.rewind(); 183 String s1 = new String(bb, cs); 184 String s2 = new String(ba, cs); 185 186 System.out.printf(" bb.str.len=%d, nio.str.len=%d%n", s1.length(), s2.length()); 187 188 for (int i = 0 ;i < s1.length(); i++) { 189 if (s1.charAt(i) != s2.charAt(i)) { 190 System.out.printf(" [%d] %x %x%n", i, s1.charAt(i) & 0xffff, s2.charAt(i) & 0xffff); 191 } 192 } 193 throw new RuntimeException("-----------"); 194 } 195 196 // getBytes(ByteBuffer/direct, cs); 197 bb = ByteBuffer.allocateDirect(buf.length); 198 n = str.getBytes(0, slen, bb, cs); 199 pos = bb.position(); 200 201 // assertEquals(n, ba.length, "getBytes(bb cs)"); 202 assertEquals(bb, 0, ba, 0, ba.length, buf, "getBytes(bb, cs)"); 203 204 bb.clear().position(3); 205 n = str.getBytes(0, slen, bb, cs); 206 pos = bb.position(); 207 // assertEquals(n, ba.length, "getBytes(bb, cs)"); 208 assertEquals(bb, 3, ba, 0, ba.length, buf, "getBytes(bb/3, cs)"); 209 210 bb.clear().limit(shortenLen); 211 n = str.getBytes(0, slen, bb, cs); 212 pos = bb.position(); 213 assertEquals(bb, 0, ba, 0, pos, buf, "getBytes(bb, cs)/shortenLen"); 214 215 216 217 ////////////////////////////////////////////////////////////// 218 219 String expected = new String(decode(cs, ba, 0, ba.length)); 220 221 //new String(csn); 222 if (!expected.equals(new String(ba, csn))) 223 throw new RuntimeException("new String(csn) failed"); 224 225 //new String(cs); 226 if (!expected.equals(new String(ba, cs))) 227 throw new RuntimeException("new String(cs) failed"); 228 229 //new String(bytebuffer, cs); 230 bb = ByteBuffer.allocate(ba.length + 10); 231 bb.put(ba); 232 bb.flip(); 233 if (!expected.equals(new String(bb, cs))) { 234 throw new RuntimeException("new String(bb, cs) failed"); 235 } 236 237 bb.clear().position(3).put(ba).flip(); 238 if (!expected.equals(new String(bb.position(3), cs))) { 239 throw new RuntimeException("new String(cs)/pos=3 failed"); 240 } 241 242 //new String(bytebuffer/direct, cs); 243 bb = ByteBuffer.allocateDirect(ba.length + 10); 244 bb.put(ba); 245 bb.flip(); 246 if (!expected.equals(new String(bb, cs))) { 247 throw new RuntimeException("new String(cs)/bmp failed"); 248 } 249 250 bb.clear().position(3).put(ba).flip(); 251 if (!expected.equals(new String(bb.position(3), cs))) { 252 throw new RuntimeException("new String(cs)/pos=3 failed"); 253 } 254 } 255 256 // copy/paste of the StringCoding.decode() 257 static char[] decode(Charset cs, byte[] ba, int off, int len) { 258 CharsetDecoder cd = cs.newDecoder(); 259 int en = (int)(len * cd.maxCharsPerByte()); 260 char[] ca = new char[en]; 261 if (len == 0) 262 return ca; 263 cd.onMalformedInput(CodingErrorAction.REPLACE) 264 .onUnmappableCharacter(CodingErrorAction.REPLACE) 265 .reset(); 266 267 ByteBuffer bb = ByteBuffer.wrap(ba, off, len); 268 CharBuffer cb = CharBuffer.wrap(ca); 269 try { 270 CoderResult cr = cd.decode(bb, cb, true); 271 if (!cr.isUnderflow()) 272 cr.throwException(); 273 cr = cd.flush(cb); |