author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 4818 | fd477db6c4ee |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
51 |
private Map<String,String> classMap |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
52 |
= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
2 | 53 |
|
54 |
/* Maps alias names to canonical names |
|
55 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
56 |
private Map<String,String> aliasMap |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
57 |
= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
2 | 58 |
|
59 |
/* Maps canonical names to alias-name arrays |
|
60 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
61 |
private Map<String,String[]> aliasNameMap |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
62 |
= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
2 | 63 |
|
64 |
/* Maps canonical names to soft references that hold cached instances |
|
65 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
66 |
private Map<String,SoftReference<Charset>> cache |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
67 |
= new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER); |
2 | 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 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
82 |
private static <K,V> void put(Map<K,V> m, K name, V value) { |
2 | 83 |
if (!m.containsKey(name)) |
84 |
m.put(name, value); |
|
85 |
} |
|
86 |
||
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
87 |
private static <K,V> void remove(Map<K,V> m, K name) { |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
88 |
V x = m.remove(name); |
2 | 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 |
/* Late initialization hook, needed by some providers |
|
115 |
*/ |
|
116 |
protected void init() { } |
|
117 |
||
118 |
private String canonicalize(String charsetName) { |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
119 |
String acn = aliasMap.get(charsetName); |
2 | 120 |
return (acn != null) ? acn : charsetName; |
121 |
} |
|
122 |
||
123 |
private Charset lookup(String csn) { |
|
124 |
||
125 |
// Check cache first |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
126 |
SoftReference<Charset> sr = cache.get(csn); |
2 | 127 |
if (sr != null) { |
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
128 |
Charset cs = sr.get(); |
2 | 129 |
if (cs != null) |
130 |
return cs; |
|
131 |
} |
|
132 |
||
133 |
// Do we even support this charset? |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
134 |
String cln = classMap.get(csn); |
2 | 135 |
|
136 |
if (cln == null) |
|
137 |
return null; |
|
138 |
||
139 |
// Instantiate the charset and cache it |
|
140 |
try { |
|
141 |
||
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
142 |
Class<?> c = Class.forName(packagePrefix + "." + cln, |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
143 |
true, |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
144 |
this.getClass().getClassLoader()); |
2 | 145 |
|
146 |
Charset cs = (Charset)c.newInstance(); |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
147 |
cache.put(csn, new SoftReference<Charset>(cs)); |
2 | 148 |
return cs; |
149 |
} catch (ClassNotFoundException x) { |
|
150 |
return null; |
|
151 |
} catch (IllegalAccessException x) { |
|
152 |
return null; |
|
153 |
} catch (InstantiationException x) { |
|
154 |
return null; |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
public final Charset charsetForName(String charsetName) { |
|
159 |
synchronized (this) { |
|
160 |
init(); |
|
161 |
return lookup(canonicalize(charsetName)); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
public final Iterator<Charset> charsets() { |
|
166 |
||
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
167 |
final ArrayList<String> ks; |
2 | 168 |
synchronized (this) { |
169 |
init(); |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
170 |
ks = new ArrayList<>(classMap.keySet()); |
2 | 171 |
} |
172 |
||
173 |
return new Iterator<Charset>() { |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
174 |
Iterator<String> i = ks.iterator(); |
2 | 175 |
|
176 |
public boolean hasNext() { |
|
177 |
return i.hasNext(); |
|
178 |
} |
|
179 |
||
180 |
public Charset next() { |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
181 |
String csn = i.next(); |
2 | 182 |
return lookup(csn); |
183 |
} |
|
184 |
||
185 |
public void remove() { |
|
186 |
throw new UnsupportedOperationException(); |
|
187 |
} |
|
188 |
}; |
|
189 |
} |
|
190 |
||
191 |
public final String[] aliases(String charsetName) { |
|
192 |
synchronized (this) { |
|
193 |
init(); |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
194 |
return aliasNameMap.get(charsetName); |
2 | 195 |
} |
196 |
} |
|
197 |
||
198 |
} |