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 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;
  80                 break;
  81             }
  82         }
  83         if (allLower)
  84             return s;
  85         char[] ca = new char[n];
  86         for (int i = 0; i < n; i++) {
  87             int c = s.charAt(i);
  88             if (((c - 'A') | ('Z' - c)) >= 0)
  89                 ca[i] = (char)(c + 0x20);
  90             else
  91                 ca[i] = (char)c;
  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;
 139     }
 140 
 141     public final Charset charsetForName(String charsetName) {
 142         synchronized (this) {
 143             return lookup(canonicalize(charsetName));
 144         }
 145     }
 146 
 147     public final Iterator<Charset> charsets() {
 148         synchronized (this) {
 149             init();
 150         }
 151         return new Iterator<Charset>() {
 152 
 153                 Iterator<String> i = classMap.keySet().iterator();
 154 
 155                 public boolean hasNext() {
 156                     return i.hasNext();
 157                 }
 158 
 159                 public Charset next() {
 160                     String csn = i.next();
 161                     return lookup(csn);
 162                 }
 163 
 164                 public void remove() {
 165                     throw new UnsupportedOperationException();
 166                 }
 167 
 168             };
 169     }
 170 
 171     private boolean initialized = false;
 172 
 173     /*   provider the sun.nio.cs.map property fir sjis/ms932 mapping hack
 174      */
 175     private void init() {
 176         if (initialized)
 177             return;
 178         if (!jdk.internal.misc.VM.isBooted())
 179             return;
 180         initialized = true;
 181 
 182         String map = GetPropertyAction.privilegedGetProperty("sun.nio.cs.map");
 183         if (map != null) {
 184             String[] maps = map.split(",");
 185             for (int i = 0; i < maps.length; i++) {
 186                 if (maps[i].equalsIgnoreCase("Windows-31J/Shift_JIS")) {
 187                     // if we dont have both sjis and ms932, do nothing
 188                     if (classMap.get("shift_jis") == null ||
 189                         classMap.get("windows-31j") == null) {
 190                         break;
 191                     }
 192                     aliases_MS932 = new String[] {
 193                         "MS932",        // JDK historical
 194                         "windows-932",
 195                         "csWindows31J",
 196                         "shift-jis",
 197                         "ms_kanji",
 198                         "x-sjis",
 199                         "csShiftJIS",
 200                         // This alias takes precedence over the actual
 201                         // Shift_JIS charset itself since aliases are always
 202                         // resolved first, before looking up canonical names.
 203                         "shift_jis"
 204                     };
 205                     aliases_SJIS = new String[] { "sjis" };
 206 
 207                     for (String alias : aliases_MS932) {
 208                         aliasMap.put(toLower(alias), "windows-31j");
 209                     }
 210                     cache.put("shift_jis", null);
 211                     break;
 212                 }
 213             }
 214         }
 215     }
 216 
 217 }