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.ext;
  31 
  32 import java.lang.ref.SoftReference;
  33 import java.nio.charset.Charset;
  34 import java.nio.charset.spi.CharsetProvider;
  35 import sun.nio.cs.AbstractCharsetProvider;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 import sun.nio.cs.AbstractCharsetProvider;
  39 
  40 /**
  41  * Provider for extended charsets.
  42  */
  43 
  44 public class ExtendedCharsets extends AbstractCharsetProvider {
  45 
  46     static volatile ExtendedCharsets instance = null;
  47 
  48     public ExtendedCharsets() {
  49 
  50         super("sun.nio.cs.ext");  // identify provider pkg name.
  51 
  52         _CHARSETS_DEF_LIST_
  53 
  54         instance = this;
  55 
  56     }
  57 
  58     private boolean initialized = false;
  59 
  60     // If the sun.nio.cs.map property is defined on the command line we won't
  61     // see it in the system-properties table until after the charset subsystem
  62     // has been initialized.  We therefore delay the effect of this property
  63     // until after the JRE has completely booted.
  64     //
  65     // At the moment following values for this property are supported, property
  66     // value string is case insensitive.
  67     //
  68     // (1)"Windows-31J/Shift_JIS"
  69     // In 1.4.1 we added a correct implementation of the Shift_JIS charset
  70     // but in previous releases this charset name had been treated as an alias
  71     // for Windows-31J, aka MS932. Users who have existing code that depends
  72     // upon this alias can restore the previous behavior by defining this
  73     // property to have this value.
  74     //
  75     // (2)"x-windows-50221/ISO-2022-JP"
  76     //    "x-windows-50220/ISO-2022-JP"
  77     //    "x-windows-iso2022jp/ISO-2022-JP"
  78     // The charset ISO-2022-JP is a "standard based" implementation by default,
  79     // which supports ASCII, JIS_X_0201 and JIS_X_0208 mappings based encoding
  80     // and decoding only.
  81     // There are three Microsoft iso-2022-jp variants, namely x-windows-50220,
  82     // x-windows-50221 and x-windows-iso2022jp which behaves "slightly" differently
  83     // compared to the "standard based" implementation. See ISO2022_JP.java for
  84     // detailed description. Users who prefer the behavior of MS iso-2022-jp
  85     // variants should use these names explicitly instead of using "ISO-2022-JP"
  86     // and its aliases. However for those who need the ISO-2022-JP charset behaves
  87     // exactly the same as MS variants do, above properties can be defined to
  88     // switch.
  89     //
  90     // If we need to define other charset-alias mappings in the future then
  91     // this property could be further extended, the general idea being that its
  92     // value should be of the form
  93     //
  94     //     new-charset-1/old-charset-1,new-charset-2/old-charset-2,...
  95     //
  96     // where each charset named to the left of a slash is intended to replace
  97     // (most) uses of the charset named to the right of the slash.
  98     //
  99     protected void init() {
 100         if (initialized)
 101             return;
 102         if (!sun.misc.VM.isBooted())
 103             return;
 104 
 105         String map = getProperty("sun.nio.cs.map");
 106         boolean sjisIsMS932 = false;
 107         boolean iso2022jpIsMS50221 = false;
 108         boolean iso2022jpIsMS50220 = false;
 109         boolean iso2022jpIsMSISO2022JP = false;
 110         if (map != null) {
 111             String[] maps = map.split(",");
 112             for (int i = 0; i < maps.length; i++) {
 113                 if (maps[i].equalsIgnoreCase("Windows-31J/Shift_JIS")) {
 114                     sjisIsMS932 = true;
 115                 } else if (maps[i].equalsIgnoreCase("x-windows-50221/ISO-2022-JP")) {
 116                     iso2022jpIsMS50221 = true;
 117                 } else if (maps[i].equalsIgnoreCase("x-windows-50220/ISO-2022-JP")) {
 118                     iso2022jpIsMS50220 = true;
 119                 } else if (maps[i].equalsIgnoreCase("x-windows-iso2022jp/ISO-2022-JP")) {
 120                     iso2022jpIsMSISO2022JP = true;
 121                 }
 122             }
 123         }
 124         if (sjisIsMS932 && hasCharset("Shift_JIS")) {
 125             deleteCharset("Shift_JIS",
 126                           new String[] {
 127                               // IANA aliases
 128                               "sjis", // historical
 129                               "shift_jis",
 130                               "shift-jis",
 131                               "ms_kanji",
 132                               "x-sjis",
 133                               "csShiftJIS"
 134                           });
 135             deleteCharset("windows-31j",
 136                           new String[] {
 137                               "MS932", // JDK historical
 138                               "windows-932",
 139                               "csWindows31J"
 140                           });
 141             charset("Shift_JIS", "SJIS",
 142                     new String[] {
 143                         // IANA aliases
 144                         "sjis"          // JDK historical
 145                     });
 146             charset("windows-31j", "MS932",
 147                     new String[] {
 148                         "MS932",        // JDK historical
 149                         "windows-932",
 150                         "csWindows31J",
 151                         "shift-jis",
 152                         "ms_kanji",
 153                         "x-sjis",
 154                         "csShiftJIS",
 155                         // This alias takes precedence over the actual
 156                         // Shift_JIS charset itself since aliases are always
 157                         // resolved first, before looking up canonical names.
 158                         "shift_jis"
 159                     });
 160         }
 161         if (iso2022jpIsMS50221 ||
 162             iso2022jpIsMS50220 ||
 163             iso2022jpIsMSISO2022JP) {
 164             deleteCharset("ISO-2022-JP",
 165                           new String[] {
 166                               "iso2022jp",
 167                                 "jis",
 168                                 "csISO2022JP",
 169                                 "jis_encoding",
 170                                 "csjisencoding"
 171                           });
 172             if (iso2022jpIsMS50221) {
 173                 deleteCharset("x-windows-50221",
 174                               new String[] {
 175                                   "cp50221",
 176                                   "ms50221"
 177                               });
 178                 charset("x-windows-50221", "MS50221",
 179                         new String[] {
 180                             "cp50221",
 181                             "ms50221",
 182                             "iso-2022-jp",
 183                             "iso2022jp",
 184                             "jis",
 185                             "csISO2022JP",
 186                             "jis_encoding",
 187                             "csjisencoding"
 188                         });
 189             } else if (iso2022jpIsMS50220) {
 190                 deleteCharset("x-windows-50220",
 191                               new String[] {
 192                                   "cp50220",
 193                                   "ms50220"
 194                               });
 195                 charset("x-windows-50220", "MS50220",
 196                         new String[] {
 197                             "cp50220",
 198                             "ms50220",
 199                             "iso-2022-jp",
 200                             "iso2022jp",
 201                             "jis",
 202                             "csISO2022JP",
 203                             "jis_encoding",
 204                             "csjisencoding"
 205                         });
 206             } else {
 207                 deleteCharset("x-windows-iso2022jp",
 208                               new String[] {
 209                                   "windows-iso2022jp"
 210                               });
 211                 charset("x-windows-iso2022jp", "MSISO2022JP",
 212                         new String[] {
 213                             "windows-iso2022jp",
 214                             "iso-2022-jp",
 215                             "iso2022jp",
 216                             "jis",
 217                             "csISO2022JP",
 218                             "jis_encoding",
 219                             "csjisencoding"
 220                         });
 221 
 222 
 223             }
 224         }
 225         initialized = true;
 226     }
 227 
 228     private static String getProperty(String key) {
 229         // this method may be called during initialization of
 230         // system class loader and thus not using lambda
 231         return AccessController.doPrivileged(
 232             new PrivilegedAction<String>() {
 233                 @Override
 234                 public String run() {
 235                     return System.getProperty(key);
 236                 }
 237             });
 238     }
 239 
 240     public static String[] aliasesFor(String charsetName) {
 241         if (instance == null)
 242             return null;
 243         return instance.aliases(charsetName);
 244     }
 245 
 246 }