34 import java.util.Iterator;
35 import java.util.Map;
36 import sun.security.action.GetPropertyAction;
37
38 public class StandardCharsets extends CharsetProvider {
39
40 _INCLUDE_ALIASES_TABLES_
41 _INCLUDE_ALIASES_MAP_
42 _INCLUDE_CLASSES_MAP_
43 _INCLUDE_CACHE_MAP_
44
45 // Maps canonical names to class names
46 private final Map<String,String> classMap;
47 // Maps alias names to canonical names
48 private final Map<String,String> aliasMap;
49 // Maps canonical names to cached instances
50 private final Map<String,Charset> cache;
51
52 private static final String packagePrefix = "sun.nio.cs";
53
54 public StandardCharsets() {
55 this.aliasMap = new Aliases();
56 this.classMap = new Classes();
57 this.cache = new Cache();
58 }
59
60 private String canonicalize(String csn) {
61 String acn = aliasMap.get(csn);
62 return (acn != null) ? acn : csn;
63 }
64
65 // Private ASCII-only version, optimized for interpretation during startup
66 //
67 private static String toLower(String s) {
68 int n = s.length();
69 boolean allLower = true;
70 for (int i = 0; i < n; i++) {
71 int c = s.charAt(i);
72 if (((c - 'A') | ('Z' - c)) >= 0) {
73 allLower = false;
86 }
87 return new String(ca);
88 }
89
90 private Charset lookup(String charsetName) {
91 init();
92 String csn = canonicalize(toLower(charsetName));
93
94 // Check cache first
95 Charset cs = cache.get(csn);
96 if (cs != null)
97 return cs;
98
99 // Do we even support this charset?
100 String cln = classMap.get(csn);
101 if (cln == null)
102 return null;
103
104 // As all charset class names added to classMap are string literals we
105 // can check identity here as an optimization
106 if (cln == "US_ASCII") {
107 return cache(csn, new US_ASCII());
108 }
109 if (cln == "ISO_8859_1") {
110 return cache(csn, new ISO_8859_1());
111 }
112 if (cln == "UTF_8") {
113 return cache(csn, new UTF_8());
114 }
115
116 // Instantiate the charset and cache it
117 try {
118 @SuppressWarnings("deprecation")
119 Object o = Class.forName(packagePrefix + "." + cln,
120 true,
121 this.getClass().getClassLoader()).newInstance();
122 return cache(csn, (Charset)o);
123 } catch (ClassNotFoundException |
124 IllegalAccessException |
125 InstantiationException x) {
126 return null;
127 }
128 }
129
130 private Charset cache(String csn, Charset cs) {
131 cache.put(csn, cs);
132 return cs;
|
34 import java.util.Iterator;
35 import java.util.Map;
36 import sun.security.action.GetPropertyAction;
37
38 public class StandardCharsets extends CharsetProvider {
39
40 _INCLUDE_ALIASES_TABLES_
41 _INCLUDE_ALIASES_MAP_
42 _INCLUDE_CLASSES_MAP_
43 _INCLUDE_CACHE_MAP_
44
45 // Maps canonical names to class names
46 private final Map<String,String> classMap;
47 // Maps alias names to canonical names
48 private final Map<String,String> aliasMap;
49 // Maps canonical names to cached instances
50 private final Map<String,Charset> cache;
51
52 private static final String packagePrefix = "sun.nio.cs";
53
54 public static final String US_ASCII = "US-ASCII";
55
56 public static final String ISO_8859_1 = "ISO-8859-1";
57
58 public static final String UTF_8 = "UTF-8";
59
60 public StandardCharsets() {
61 this.aliasMap = new Aliases();
62 this.classMap = new Classes();
63 this.cache = new Cache();
64 }
65
66 private String canonicalize(String csn) {
67 String acn = aliasMap.get(csn);
68 return (acn != null) ? acn : csn;
69 }
70
71 // Private ASCII-only version, optimized for interpretation during startup
72 //
73 private static String toLower(String s) {
74 int n = s.length();
75 boolean allLower = true;
76 for (int i = 0; i < n; i++) {
77 int c = s.charAt(i);
78 if (((c - 'A') | ('Z' - c)) >= 0) {
79 allLower = false;
92 }
93 return new String(ca);
94 }
95
96 private Charset lookup(String charsetName) {
97 init();
98 String csn = canonicalize(toLower(charsetName));
99
100 // Check cache first
101 Charset cs = cache.get(csn);
102 if (cs != null)
103 return cs;
104
105 // Do we even support this charset?
106 String cln = classMap.get(csn);
107 if (cln == null)
108 return null;
109
110 // As all charset class names added to classMap are string literals we
111 // can check identity here as an optimization
112 if (cln == US_ASCII) {
113 return cache(csn, new US_ASCII());
114 }
115 if (cln == ISO_8859_1) {
116 return cache(csn, new ISO_8859_1());
117 }
118 if (cln == UTF_8) {
119 return cache(csn, new UTF_8());
120 }
121
122 // Instantiate the charset and cache it
123 try {
124 @SuppressWarnings("deprecation")
125 Object o = Class.forName(packagePrefix + "." + cln,
126 true,
127 this.getClass().getClassLoader()).newInstance();
128 return cache(csn, (Charset)o);
129 } catch (ClassNotFoundException |
130 IllegalAccessException |
131 InstantiationException x) {
132 return null;
133 }
134 }
135
136 private Charset cache(String csn, Charset cs) {
137 cache.put(csn, cs);
138 return cs;
|