8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets
Reviewed-by: sherman

   1 /*
   2  * Copyright (c) 2000, 2017, 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 java.util.Set;
  37 import jdk.internal.vm.annotation.Stable;
  38 import sun.security.action.GetPropertyAction;
  39 
  40 public class StandardCharsets extends CharsetProvider {
  41 
  42     _INCLUDE_ALIASES_TABLES_
  43     _INCLUDE_ALIASES_MAP_
  44     _INCLUDE_CLASSES_MAP_
  45     _INCLUDE_CACHE_MAP_
  46 
  47     // Maps canonical names to class names
  48     private @Stable Map<String,String> classMap;
  49 
  50     // Maps alias names to canonical names
  51     private @Stable Map<String,String> aliasMap;
  52 
  53     // Maps canonical names to cached instances
  54     private @Stable Map<String,Charset> cache;
  55 
  56     private static final String packagePrefix = "sun.nio.cs.";
  57 
  58     public StandardCharsets() {
  59     }
  60 
  61     private String canonicalize(String csn) {
  62         String acn = aliasMap().get(csn);
  63         return (acn != null) ? acn : csn;
  64     }
  65 
  66     private Map<String,String> aliasMap() {
  67         Map<String,String> map = aliasMap;
  68         if (map == null) {
  69             aliasMap = map = new Aliases();
  70         }
  71         return map;
  72     }
  73 
  74     private Map<String,String> classMap() {
  75         Map<String,String> map = classMap;
  76         if (map == null) {
  77             classMap = map = new Classes();
  78         }
  79         return map;
  80     }
  81 
  82     private Map<String,Charset> cache() {
  83         Map<String,Charset> map = cache;
  84         if (map == null) {
  85             map = new Cache();
  86             map.put("utf-8", java.nio.charset.StandardCharsets.UTF_8);
  87             map.put("iso-8859-1", java.nio.charset.StandardCharsets.ISO_8859_1);
  88             map.put("us-ascii", java.nio.charset.StandardCharsets.US_ASCII);
  89             map.put("utf-16", java.nio.charset.StandardCharsets.UTF_16);
  90             map.put("utf-16be", java.nio.charset.StandardCharsets.UTF_16BE);
  91             map.put("utf-16le", java.nio.charset.StandardCharsets.UTF_16LE);
  92             cache = map;
  93         }
  94         return map;
  95     }
  96 
  97     // Private ASCII-only version, optimized for interpretation during startup
  98     //
  99     private static String toLower(String s) {
 100         int n = s.length();
 101         boolean allLower = true;
 102         for (int i = 0; i < n; i++) {
 103             int c = s.charAt(i);
 104             if (((c - 'A') | ('Z' - c)) >= 0) {
 105                 allLower = false;
 106                 break;
 107             }
 108         }
 109         if (allLower)
 110             return s;
 111         StringBuilder sb = new StringBuilder(n);
 112         for (int i = 0; i < n; i++) {
 113             int c = s.charAt(i);
 114             if (((c - 'A') | ('Z' - c)) >= 0)
 115                 sb.append((char)(c + 0x20));
 116             else
 117                 sb.append((char)c);
 118         }
 119         return sb.toString();
 120     }
 121 
 122     private Charset lookup(String charsetName) {
 123         init();
 124 
 125         // By checking these built-ins we can avoid initializing Aliases and
 126         // Classes eagerly during bootstrap
 127         String csn;
 128         if (charsetName.equals("UTF-8")) {
 129             return java.nio.charset.StandardCharsets.UTF_8;
 130         } else if (charsetName.equals("US-ASCII")) {
 131             return java.nio.charset.StandardCharsets.US_ASCII;
 132         } else if (charsetName.equals("ISO-8859-1")) {
 133             return java.nio.charset.StandardCharsets.ISO_8859_1;
 134         } else {
 135             csn = canonicalize(toLower(charsetName));
 136         }
 137 
 138         // Check cache first
 139         Charset cs = cache().get(csn);
 140         if (cs != null)
 141             return cs;
 142 
 143         // Do we even support this charset?
 144         String cln = classMap().get(csn);
 145         if (cln == null)
 146             return null;
 147 
 148         // Instantiate the charset and cache it
 149         try {
 150             @SuppressWarnings("deprecation")
 151             Object o = Class.forName(packagePrefix + cln,
 152                                      true,
 153                                      this.getClass().getClassLoader()).newInstance();
 154             return cache(csn, (Charset)o);
 155         } catch (ClassNotFoundException |
 156                  IllegalAccessException |
 157                  InstantiationException x) {
 158             return null;
 159         }
 160     }
 161 
 162     private Charset cache(String csn, Charset cs) {
 163         cache().put(csn, cs);
 164         return cs;
 165     }
 166 
 167     public final Charset charsetForName(String charsetName) {
 168         synchronized (this) {
 169             return lookup(charsetName);
 170         }
 171     }
 172 
 173     public final Iterator<Charset> charsets() {
 174         Set<String> charsetNames;
 175         synchronized (this) {
 176             init();
 177             // Ensure initialized in synchronized block
 178             charsetNames = classMap().keySet();
 179             aliasMap();
 180             cache();
 181         }
 182         return new Iterator<Charset>() {
 183 
 184                 Iterator<String> i = charsetNames.iterator();
 185 
 186                 public boolean hasNext() {
 187                     return i.hasNext();
 188                 }
 189 
 190                 public Charset next() {
 191                     String csn = i.next();
 192                     return lookup(csn);
 193                 }
 194 
 195                 public void remove() {
 196                     throw new UnsupportedOperationException();
 197                 }
 198 
 199             };
 200     }
 201 
 202     private boolean initialized = false;
 203 
 204     /*   provider the sun.nio.cs.map property fir sjis/ms932 mapping hack
 205      */
 206     private void init() {
 207         if (initialized)
 208             return;
 209         if (!jdk.internal.misc.VM.isBooted())
 210             return;
 211         initialized = true;
 212 
 213         String map = GetPropertyAction.privilegedGetProperty("sun.nio.cs.map");
 214         if (map != null) {
 215             Map<String,String> aliasMap = aliasMap();
 216             Map<String,String> classMap = classMap();
 217             String[] maps = map.split(",");
 218             for (int i = 0; i < maps.length; i++) {
 219                 if (maps[i].equalsIgnoreCase("Windows-31J/Shift_JIS")) {
 220                     // if we dont have both sjis and ms932, do nothing
 221                     if (classMap.get("shift_jis") == null ||
 222                         classMap.get("windows-31j") == null) {
 223                         break;
 224                     }
 225                     aliases_MS932 = new String[] {
 226                         "MS932",        // JDK historical
 227                         "windows-932",
 228                         "csWindows31J",
 229                         "shift-jis",
 230                         "ms_kanji",
 231                         "x-sjis",
 232                         "csShiftJIS",
 233                         // This alias takes precedence over the actual
 234                         // Shift_JIS charset itself since aliases are always
 235                         // resolved first, before looking up canonical names.
 236                         "shift_jis"
 237                     };
 238                     aliases_SJIS = new String[] { "sjis" };
 239 
 240                     for (String alias : aliases_MS932) {
 241                         aliasMap.put(toLower(alias), "windows-31j");
 242                     }
 243                     cache().put("shift_jis", null);
 244                     break;
 245                 }
 246             }
 247         }
 248     }
 249 
 250 }
--- EOF ---