author | prappo |
Wed, 08 Nov 2017 18:45:14 +0300 | |
branch | http-client-branch |
changeset 55786 | a32b59f7b7fb |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
10424
diff
changeset
|
2 |
* Copyright (c) 2000, 2011, 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 |
||
30896
7d150914095e
8081452: Move sun.nio.cs.AbstractCharsetProvider into jdk.charset/sun.nio.cs.ext
sherman
parents:
28969
diff
changeset
|
26 |
package sun.nio.cs.ext; |
2 | 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 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
50 |
private Map<String,String> classMap |
36411
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
30896
diff
changeset
|
51 |
= new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
2 | 52 |
|
53 |
/* Maps alias names to canonical names |
|
54 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
55 |
private Map<String,String> aliasMap |
36411
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
30896
diff
changeset
|
56 |
= new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
2 | 57 |
|
58 |
/* Maps canonical names to alias-name arrays |
|
59 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
60 |
private Map<String,String[]> aliasNameMap |
36411
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
30896
diff
changeset
|
61 |
= new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
2 | 62 |
|
63 |
/* Maps canonical names to soft references that hold cached instances |
|
64 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
65 |
private Map<String,SoftReference<Charset>> cache |
36411
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
30896
diff
changeset
|
66 |
= new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
2 | 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 |
*/ |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
81 |
private static <K,V> void put(Map<K,V> m, K name, V value) { |
2 | 82 |
if (!m.containsKey(name)) |
83 |
m.put(name, value); |
|
84 |
} |
|
85 |
||
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
86 |
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
|
87 |
V x = m.remove(name); |
2 | 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 |
||
28969
f980bee32887
8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents:
25859
diff
changeset
|
113 |
protected boolean hasCharset(String name) { |
f980bee32887
8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents:
25859
diff
changeset
|
114 |
synchronized (this) { |
f980bee32887
8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents:
25859
diff
changeset
|
115 |
return classMap.containsKey(name); |
f980bee32887
8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents:
25859
diff
changeset
|
116 |
} |
f980bee32887
8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents:
25859
diff
changeset
|
117 |
} |
f980bee32887
8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents:
25859
diff
changeset
|
118 |
|
2 | 119 |
/* Late initialization hook, needed by some providers |
120 |
*/ |
|
121 |
protected void init() { } |
|
122 |
||
123 |
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
|
124 |
String acn = aliasMap.get(charsetName); |
2 | 125 |
return (acn != null) ? acn : charsetName; |
126 |
} |
|
127 |
||
128 |
private Charset lookup(String csn) { |
|
129 |
||
130 |
// 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
|
131 |
SoftReference<Charset> sr = cache.get(csn); |
2 | 132 |
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
|
133 |
Charset cs = sr.get(); |
2 | 134 |
if (cs != null) |
135 |
return cs; |
|
136 |
} |
|
137 |
||
138 |
// 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
|
139 |
String cln = classMap.get(csn); |
2 | 140 |
|
141 |
if (cln == null) |
|
142 |
return null; |
|
143 |
||
144 |
// Instantiate the charset and cache it |
|
145 |
try { |
|
146 |
||
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
147 |
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
|
148 |
true, |
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
149 |
this.getClass().getClassLoader()); |
2 | 150 |
|
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36411
diff
changeset
|
151 |
@SuppressWarnings("deprecation") |
2 | 152 |
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
|
153 |
cache.put(csn, new SoftReference<Charset>(cs)); |
2 | 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 |
||
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
173 |
final ArrayList<String> ks; |
2 | 174 |
synchronized (this) { |
175 |
init(); |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
176 |
ks = new ArrayList<>(classMap.keySet()); |
2 | 177 |
} |
178 |
||
179 |
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
|
180 |
Iterator<String> i = ks.iterator(); |
2 | 181 |
|
182 |
public boolean hasNext() { |
|
183 |
return i.hasNext(); |
|
184 |
} |
|
185 |
||
186 |
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
|
187 |
String csn = i.next(); |
10424
df8a25e78db3
6898310: (cs) Charset cache lookups should be synchronized
sherman
parents:
7668
diff
changeset
|
188 |
synchronized (AbstractCharsetProvider.this) { |
df8a25e78db3
6898310: (cs) Charset cache lookups should be synchronized
sherman
parents:
7668
diff
changeset
|
189 |
return lookup(csn); |
df8a25e78db3
6898310: (cs) Charset cache lookups should be synchronized
sherman
parents:
7668
diff
changeset
|
190 |
} |
2 | 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(); |
|
4818
fd477db6c4ee
6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents:
2
diff
changeset
|
202 |
return aliasNameMap.get(charsetName); |
2 | 203 |
} |
204 |
} |
|
205 |
||
206 |
} |