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             @SuppressWarnings("deprecation")
 114             Object o = Class.forName(packagePrefix + "." + cln,
 115                                      true,
 116                                      this.getClass().getClassLoader()).newInstance();
 117             cs = (Charset)o;
 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 (!jdk.internal.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         return GetPropertyAction.getProperty(key);
 205     }
 206 
 207 
 208 }