Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/CharacterName.java
38829 views
/*1* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import java.io.DataInputStream;28import java.io.InputStream;29import java.lang.ref.SoftReference;30import java.util.Arrays;31import java.util.zip.InflaterInputStream;32import java.security.AccessController;33import java.security.PrivilegedAction;3435class CharacterName {3637private static SoftReference<byte[]> refStrPool;38private static int[][] lookup;3940private static synchronized byte[] initNamePool() {41byte[] strPool = null;42if (refStrPool != null && (strPool = refStrPool.get()) != null)43return strPool;44DataInputStream dis = null;45try {46dis = new DataInputStream(new InflaterInputStream(47AccessController.doPrivileged(new PrivilegedAction<InputStream>()48{49public InputStream run() {50return getClass().getResourceAsStream("uniName.dat");51}52})));5354lookup = new int[(Character.MAX_CODE_POINT + 1) >> 8][];55int total = dis.readInt();56int cpEnd = dis.readInt();57byte ba[] = new byte[cpEnd];58dis.readFully(ba);5960int nameOff = 0;61int cpOff = 0;62int cp = 0;63do {64int len = ba[cpOff++] & 0xff;65if (len == 0) {66len = ba[cpOff++] & 0xff;67// always big-endian68cp = ((ba[cpOff++] & 0xff) << 16) |69((ba[cpOff++] & 0xff) << 8) |70((ba[cpOff++] & 0xff));71} else {72cp++;73}74int hi = cp >> 8;75if (lookup[hi] == null) {76lookup[hi] = new int[0x100];77}78lookup[hi][cp&0xff] = (nameOff << 8) | len;79nameOff += len;80} while (cpOff < cpEnd);81strPool = new byte[total - cpEnd];82dis.readFully(strPool);83refStrPool = new SoftReference<>(strPool);84} catch (Exception x) {85throw new InternalError(x.getMessage(), x);86} finally {87try {88if (dis != null)89dis.close();90} catch (Exception xx) {}91}92return strPool;93}9495public static String get(int cp) {96byte[] strPool = null;97if (refStrPool == null || (strPool = refStrPool.get()) == null)98strPool = initNamePool();99int off = 0;100if (lookup[cp>>8] == null ||101(off = lookup[cp>>8][cp&0xff]) == 0)102return null;103@SuppressWarnings("deprecation")104String result = new String(strPool, 0, off >>> 8, off & 0xff); // ASCII105return result;106}107}108109110