/* * Copyright (c) 2011, 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. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test @bug 7040220 8054307 @summary Test if StringCoding and NIO result have the same de/encoding result for UTF-8 * @run main/othervm/timeout=2000 -Djava.security.manager=allow TestStringCodingUTF8 * @key randomness
*/
char[] ascii = newchar[0x80]; for (int i = 0; i < 0x80; i++) {
ascii[i] = (char)i;
}
test(cs, ascii, 0, ascii.length);
char[] latin1 = newchar[0x100]; for (int i = 0; i < 0x100; i++) {
latin1[i] = (char)i;
}
test(cs, latin1, 0, latin1.length);
ArrayList<Integer> list = new ArrayList<>(0x20000); for (int i = 0; i < 0x20000; i++) {
list.add(i, i);
}
Collections.shuffle(list); int j = 0; char[] bmpsupp = newchar[0x30000]; for (int i = 0; i < 0x20000; i++) {
j += Character.toChars(list.get(i), bmpsupp, j);
} 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);
test(cs, bmpsupp, off, len);
}
// random length of bytes, test the edge corner case for (int i = 0; i < itr; i++) { byte[] ba = newbyte[rnd.nextInt(maxlen)];
rnd.nextBytes(ba); //new String(csn); if (!new String(ba, cs.name()).equals( new String(decode(cs, ba, 0, ba.length)))) thrownew RuntimeException("new String(csn) failed"); //new String(cs); if (!new String(ba, cs).equals( new String(decode(cs, ba, 0, ba.length)))) thrownew RuntimeException("new String(cs) failed");
}
System.out.println("done!");
}
staticvoid 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);
// copy/paste of the StringCoding.decode() staticchar[] decode(Charset cs, byte[] ba, int off, int len) {
CharsetDecoder cd = cs.newDecoder(); int en = (int)(len * cd.maxCharsPerByte()); char[] ca = newchar[en]; if (len == 0) return ca;
cd.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.reset();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.