12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.DataInputStream;
29 import java.io.InputStream;
30 import java.lang.ref.SoftReference;
31 import java.util.Arrays;
32 import java.util.Locale;
33 import java.util.zip.InflaterInputStream;
34 import java.security.AccessController;
35 import java.security.PrivilegedAction;
36
37 class CharacterName {
38
39 private static SoftReference<CharacterName> refCharName;
40
41 // codepoint -> bkIndex -> lookup -> offset/len
42 private final byte[] strPool;
43 private final int[] lookup; // code point -> offset/len in strPool
44 private final int[] bkIndices; // code point -> lookup index
45
46 // name -> hash -> hsIndices -> cpEntries -> code point
47 private final int[] cpEntries; // code points that have name in strPool
48 private final int[] hsIndices; // chain heads, hash indices into "cps"
49
50 private CharacterName() {
51 try (DataInputStream dis = new DataInputStream(new InflaterInputStream(
52 AccessController.doPrivileged(new PrivilegedAction<>() {
53 public InputStream run() {
54 return getClass().getResourceAsStream("uniName.dat");
55 }
56 })))) {
134 SoftReference<CharacterName> ref = refCharName;
135 CharacterName cname = null;
136 if (ref == null || (cname = ref.get()) == null) {
137 cname = new CharacterName();
138 refCharName = new SoftReference<>(cname);
139 }
140 return cname;
141 }
142
143 public String getName(int cp) {
144 int off = 0;
145 int bk = bkIndices[cp >> 8];
146 if (bk == -1 || (off = lookup[(bk << 8) + (cp & 0xff)]) == 0)
147 return null;
148 @SuppressWarnings("deprecation")
149 String result = new String(strPool, 0, off >>> 8, off & 0xff); // ASCII
150 return result;
151 }
152
153 public int getCodePoint(String name) {
154 byte[] bname = name.getBytes(java.nio.charset.StandardCharsets.ISO_8859_1);
155 int hsh = hashN(bname, 0, bname.length);
156 int idx = hsIndices[(hsh & 0x7fffffff) % hsIndices.length];
157 while (idx != -1) {
158 if (getCpHash(idx) == hsh) {
159 int cp = getCp(idx);
160 int off = -1;
161 int bk = bkIndices[cp >> 8];
162 if (bk != -1 && (off = lookup[(bk << 8) + (cp & 0xff)]) != 0) {
163 int len = off & 0xff;
164 off = off >>> 8;
165 if (bname.length == len) {
166 int i = 0;
167 while (i < len && bname[i] == strPool[off++]) {
168 i++;
169 }
170 if (i == len) {
171 return cp;
172 }
173 }
174 }
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.DataInputStream;
29 import java.io.InputStream;
30 import java.lang.ref.SoftReference;
31 import java.util.Arrays;
32 import java.util.zip.InflaterInputStream;
33 import java.security.AccessController;
34 import java.security.PrivilegedAction;
35
36 import sun.nio.cs.ISO_8859_1;
37
38 class CharacterName {
39
40 private static SoftReference<CharacterName> refCharName;
41
42 // codepoint -> bkIndex -> lookup -> offset/len
43 private final byte[] strPool;
44 private final int[] lookup; // code point -> offset/len in strPool
45 private final int[] bkIndices; // code point -> lookup index
46
47 // name -> hash -> hsIndices -> cpEntries -> code point
48 private final int[] cpEntries; // code points that have name in strPool
49 private final int[] hsIndices; // chain heads, hash indices into "cps"
50
51 private CharacterName() {
52 try (DataInputStream dis = new DataInputStream(new InflaterInputStream(
53 AccessController.doPrivileged(new PrivilegedAction<>() {
54 public InputStream run() {
55 return getClass().getResourceAsStream("uniName.dat");
56 }
57 })))) {
135 SoftReference<CharacterName> ref = refCharName;
136 CharacterName cname = null;
137 if (ref == null || (cname = ref.get()) == null) {
138 cname = new CharacterName();
139 refCharName = new SoftReference<>(cname);
140 }
141 return cname;
142 }
143
144 public String getName(int cp) {
145 int off = 0;
146 int bk = bkIndices[cp >> 8];
147 if (bk == -1 || (off = lookup[(bk << 8) + (cp & 0xff)]) == 0)
148 return null;
149 @SuppressWarnings("deprecation")
150 String result = new String(strPool, 0, off >>> 8, off & 0xff); // ASCII
151 return result;
152 }
153
154 public int getCodePoint(String name) {
155 byte[] bname = name.getBytes(ISO_8859_1.INSTANCE);
156 int hsh = hashN(bname, 0, bname.length);
157 int idx = hsIndices[(hsh & 0x7fffffff) % hsIndices.length];
158 while (idx != -1) {
159 if (getCpHash(idx) == hsh) {
160 int cp = getCp(idx);
161 int off = -1;
162 int bk = bkIndices[cp >> 8];
163 if (bk != -1 && (off = lookup[(bk << 8) + (cp & 0xff)]) != 0) {
164 int len = off & 0xff;
165 off = off >>> 8;
166 if (bname.length == len) {
167 int i = 0;
168 while (i < len && bname[i] == strPool[off++]) {
169 i++;
170 }
171 if (i == len) {
172 return cp;
173 }
174 }
175 }
|