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