1 /* 2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.nio.cs.ext; 27 28 import java.lang.ref.SoftReference; 29 import java.nio.charset.Charset; 30 import java.nio.charset.spi.CharsetProvider; 31 import java.util.ArrayList; 32 import java.util.TreeMap; 33 import java.util.Iterator; 34 import java.util.Locale; 35 import java.util.Map; 36 37 38 /** 39 * Abstract base class for charset providers. 40 * 41 * @author Mark Reinhold 42 */ 43 44 public class AbstractCharsetProvider 45 extends CharsetProvider 46 { 47 48 /* Maps canonical names to class names 49 */ 50 private Map<String,String> classMap 51 = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 52 53 /* Maps alias names to canonical names 54 */ 55 private Map<String,String> aliasMap 56 = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 57 58 /* Maps canonical names to alias-name arrays 59 */ 60 private Map<String,String[]> aliasNameMap 61 = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 62 63 /* Maps canonical names to soft references that hold cached instances 64 */ 65 private Map<String,SoftReference<Charset>> cache 66 = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 67 68 private String packagePrefix; 69 70 protected AbstractCharsetProvider() { 71 packagePrefix = "sun.nio.cs"; 72 } 73 74 protected AbstractCharsetProvider(String pkgPrefixName) { 75 packagePrefix = pkgPrefixName; 76 } 77 78 /* Add an entry to the given map, but only if no mapping yet exists 79 * for the given name. 80 */ 81 private static <K,V> void put(Map<K,V> m, K name, V value) { 82 if (!m.containsKey(name)) 83 m.put(name, value); 84 } 85 86 private static <K,V> void remove(Map<K,V> m, K name) { 87 V x = m.remove(name); 88 assert (x != null); 89 } 90 91 /* Declare support for the given charset 92 */ 93 protected void charset(String name, String className, String[] aliases) { 94 synchronized (this) { 95 put(classMap, name, className); 96 for (int i = 0; i < aliases.length; i++) 97 put(aliasMap, aliases[i], name); 98 put(aliasNameMap, name, aliases); 99 cache.clear(); 100 } 101 } 102 103 protected void deleteCharset(String name, String[] aliases) { 104 synchronized (this) { 105 remove(classMap, name); 106 for (int i = 0; i < aliases.length; i++) 107 remove(aliasMap, aliases[i]); 108 remove(aliasNameMap, name); 109 cache.clear(); 110 } 111 } 112 113 protected boolean hasCharset(String name) { 114 synchronized (this) { 115 return classMap.containsKey(name); 116 } 117 } 118 119 /* Late initialization hook, needed by some providers 120 */ 121 protected void init() { } 122 123 private String canonicalize(String charsetName) { 124 String acn = aliasMap.get(charsetName); 125 return (acn != null) ? acn : charsetName; 126 } 127 128 private Charset lookup(String csn) { 129 130 // Check cache first 131 SoftReference<Charset> sr = cache.get(csn); 132 if (sr != null) { 133 Charset cs = sr.get(); 134 if (cs != null) 135 return cs; 136 } 137 138 // Do we even support this charset? 139 String cln = classMap.get(csn); 140 141 if (cln == null) 142 return null; 143 144 // Instantiate the charset and cache it 145 try { 146 147 Class<?> c = Class.forName(packagePrefix + "." + cln, 148 true, 149 this.getClass().getClassLoader()); 150 151 @SuppressWarnings("deprecation") 152 Charset cs = (Charset)c.newInstance(); 153 cache.put(csn, new SoftReference<Charset>(cs)); 154 return cs; 155 } catch (ClassNotFoundException x) { 156 return null; 157 } catch (IllegalAccessException x) { 158 return null; 159 } catch (InstantiationException x) { 160 return null; 161 } 162 } 163 164 public final Charset charsetForName(String charsetName) { 165 synchronized (this) { 166 init(); 167 return lookup(canonicalize(charsetName)); 168 } 169 } 170 171 public final Iterator<Charset> charsets() { 172 173 final ArrayList<String> ks; 174 synchronized (this) { 175 init(); 176 ks = new ArrayList<>(classMap.keySet()); 177 } 178 179 return new Iterator<Charset>() { 180 Iterator<String> i = ks.iterator(); 181 182 public boolean hasNext() { 183 return i.hasNext(); 184 } 185 186 public Charset next() { 187 String csn = i.next(); 188 synchronized (AbstractCharsetProvider.this) { 189 return lookup(csn); 190 } 191 } 192 193 public void remove() { 194 throw new UnsupportedOperationException(); 195 } 196 }; 197 } 198 199 public final String[] aliases(String charsetName) { 200 synchronized (this) { 201 init(); 202 return aliasNameMap.get(charsetName); 203 } 204 } 205 206 }