1 /* 2 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 * 26 */ 27 28 // -- This file was mechanically generated: Do not edit! -- // 29 30 package sun.nio.cs; 31 32 import java.nio.charset.Charset; 33 import java.nio.charset.spi.CharsetProvider; 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; 74 break; 75 } 76 } 77 if (allLower) 78 return s; 79 char[] ca = new char[n]; 80 for (int i = 0; i < n; i++) { 81 int c = s.charAt(i); 82 if (((c - 'A') | ('Z' - c)) >= 0) 83 ca[i] = (char)(c + 0x20); 84 else 85 ca[i] = (char)c; 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; 133 } 134 135 public final Charset charsetForName(String charsetName) { 136 synchronized (this) { 137 return lookup(canonicalize(charsetName)); 138 } 139 } 140 141 public final Iterator<Charset> charsets() { 142 synchronized (this) { 143 init(); 144 } 145 return new Iterator<Charset>() { 146 147 Iterator<String> i = classMap.keySet().iterator(); 148 149 public boolean hasNext() { 150 return i.hasNext(); 151 } 152 153 public Charset next() { 154 String csn = i.next(); 155 return lookup(csn); 156 } 157 158 public void remove() { 159 throw new UnsupportedOperationException(); 160 } 161 162 }; 163 } 164 165 private boolean initialized = false; 166 167 /* provider the sun.nio.cs.map property fir sjis/ms932 mapping hack 168 */ 169 private void init() { 170 if (initialized) 171 return; 172 if (!jdk.internal.misc.VM.isBooted()) 173 return; 174 initialized = true; 175 176 String map = GetPropertyAction.privilegedGetProperty("sun.nio.cs.map"); 177 if (map != null) { 178 String[] maps = map.split(","); 179 for (int i = 0; i < maps.length; i++) { 180 if (maps[i].equalsIgnoreCase("Windows-31J/Shift_JIS")) { 181 // if we dont have both sjis and ms932, do nothing 182 if (classMap.get("shift_jis") == null || 183 classMap.get("windows-31j") == null) { 184 break; 185 } 186 aliases_MS932 = new String[] { 187 "MS932", // JDK historical 188 "windows-932", 189 "csWindows31J", 190 "shift-jis", 191 "ms_kanji", 192 "x-sjis", 193 "csShiftJIS", 194 // This alias takes precedence over the actual 195 // Shift_JIS charset itself since aliases are always 196 // resolved first, before looking up canonical names. 197 "shift_jis" 198 }; 199 aliases_SJIS = new String[] { "sjis" }; 200 201 for (String alias : aliases_MS932) { 202 aliasMap.put(toLower(alias), "windows-31j"); 203 } 204 cache.put("shift_jis", null); 205 break; 206 } 207 } 208 } 209 } 210 211 }