2401N/A/*
2685N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2401N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2401N/A *
2401N/A * This code is free software; you can redistribute it and/or modify it
2401N/A * under the terms of the GNU General Public License version 2 only, as
2685N/A * published by the Free Software Foundation. Oracle designates this
2401N/A * particular file as subject to the "Classpath" exception as provided
2685N/A * by Oracle in the LICENSE file that accompanied this code.
2401N/A *
2401N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2401N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2401N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2401N/A * version 2 for more details (a copy is included in the LICENSE file that
2401N/A * accompanied this code).
2401N/A *
2401N/A * You should have received a copy of the GNU General Public License version
2401N/A * 2 along with this work; if not, write to the Free Software Foundation,
2401N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2401N/A *
2685N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2685N/A * or visit www.oracle.com if you need additional information or have any
2685N/A * questions.
2401N/A */
2401N/A
2401N/Apackage java.lang;
2401N/A
2401N/Aimport java.io.DataInputStream;
2401N/Aimport java.io.InputStream;
2401N/Aimport java.lang.ref.SoftReference;
2401N/Aimport java.util.Arrays;
2401N/Aimport java.util.zip.InflaterInputStream;
2401N/Aimport java.security.AccessController;
2401N/Aimport java.security.PrivilegedAction;
2401N/A
2401N/Aclass CharacterName {
2401N/A
2401N/A private static SoftReference<byte[]> refStrPool;
2401N/A private static int[][] lookup;
2401N/A
2401N/A private static synchronized byte[] initNamePool() {
2401N/A byte[] strPool = null;
2401N/A if (refStrPool != null && (strPool = refStrPool.get()) != null)
2401N/A return strPool;
2401N/A DataInputStream dis = null;
2401N/A try {
2401N/A dis = new DataInputStream(new InflaterInputStream(
2401N/A AccessController.doPrivileged(new PrivilegedAction<InputStream>()
2401N/A {
2401N/A public InputStream run() {
2401N/A return getClass().getResourceAsStream("uniName.dat");
2401N/A }
2401N/A })));
2401N/A
2401N/A lookup = new int[(Character.MAX_CODE_POINT + 1) >> 8][];
2401N/A int total = dis.readInt();
2401N/A int cpEnd = dis.readInt();
2401N/A byte ba[] = new byte[cpEnd];
2401N/A dis.readFully(ba);
2401N/A
2401N/A int nameOff = 0;
2401N/A int cpOff = 0;
2401N/A int cp = 0;
2401N/A do {
2401N/A int len = ba[cpOff++] & 0xff;
2401N/A if (len == 0) {
2401N/A len = ba[cpOff++] & 0xff;
2401N/A // always big-endian
2401N/A cp = ((ba[cpOff++] & 0xff) << 16) |
2401N/A ((ba[cpOff++] & 0xff) << 8) |
2401N/A ((ba[cpOff++] & 0xff));
2401N/A } else {
2401N/A cp++;
2401N/A }
2401N/A int hi = cp >> 8;
2401N/A if (lookup[hi] == null) {
2401N/A lookup[hi] = new int[0x100];
2401N/A }
2401N/A lookup[hi][cp&0xff] = (nameOff << 8) | len;
2401N/A nameOff += len;
2401N/A } while (cpOff < cpEnd);
2401N/A strPool = new byte[total - cpEnd];
2401N/A dis.readFully(strPool);
3323N/A refStrPool = new SoftReference<>(strPool);
2401N/A } catch (Exception x) {
2401N/A throw new InternalError(x.getMessage());
2401N/A } finally {
2401N/A try {
2401N/A if (dis != null)
2401N/A dis.close();
2401N/A } catch (Exception xx) {}
2401N/A }
2401N/A return strPool;
2401N/A }
2401N/A
2401N/A public static String get(int cp) {
2401N/A byte[] strPool = null;
2401N/A if (refStrPool == null || (strPool = refStrPool.get()) == null)
2401N/A strPool = initNamePool();
2401N/A int off = 0;
2401N/A if (lookup[cp>>8] == null ||
2401N/A (off = lookup[cp>>8][cp&0xff]) == 0)
2401N/A return null;
2401N/A return new String(strPool, 0, off >>> 8, off & 0xff); // ASCII
2401N/A }
2401N/A}