Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/cs/AbstractCharsetProvider.java
38918 views
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
/* Late initialization hook, needed by some providers
115
*/
116
protected void init() { }
117
118
private String canonicalize(String charsetName) {
119
String acn = aliasMap.get(charsetName);
120
return (acn != null) ? acn : charsetName;
121
}
122
123
private Charset lookup(String csn) {
124
125
// Check cache first
126
SoftReference<Charset> sr = cache.get(csn);
127
if (sr != null) {
128
Charset cs = sr.get();
129
if (cs != null)
130
return cs;
131
}
132
133
// Do we even support this charset?
134
String cln = classMap.get(csn);
135
136
if (cln == null)
137
return null;
138
139
// Instantiate the charset and cache it
140
try {
141
142
Class<?> c = Class.forName(packagePrefix + "." + cln,
143
true,
144
this.getClass().getClassLoader());
145
146
Charset cs = (Charset)c.newInstance();
147
cache.put(csn, new SoftReference<Charset>(cs));
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
167
final ArrayList<String> ks;
168
synchronized (this) {
169
init();
170
ks = new ArrayList<>(classMap.keySet());
171
}
172
173
return new Iterator<Charset>() {
174
Iterator<String> i = ks.iterator();
175
176
public boolean hasNext() {
177
return i.hasNext();
178
}
179
180
public Charset next() {
181
String csn = i.next();
182
synchronized (AbstractCharsetProvider.this) {
183
return lookup(csn);
184
}
185
}
186
187
public void remove() {
188
throw new UnsupportedOperationException();
189
}
190
};
191
}
192
193
public final String[] aliases(String charsetName) {
194
synchronized (this) {
195
init();
196
return aliasNameMap.get(charsetName);
197
}
198
}
199
200
}
201
202