Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/cs/FastCharsetProvider.java
38918 views
/*1* Copyright (c) 2004, 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 sun.nio.cs;2627import java.nio.charset.Charset;28import java.nio.charset.spi.CharsetProvider;29import java.util.Iterator;30import java.util.Map;313233/**34* Abstract base class for fast charset providers.35*36* @author Mark Reinhold37*/3839public class FastCharsetProvider40extends CharsetProvider41{4243// Maps canonical names to class names44private Map<String,String> classMap;4546// Maps alias names to canonical names47private Map<String,String> aliasMap;4849// Maps canonical names to cached instances50private Map<String,Charset> cache;5152private String packagePrefix;5354protected FastCharsetProvider(String pp,55Map<String,String> am,56Map<String,String> cm,57Map<String,Charset> c)58{59packagePrefix = pp;60aliasMap = am;61classMap = cm;62cache = c;63}6465private String canonicalize(String csn) {66String acn = aliasMap.get(csn);67return (acn != null) ? acn : csn;68}6970// Private ASCII-only version, optimized for interpretation during startup71//72private static String toLower(String s) {73int n = s.length();74boolean allLower = true;75for (int i = 0; i < n; i++) {76int c = s.charAt(i);77if (((c - 'A') | ('Z' - c)) >= 0) {78allLower = false;79break;80}81}82if (allLower)83return s;84char[] ca = new char[n];85for (int i = 0; i < n; i++) {86int c = s.charAt(i);87if (((c - 'A') | ('Z' - c)) >= 0)88ca[i] = (char)(c + 0x20);89else90ca[i] = (char)c;91}92return new String(ca);93}9495private Charset lookup(String charsetName) {9697String csn = canonicalize(toLower(charsetName));9899// Check cache first100Charset cs = cache.get(csn);101if (cs != null)102return cs;103104// Do we even support this charset?105String cln = classMap.get(csn);106if (cln == null)107return null;108109if (cln.equals("US_ASCII")) {110cs = new US_ASCII();111cache.put(csn, cs);112return cs;113}114115// Instantiate the charset and cache it116try {117Class<?> c = Class.forName(packagePrefix + "." + cln,118true,119this.getClass().getClassLoader());120cs = (Charset)c.newInstance();121cache.put(csn, cs);122return cs;123} catch (ClassNotFoundException |124IllegalAccessException |125InstantiationException x) {126return null;127}128}129130public final Charset charsetForName(String charsetName) {131synchronized (this) {132return lookup(canonicalize(charsetName));133}134}135136public final Iterator<Charset> charsets() {137138return new Iterator<Charset>() {139140Iterator<String> i = classMap.keySet().iterator();141142public boolean hasNext() {143return i.hasNext();144}145146public Charset next() {147String csn = i.next();148return lookup(csn);149}150151public void remove() {152throw new UnsupportedOperationException();153}154155};156157}158159}160161162