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; |
|
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 import sun.misc.ASCIICaseInsensitiveComparator; |
|
37 |
|
38 |
|
39 /** |
|
40 * Abstract base class for charset providers. |
|
41 * |
|
42 * @author Mark Reinhold |
|
43 */ |
|
44 |
|
45 public class AbstractCharsetProvider |
|
46 extends CharsetProvider |
|
47 { |
|
48 |
|
49 /* Maps canonical names to class names |
|
50 */ |
|
51 private Map<String,String> classMap |
|
52 = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
|
53 |
|
54 /* Maps alias names to canonical names |
|
55 */ |
|
56 private Map<String,String> aliasMap |
|
57 = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
|
58 |
|
59 /* Maps canonical names to alias-name arrays |
|
60 */ |
|
61 private Map<String,String[]> aliasNameMap |
|
62 = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
|
63 |
|
64 /* Maps canonical names to soft references that hold cached instances |
|
65 */ |
|
66 private Map<String,SoftReference<Charset>> cache |
|
67 = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
|
68 |
|
69 private String packagePrefix; |
|
70 |
|
71 protected AbstractCharsetProvider() { |
|
72 packagePrefix = "sun.nio.cs"; |
|
73 } |
|
74 |
|
75 protected AbstractCharsetProvider(String pkgPrefixName) { |
|
76 packagePrefix = pkgPrefixName; |
|
77 } |
|
78 |
|
79 /* Add an entry to the given map, but only if no mapping yet exists |
|
80 * for the given name. |
|
81 */ |
|
82 private static <K,V> void put(Map<K,V> m, K name, V value) { |
|
83 if (!m.containsKey(name)) |
|
84 m.put(name, value); |
|
85 } |
|
86 |
|
87 private static <K,V> void remove(Map<K,V> m, K name) { |
|
88 V x = m.remove(name); |
|
89 assert (x != null); |
|
90 } |
|
91 |
|
92 /* Declare support for the given charset |
|
93 */ |
|
94 protected void charset(String name, String className, String[] aliases) { |
|
95 synchronized (this) { |
|
96 put(classMap, name, className); |
|
97 for (int i = 0; i < aliases.length; i++) |
|
98 put(aliasMap, aliases[i], name); |
|
99 put(aliasNameMap, name, aliases); |
|
100 cache.clear(); |
|
101 } |
|
102 } |
|
103 |
|
104 protected void deleteCharset(String name, String[] aliases) { |
|
105 synchronized (this) { |
|
106 remove(classMap, name); |
|
107 for (int i = 0; i < aliases.length; i++) |
|
108 remove(aliasMap, aliases[i]); |
|
109 remove(aliasNameMap, name); |
|
110 cache.clear(); |
|
111 } |
|
112 } |
|
113 |
|
114 protected boolean hasCharset(String name) { |
|
115 synchronized (this) { |
|
116 return classMap.containsKey(name); |
|
117 } |
|
118 } |
|
119 |
|
120 /* Late initialization hook, needed by some providers |
|
121 */ |
|
122 protected void init() { } |
|
123 |
|
124 private String canonicalize(String charsetName) { |
|
125 String acn = aliasMap.get(charsetName); |
|
126 return (acn != null) ? acn : charsetName; |
|
127 } |
|
128 |
|
129 private Charset lookup(String csn) { |
|
130 |
|
131 // Check cache first |
|
132 SoftReference<Charset> sr = cache.get(csn); |
|
133 if (sr != null) { |
|
134 Charset cs = sr.get(); |
|
135 if (cs != null) |
|
136 return cs; |
|
137 } |
|
138 |
|
139 // Do we even support this charset? |
|
140 String cln = classMap.get(csn); |
|
141 |
|
142 if (cln == null) |
|
143 return null; |
|
144 |
|
145 // Instantiate the charset and cache it |
|
146 try { |
|
147 |
|
148 Class<?> c = Class.forName(packagePrefix + "." + cln, |
|
149 true, |
|
150 this.getClass().getClassLoader()); |
|
151 |
|
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 } |
|