Path: blob/master/src/jdk.charsets/share/classes/sun/nio/cs/ext/AbstractCharsetProvider.java
41161 views
/*1* Copyright (c) 2000, 2021, 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.ext;2627import java.lang.ref.SoftReference;28import java.nio.charset.Charset;29import java.nio.charset.spi.CharsetProvider;30import java.util.ArrayList;31import java.util.TreeMap;32import java.util.Iterator;33import java.util.Locale;34import java.util.Map;353637/**38* Abstract base class for charset providers.39*40* @author Mark Reinhold41*/4243public class AbstractCharsetProvider44extends CharsetProvider45{4647/* Maps canonical names to class names48*/49private Map<String,String> classMap50= new TreeMap<>(String.CASE_INSENSITIVE_ORDER);5152/* Maps alias names to canonical names53*/54private Map<String,String> aliasMap55= new TreeMap<>(String.CASE_INSENSITIVE_ORDER);5657/* Maps canonical names to alias-name arrays58*/59private Map<String,String[]> aliasNameMap60= new TreeMap<>(String.CASE_INSENSITIVE_ORDER);6162/* Maps canonical names to soft references that hold cached instances63*/64private Map<String,SoftReference<Charset>> cache65= new TreeMap<>(String.CASE_INSENSITIVE_ORDER);6667private String packagePrefix;6869protected AbstractCharsetProvider() {70packagePrefix = "sun.nio.cs.";71}7273protected AbstractCharsetProvider(String pkgPrefixName) {74packagePrefix = pkgPrefixName.concat(".");75}7677/* Add an entry to the given map, but only if no mapping yet exists78* for the given name.79*/80private static <K,V> void put(Map<K,V> m, K name, V value) {81if (!m.containsKey(name))82m.put(name, value);83}8485private static <K,V> void remove(Map<K,V> m, K name) {86V x = m.remove(name);87assert (x != null);88}8990/* Declare support for the given charset91*/92protected void charset(String name, String className, String[] aliases) {93synchronized (this) {94put(classMap, name, className);95for (int i = 0; i < aliases.length; i++)96put(aliasMap, aliases[i], name);97put(aliasNameMap, name, aliases);98cache.clear();99}100}101102protected void deleteCharset(String name, String[] aliases) {103synchronized (this) {104remove(classMap, name);105for (int i = 0; i < aliases.length; i++)106remove(aliasMap, aliases[i]);107remove(aliasNameMap, name);108cache.clear();109}110}111112protected boolean hasCharset(String name) {113synchronized (this) {114return classMap.containsKey(name);115}116}117118/* Late initialization hook, needed by some providers119*/120protected void init() { }121122private String canonicalize(String charsetName) {123String acn = aliasMap.get(charsetName);124return (acn != null) ? acn : charsetName;125}126127private Charset lookup(String csn) {128129// Check cache first130SoftReference<Charset> sr = cache.get(csn);131if (sr != null) {132Charset cs = sr.get();133if (cs != null)134return cs;135}136137// Do we even support this charset?138String cln = classMap.get(csn);139140if (cln == null)141return null;142143// Instantiate the charset and cache it144try {145146Class<?> c = Class.forName(packagePrefix.concat(cln),147true,148this.getClass().getClassLoader());149150@SuppressWarnings("deprecation")151Charset cs = (Charset)c.newInstance();152cache.put(csn, new SoftReference<Charset>(cs));153return cs;154} catch (ClassNotFoundException x) {155return null;156} catch (IllegalAccessException x) {157return null;158} catch (InstantiationException x) {159return null;160}161}162163public final Charset charsetForName(String charsetName) {164synchronized (this) {165init();166return lookup(canonicalize(charsetName));167}168}169170public final Iterator<Charset> charsets() {171172final ArrayList<String> ks;173synchronized (this) {174init();175ks = new ArrayList<>(classMap.keySet());176}177178return new Iterator<Charset>() {179Iterator<String> i = ks.iterator();180181public boolean hasNext() {182return i.hasNext();183}184185public Charset next() {186String csn = i.next();187synchronized (AbstractCharsetProvider.this) {188return lookup(csn);189}190}191192public void remove() {193throw new UnsupportedOperationException();194}195};196}197198public final String[] aliases(String charsetName) {199synchronized (this) {200init();201return aliasNameMap.get(charsetName);202}203}204205}206207208