27 |
27 |
28 // -- This file was mechanically generated: Do not edit! -- // |
28 // -- This file was mechanically generated: Do not edit! -- // |
29 |
29 |
30 package sun.nio.cs; |
30 package sun.nio.cs; |
31 |
31 |
32 import java.nio.charset.*; |
32 import java.nio.charset.Charset; |
33 |
33 import java.nio.charset.spi.CharsetProvider; |
34 |
34 import java.util.Iterator; |
35 public class StandardCharsets |
35 import java.util.Locale; |
36 extends FastCharsetProvider |
36 import java.util.Map; |
37 { |
37 import java.security.AccessController; |
|
38 import java.security.PrivilegedAction; |
|
39 |
|
40 public class StandardCharsets extends CharsetProvider { |
38 |
41 |
39 _INCLUDE_ALIASES_TABLES_ |
42 _INCLUDE_ALIASES_TABLES_ |
40 _INCLUDE_ALIASES_MAP_ |
43 _INCLUDE_ALIASES_MAP_ |
41 _INCLUDE_CLASSES_MAP_ |
44 _INCLUDE_CLASSES_MAP_ |
42 _INCLUDE_CACHE_MAP_ |
45 _INCLUDE_CACHE_MAP_ |
43 |
46 |
|
47 // Maps canonical names to class names |
|
48 private Map<String,String> classMap; |
|
49 // Maps alias names to canonical names |
|
50 private Map<String,String> aliasMap; |
|
51 // Maps canonical names to cached instances |
|
52 private Map<String,Charset> cache; |
|
53 |
|
54 private String packagePrefix = "sun.nio.cs"; |
|
55 |
44 public StandardCharsets() { |
56 public StandardCharsets() { |
45 super("sun.nio.cs", new Aliases(), new Classes(), new Cache()); |
57 this.aliasMap = new Aliases(); |
46 } |
58 this.classMap = new Classes(); |
|
59 this.cache = new Cache(); |
|
60 } |
|
61 |
|
62 private String canonicalize(String csn) { |
|
63 String acn = aliasMap.get(csn); |
|
64 return (acn != null) ? acn : csn; |
|
65 } |
|
66 |
|
67 // Private ASCII-only version, optimized for interpretation during startup |
|
68 // |
|
69 private static String toLower(String s) { |
|
70 int n = s.length(); |
|
71 boolean allLower = true; |
|
72 for (int i = 0; i < n; i++) { |
|
73 int c = s.charAt(i); |
|
74 if (((c - 'A') | ('Z' - c)) >= 0) { |
|
75 allLower = false; |
|
76 break; |
|
77 } |
|
78 } |
|
79 if (allLower) |
|
80 return s; |
|
81 char[] ca = new char[n]; |
|
82 for (int i = 0; i < n; i++) { |
|
83 int c = s.charAt(i); |
|
84 if (((c - 'A') | ('Z' - c)) >= 0) |
|
85 ca[i] = (char)(c + 0x20); |
|
86 else |
|
87 ca[i] = (char)c; |
|
88 } |
|
89 return new String(ca); |
|
90 } |
|
91 |
|
92 private Charset lookup(String charsetName) { |
|
93 init(); |
|
94 String csn = canonicalize(toLower(charsetName)); |
|
95 |
|
96 // Check cache first |
|
97 Charset cs = cache.get(csn); |
|
98 if (cs != null) |
|
99 return cs; |
|
100 |
|
101 // Do we even support this charset? |
|
102 String cln = classMap.get(csn); |
|
103 if (cln == null) |
|
104 return null; |
|
105 |
|
106 if (cln.equals("US_ASCII")) { |
|
107 cs = new US_ASCII(); |
|
108 cache.put(csn, cs); |
|
109 return cs; |
|
110 } |
|
111 |
|
112 // Instantiate the charset and cache it |
|
113 try { |
|
114 Class<?> c = Class.forName(packagePrefix + "." + cln, |
|
115 true, |
|
116 this.getClass().getClassLoader()); |
|
117 cs = (Charset)c.newInstance(); |
|
118 cache.put(csn, cs); |
|
119 return cs; |
|
120 } catch (ClassNotFoundException | |
|
121 IllegalAccessException | |
|
122 InstantiationException x) { |
|
123 return null; |
|
124 } |
|
125 } |
|
126 |
|
127 public final Charset charsetForName(String charsetName) { |
|
128 synchronized (this) { |
|
129 return lookup(canonicalize(charsetName)); |
|
130 } |
|
131 } |
|
132 |
|
133 public final Iterator<Charset> charsets() { |
|
134 synchronized (this) { |
|
135 init(); |
|
136 } |
|
137 return new Iterator<Charset>() { |
|
138 |
|
139 Iterator<String> i = classMap.keySet().iterator(); |
|
140 |
|
141 public boolean hasNext() { |
|
142 return i.hasNext(); |
|
143 } |
|
144 |
|
145 public Charset next() { |
|
146 String csn = i.next(); |
|
147 return lookup(csn); |
|
148 } |
|
149 |
|
150 public void remove() { |
|
151 throw new UnsupportedOperationException(); |
|
152 } |
|
153 |
|
154 }; |
|
155 } |
|
156 |
|
157 private boolean initialized = false; |
|
158 |
|
159 /* provider the sun.nio.cs.map property fir sjis/ms932 mapping hack |
|
160 */ |
|
161 private void init() { |
|
162 if (initialized) |
|
163 return; |
|
164 if (!sun.misc.VM.isBooted()) |
|
165 return; |
|
166 initialized = true; |
|
167 |
|
168 String map = getProperty("sun.nio.cs.map"); |
|
169 if (map != null) { |
|
170 String[] maps = map.split(","); |
|
171 for (int i = 0; i < maps.length; i++) { |
|
172 if (maps[i].equalsIgnoreCase("Windows-31J/Shift_JIS")) { |
|
173 // if we dont have both sjis and ms932, do nothing |
|
174 if (classMap.get("shift_jis") == null || |
|
175 classMap.get("windows-31j") == null) { |
|
176 break; |
|
177 } |
|
178 aliases_MS932 = new String[] { |
|
179 "MS932", // JDK historical |
|
180 "windows-932", |
|
181 "csWindows31J", |
|
182 "shift-jis", |
|
183 "ms_kanji", |
|
184 "x-sjis", |
|
185 "csShiftJIS", |
|
186 // This alias takes precedence over the actual |
|
187 // Shift_JIS charset itself since aliases are always |
|
188 // resolved first, before looking up canonical names. |
|
189 "shift_jis" |
|
190 }; |
|
191 aliases_SJIS = new String[] { "sjis" }; |
|
192 |
|
193 for (String alias : aliases_MS932) { |
|
194 aliasMap.put(toLower(alias), "windows-31j"); |
|
195 } |
|
196 cache.put("shift_jis", null); |
|
197 break; |
|
198 } |
|
199 } |
|
200 } |
|
201 } |
|
202 |
|
203 private static String getProperty(String key) { |
|
204 // this method may be called during initialization of |
|
205 // system class loader and thus not using lambda |
|
206 return AccessController.doPrivileged( |
|
207 new PrivilegedAction<String>() { |
|
208 @Override |
|
209 public String run() { |
|
210 return System.getProperty(key); |
|
211 } |
|
212 }); |
|
213 } |
|
214 |
47 |
215 |
48 } |
216 } |