test/sun/nio/cs/TestStringCoding.java

Print this page

        

*** 22,32 **** * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! @bug 6636323 6636319 7040220 7096080 @summary Test if StringCoding and NIO result have the same de/encoding result * @run main/othervm/timeout=2000 TestStringCoding */ import java.util.*; --- 22,32 ---- * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! @bug 6636323 6636319 7040220 7096080 7183053 @summary Test if StringCoding and NIO result have the same de/encoding result * @run main/othervm/timeout=2000 TestStringCoding */ import java.util.*;
*** 68,82 **** --- 68,133 ---- bmpCA[pos] = '\uD800'; bmpCA[pos+1] = '\uDC00'; } test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen)); } + + testMixed(cs); System.out.println("done!"); } } } + static void testMixed(Charset cs) throws Throwable { + CharsetDecoder dec = cs.newDecoder() + .onMalformedInput(CodingErrorAction.REPLACE) + .onUnmappableCharacter(CodingErrorAction.REPLACE); + CharsetEncoder enc = cs.newEncoder() + .onMalformedInput(CodingErrorAction.REPLACE) + .onUnmappableCharacter(CodingErrorAction.REPLACE); + List<Integer> cps = new ArrayList<>(0x10000); + int off = 0; + int cp = 0; + while (cp < 0x10000) { + if (enc.canEncode((char)cp)) { + cps.add(cp); + } + cp++; + } + Collections.shuffle(cps); + char[] bmpCA = new char[cps.size()]; + for (int i = 0; i < cps.size(); i++) + bmpCA[i] = (char)(int)cps.get(i); + String bmpStr = new String(bmpCA); + //getBytes(csn); + byte[] bmpBA = bmpStr.getBytes(cs.name()); + ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(bmpCA)); + byte[] baNIO = new byte[bf.limit()]; + bf.get(baNIO, 0, baNIO.length); + if (!Arrays.equals(bmpBA, baNIO)) { + throw new RuntimeException("getBytes(csn) failed -> " + cs.name()); + } + + //getBytes(cs); + bmpBA = bmpStr.getBytes(cs); + if (!Arrays.equals(bmpBA, baNIO)) + throw new RuntimeException("getBytes(cs) failed -> " + cs.name()); + + //new String(csn); + String strSC = new String(bmpBA, cs.name()); + String strNIO = dec.reset().decode(ByteBuffer.wrap(bmpBA)).toString(); + if(!strNIO.equals(strSC)) { + throw new RuntimeException("new String(csn) failed -> " + cs.name()); + } + + //new String(cs); + strSC = new String(bmpBA, cs); + if (!strNIO.equals(strSC)) + throw new RuntimeException("new String(cs) failed -> " + cs.name()); + + } + static void test(Charset cs, char[] bmpCA, byte[] sbBA) throws Throwable { String bmpStr = new String(bmpCA); CharsetDecoder dec = cs.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE);
*** 98,107 **** --- 149,159 ---- throw new RuntimeException("getBytes(cs) failed -> " + cs.name()); //new String(csn); String strSC = new String(sbBA, cs.name()); String strNIO = dec.reset().decode(ByteBuffer.wrap(sbBA)).toString(); + if(!strNIO.equals(strSC)) throw new RuntimeException("new String(csn) failed -> " + cs.name()); //new String(cs); strSC = new String(sbBA, cs);
*** 135,151 **** n = cae.encode(str.toCharArray(), 0, str.length(), ba); if (n != 8 || !"abABABcd".equals(new String(ba, 0, n, cs.name()))) throw new RuntimeException("encode3(surrogates) failed -> " + cs.name()); ba = new byte[str.length() - 1]; n = cae.encode(str.toCharArray(), 0, str.length(), ba); ! if (n != 7 || !"abABABc".equals(new String(ba, 0, n, ! cs.name()))) throw new RuntimeException("encode4(surrogates) failed -> " + cs.name()); } } static class PermissiveSecurityManger extends SecurityManager { @Override public void checkPermission(java.security.Permission p) {} --- 187,207 ---- n = cae.encode(str.toCharArray(), 0, str.length(), ba); if (n != 8 || !"abABABcd".equals(new String(ba, 0, n, cs.name()))) throw new RuntimeException("encode3(surrogates) failed -> " + cs.name()); + /* sun.nio.cs.ArrayDeEncoder works on the assumption that the + invoker (StringCoder) allocates enough output buf, utf8 + and double-byte coder does not check the output buffer limit. ba = new byte[str.length() - 1]; n = cae.encode(str.toCharArray(), 0, str.length(), ba); ! if (n != 7 || !"abABABc".equals(new String(ba, 0, n, cs.name()))) { throw new RuntimeException("encode4(surrogates) failed -> " + cs.name()); } + */ + } } static class PermissiveSecurityManger extends SecurityManager { @Override public void checkPermission(java.security.Permission p) {}