Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/cs/AbstractCharsetProvider.java
38918 views
/*1* Copyright (c) 2000, 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.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;35import sun.misc.ASCIICaseInsensitiveComparator;363738/**39* Abstract base class for charset providers.40*41* @author Mark Reinhold42*/4344public class AbstractCharsetProvider45extends CharsetProvider46{4748/* Maps canonical names to class names49*/50private Map<String,String> classMap51= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);5253/* Maps alias names to canonical names54*/55private Map<String,String> aliasMap56= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);5758/* Maps canonical names to alias-name arrays59*/60private Map<String,String[]> aliasNameMap61= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);6263/* Maps canonical names to soft references that hold cached instances64*/65private Map<String,SoftReference<Charset>> cache66= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);6768private String packagePrefix;6970protected AbstractCharsetProvider() {71packagePrefix = "sun.nio.cs";72}7374protected AbstractCharsetProvider(String pkgPrefixName) {75packagePrefix = pkgPrefixName;76}7778/* Add an entry to the given map, but only if no mapping yet exists79* for the given name.80*/81private static <K,V> void put(Map<K,V> m, K name, V value) {82if (!m.containsKey(name))83m.put(name, value);84}8586private static <K,V> void remove(Map<K,V> m, K name) {87V x = m.remove(name);88assert (x != null);89}9091/* Declare support for the given charset92*/93protected void charset(String name, String className, String[] aliases) {94synchronized (this) {95put(classMap, name, className);96for (int i = 0; i < aliases.length; i++)97put(aliasMap, aliases[i], name);98put(aliasNameMap, name, aliases);99cache.clear();100}101}102103protected void deleteCharset(String name, String[] aliases) {104synchronized (this) {105remove(classMap, name);106for (int i = 0; i < aliases.length; i++)107remove(aliasMap, aliases[i]);108remove(aliasNameMap, name);109cache.clear();110}111}112113/* Late initialization hook, needed by some providers114*/115protected void init() { }116117private String canonicalize(String charsetName) {118String acn = aliasMap.get(charsetName);119return (acn != null) ? acn : charsetName;120}121122private Charset lookup(String csn) {123124// Check cache first125SoftReference<Charset> sr = cache.get(csn);126if (sr != null) {127Charset cs = sr.get();128if (cs != null)129return cs;130}131132// Do we even support this charset?133String cln = classMap.get(csn);134135if (cln == null)136return null;137138// Instantiate the charset and cache it139try {140141Class<?> c = Class.forName(packagePrefix + "." + cln,142true,143this.getClass().getClassLoader());144145Charset cs = (Charset)c.newInstance();146cache.put(csn, new SoftReference<Charset>(cs));147return cs;148} catch (ClassNotFoundException x) {149return null;150} catch (IllegalAccessException x) {151return null;152} catch (InstantiationException x) {153return null;154}155}156157public final Charset charsetForName(String charsetName) {158synchronized (this) {159init();160return lookup(canonicalize(charsetName));161}162}163164public final Iterator<Charset> charsets() {165166final ArrayList<String> ks;167synchronized (this) {168init();169ks = new ArrayList<>(classMap.keySet());170}171172return new Iterator<Charset>() {173Iterator<String> i = ks.iterator();174175public boolean hasNext() {176return i.hasNext();177}178179public Charset next() {180String csn = i.next();181synchronized (AbstractCharsetProvider.this) {182return lookup(csn);183}184}185186public void remove() {187throw new UnsupportedOperationException();188}189};190}191192public final String[] aliases(String charsetName) {193synchronized (this) {194init();195return aliasNameMap.get(charsetName);196}197}198199}200201202