blob: 5a908b14dbf906acb195caf43cd6b83a17e078ed [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.nio.cs;
27
28import java.lang.ref.SoftReference;
29import java.nio.charset.Charset;
30import java.nio.charset.spi.CharsetProvider;
31import java.util.Iterator;
32import java.util.Map;
33
34
35/**
36 * Abstract base class for fast charset providers.
37 *
38 * @author Mark Reinhold
39 */
40
41public class FastCharsetProvider
42 extends CharsetProvider
43{
44
45 // Maps canonical names to class names
46 private Map<String,String> classMap;
47
48 // Maps alias names to canonical names
49 private Map<String,String> aliasMap;
50
51 // Maps canonical names to cached instances
52 private Map<String,Charset> cache;
53
54 private String packagePrefix;
55
56 protected FastCharsetProvider(String pp,
57 Map<String,String> am,
58 Map<String,String> cm,
59 Map<String,Charset> c)
60 {
61 packagePrefix = pp;
62 aliasMap = am;
63 classMap = cm;
64 cache = c;
65 }
66
67 private String canonicalize(String csn) {
68 String acn = aliasMap.get(csn);
69 return (acn != null) ? acn : csn;
70 }
71
72 // Private ASCII-only version, optimized for interpretation during startup
73 //
74 private static String toLower(String s) {
75 int n = s.length();
76 boolean allLower = true;
77 for (int i = 0; i < n; i++) {
78 int c = s.charAt(i);
79 if (((c - 'A') | ('Z' - c)) >= 0) {
80 allLower = false;
81 break;
82 }
83 }
84 if (allLower)
85 return s;
86 char[] ca = new char[n];
87 for (int i = 0; i < n; i++) {
88 int c = s.charAt(i);
89 if (((c - 'A') | ('Z' - c)) >= 0)
90 ca[i] = (char)(c + 0x20);
91 else
92 ca[i] = (char)c;
93 }
94 return new String(ca);
95 }
96
97 private Charset lookup(String charsetName) {
98
99 String csn = canonicalize(toLower(charsetName));
100
101 // Check cache first
102 Charset cs = cache.get(csn);
103 if (cs != null)
104 return cs;
105
106 // Do we even support this charset?
107 String cln = classMap.get(csn);
108 if (cln == null)
109 return null;
110
111 if (cln.equals("US_ASCII")) {
112 cs = new US_ASCII();
113 cache.put(csn, cs);
114 return cs;
115 }
116
117 // Instantiate the charset and cache it
118 try {
119 Class c = Class.forName(packagePrefix + "." + cln,
120 true,
121 this.getClass().getClassLoader());
122 cs = (Charset)c.newInstance();
123 cache.put(csn, cs);
124 return cs;
125 } catch (ClassNotFoundException x) {
126 return null;
127 } catch (IllegalAccessException x) {
128 return null;
129 } catch (InstantiationException x) {
130 return null;
131 }
132 }
133
134 public final Charset charsetForName(String charsetName) {
135 synchronized (this) {
136 return lookup(canonicalize(charsetName));
137 }
138 }
139
140 public final Iterator<Charset> charsets() {
141
142 return new Iterator<Charset>() {
143
144 Iterator<String> i = classMap.keySet().iterator();
145
146 public boolean hasNext() {
147 return i.hasNext();
148 }
149
150 public Charset next() {
151 String csn = i.next();
152 return lookup(csn);
153 }
154
155 public void remove() {
156 throw new UnsupportedOperationException();
157 }
158
159 };
160
161 }
162
163}