< prev index next >

src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template

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


  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", UTF_8.INSTANCE);
  87             map.put("iso-8859-1", ISO_8859_1.INSTANCE);
  88             map.put("us-ascii", US_ASCII.INSTANCE);



  89             cache = map;
  90         }
  91         return map;
  92     }
  93 
  94     // Private ASCII-only version, optimized for interpretation during startup
  95     //
  96     private static String toLower(String s) {
  97         int n = s.length();
  98         boolean allLower = true;
  99         for (int i = 0; i < n; i++) {
 100             int c = s.charAt(i);
 101             if (((c - 'A') | ('Z' - c)) >= 0) {
 102                 allLower = false;
 103                 break;
 104             }
 105         }
 106         if (allLower)
 107             return s;
 108         StringBuilder sb = new StringBuilder(n);
 109         for (int i = 0; i < n; i++) {
 110             int c = s.charAt(i);
 111             if (((c - 'A') | ('Z' - c)) >= 0)
 112                 sb.append((char)(c + 0x20));
 113             else
 114                 sb.append((char)c);
 115         }
 116         return sb.toString();
 117     }
 118 
 119     private Charset lookup(String charsetName) {
 120         init();
 121 
 122         // By checking these built-ins we can avoid initializing Aliases and
 123         // Classes eagerly during bootstrap
 124         String csn;
 125         if (charsetName.equals("UTF-8")) {
 126             return UTF_8.INSTANCE;
 127         } else if (charsetName.equals("US-ASCII")) {
 128             return US_ASCII.INSTANCE;
 129         } else if (charsetName.equals("ISO-8859-1")) {
 130             return ISO_8859_1.INSTANCE;
 131         } else {
 132             csn = canonicalize(toLower(charsetName));
 133         }
 134 
 135         // Check cache first
 136         Charset cs = cache().get(csn);
 137         if (cs != null)
 138             return cs;
 139 
 140         // Do we even support this charset?
 141         String cln = classMap().get(csn);
 142         if (cln == null)
 143             return null;
 144 
 145         // Instantiate the charset and cache it
 146         try {
 147             @SuppressWarnings("deprecation")
 148             Object o = Class.forName(packagePrefix + cln,
 149                                      true,
 150                                      this.getClass().getClassLoader()).newInstance();




  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();


< prev index next >