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