1.1 --- a/rt/emul/compact/src/main/java/java/nio/charset/Charset.java Thu Oct 03 15:40:35 2013 +0200
1.2 +++ b/rt/emul/compact/src/main/java/java/nio/charset/Charset.java Fri Oct 04 15:02:17 2013 +0200
1.3 @@ -25,27 +25,16 @@
1.4
1.5 package java.nio.charset;
1.6
1.7 -import java.nio.ByteBuffer;
1.8 -import java.nio.CharBuffer;
1.9 -import java.nio.charset.spi.CharsetProvider;
1.10 -import java.security.AccessController;
1.11 -import java.security.AccessControlException;
1.12 -import java.security.PrivilegedAction;
1.13 +//import java.nio.ByteBuffer;
1.14 +//import java.nio.CharBuffer;
1.15 import java.util.Collections;
1.16 import java.util.HashSet;
1.17 import java.util.Iterator;
1.18 import java.util.Locale;
1.19 import java.util.Map;
1.20 -import java.util.NoSuchElementException;
1.21 import java.util.Set;
1.22 -import java.util.ServiceLoader;
1.23 -import java.util.ServiceConfigurationError;
1.24 import java.util.SortedMap;
1.25 import java.util.TreeMap;
1.26 -import sun.misc.ASCIICaseInsensitiveComparator;
1.27 -import sun.nio.cs.StandardCharsets;
1.28 -import sun.nio.cs.ThreadLocalCoders;
1.29 -import sun.security.action.GetPropertyAction;
1.30
1.31
1.32 /**
1.33 @@ -281,17 +270,6 @@
1.34
1.35 private static volatile String bugLevel = null;
1.36
1.37 - static boolean atBugLevel(String bl) { // package-private
1.38 - String level = bugLevel;
1.39 - if (level == null) {
1.40 - if (!sun.misc.VM.isBooted())
1.41 - return false;
1.42 - bugLevel = level = AccessController.doPrivileged(
1.43 - new GetPropertyAction("sun.nio.cs.bugLevel", ""));
1.44 - }
1.45 - return level.equals(bl);
1.46 - }
1.47 -
1.48 /**
1.49 * Checks that the given string is a legal charset name. </p>
1.50 *
1.51 @@ -303,10 +281,8 @@
1.52 */
1.53 private static void checkName(String s) {
1.54 int n = s.length();
1.55 - if (!atBugLevel("1.4")) {
1.56 if (n == 0)
1.57 throw new IllegalCharsetNameException(s);
1.58 - }
1.59 for (int i = 0; i < n; i++) {
1.60 char c = s.charAt(i);
1.61 if (c >= 'A' && c <= 'Z') continue;
1.62 @@ -321,9 +297,6 @@
1.63 }
1.64 }
1.65
1.66 - /* The standard set of charsets */
1.67 - private static CharsetProvider standardProvider = new StandardCharsets();
1.68 -
1.69 // Cache of the most-recently-returned charsets,
1.70 // along with the names that were used to find them
1.71 //
1.72 @@ -340,126 +313,23 @@
1.73 // thrown. Should be invoked with full privileges.
1.74 //
1.75 private static Iterator providers() {
1.76 - return new Iterator() {
1.77 -
1.78 - ClassLoader cl = ClassLoader.getSystemClassLoader();
1.79 - ServiceLoader<CharsetProvider> sl =
1.80 - ServiceLoader.load(CharsetProvider.class, cl);
1.81 - Iterator<CharsetProvider> i = sl.iterator();
1.82 -
1.83 - Object next = null;
1.84 -
1.85 - private boolean getNext() {
1.86 - while (next == null) {
1.87 - try {
1.88 - if (!i.hasNext())
1.89 - return false;
1.90 - next = i.next();
1.91 - } catch (ServiceConfigurationError sce) {
1.92 - if (sce.getCause() instanceof SecurityException) {
1.93 - // Ignore security exceptions
1.94 - continue;
1.95 - }
1.96 - throw sce;
1.97 - }
1.98 - }
1.99 - return true;
1.100 - }
1.101 -
1.102 - public boolean hasNext() {
1.103 - return getNext();
1.104 - }
1.105 -
1.106 - public Object next() {
1.107 - if (!getNext())
1.108 - throw new NoSuchElementException();
1.109 - Object n = next;
1.110 - next = null;
1.111 - return n;
1.112 - }
1.113 -
1.114 - public void remove() {
1.115 - throw new UnsupportedOperationException();
1.116 - }
1.117 -
1.118 - };
1.119 + return Collections.emptyIterator();
1.120 }
1.121
1.122 // Thread-local gate to prevent recursive provider lookups
1.123 private static ThreadLocal<ThreadLocal> gate = new ThreadLocal<ThreadLocal>();
1.124
1.125 private static Charset lookupViaProviders(final String charsetName) {
1.126 -
1.127 - // The runtime startup sequence looks up standard charsets as a
1.128 - // consequence of the VM's invocation of System.initializeSystemClass
1.129 - // in order to, e.g., set system properties and encode filenames. At
1.130 - // that point the application class loader has not been initialized,
1.131 - // however, so we can't look for providers because doing so will cause
1.132 - // that loader to be prematurely initialized with incomplete
1.133 - // information.
1.134 - //
1.135 - if (!sun.misc.VM.isBooted())
1.136 - return null;
1.137 -
1.138 - if (gate.get() != null)
1.139 - // Avoid recursive provider lookups
1.140 - return null;
1.141 - try {
1.142 - gate.set(gate);
1.143 -
1.144 - return AccessController.doPrivileged(
1.145 - new PrivilegedAction<Charset>() {
1.146 - public Charset run() {
1.147 - for (Iterator i = providers(); i.hasNext();) {
1.148 - CharsetProvider cp = (CharsetProvider)i.next();
1.149 - Charset cs = cp.charsetForName(charsetName);
1.150 - if (cs != null)
1.151 - return cs;
1.152 - }
1.153 - return null;
1.154 - }
1.155 - });
1.156 -
1.157 - } finally {
1.158 - gate.set(null);
1.159 - }
1.160 + return null;
1.161 }
1.162
1.163 /* The extended set of charsets */
1.164 private static Object extendedProviderLock = new Object();
1.165 private static boolean extendedProviderProbed = false;
1.166 - private static CharsetProvider extendedProvider = null;
1.167
1.168 - private static void probeExtendedProvider() {
1.169 - AccessController.doPrivileged(new PrivilegedAction<Object>() {
1.170 - public Object run() {
1.171 - try {
1.172 - Class epc
1.173 - = Class.forName("sun.nio.cs.ext.ExtendedCharsets");
1.174 - extendedProvider = (CharsetProvider)epc.newInstance();
1.175 - } catch (ClassNotFoundException x) {
1.176 - // Extended charsets not available
1.177 - // (charsets.jar not present)
1.178 - } catch (InstantiationException x) {
1.179 - throw new Error(x);
1.180 - } catch (IllegalAccessException x) {
1.181 - throw new Error(x);
1.182 - }
1.183 - return null;
1.184 - }
1.185 - });
1.186 - }
1.187
1.188 private static Charset lookupExtendedCharset(String charsetName) {
1.189 - CharsetProvider ecp = null;
1.190 - synchronized (extendedProviderLock) {
1.191 - if (!extendedProviderProbed) {
1.192 - probeExtendedProvider();
1.193 - extendedProviderProbed = true;
1.194 - }
1.195 - ecp = extendedProvider;
1.196 - }
1.197 - return (ecp != null) ? ecp.charsetForName(charsetName) : null;
1.198 + return null;
1.199 }
1.200
1.201 private static Charset lookup(String charsetName) {
1.202 @@ -483,15 +353,6 @@
1.203 return (Charset)a[1];
1.204 }
1.205
1.206 - Charset cs;
1.207 - if ((cs = standardProvider.charsetForName(charsetName)) != null ||
1.208 - (cs = lookupExtendedCharset(charsetName)) != null ||
1.209 - (cs = lookupViaProviders(charsetName)) != null)
1.210 - {
1.211 - cache(charsetName, cs);
1.212 - return cs;
1.213 - }
1.214 -
1.215 /* Only need to check the name if we didn't find a charset for it */
1.216 checkName(charsetName);
1.217 return null;
1.218 @@ -581,20 +442,9 @@
1.219 * to charset objects
1.220 */
1.221 public static SortedMap<String,Charset> availableCharsets() {
1.222 - return AccessController.doPrivileged(
1.223 - new PrivilegedAction<SortedMap<String,Charset>>() {
1.224 - public SortedMap<String,Charset> run() {
1.225 - TreeMap<String,Charset> m =
1.226 - new TreeMap<String,Charset>(
1.227 - ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
1.228 - put(standardProvider.charsets(), m);
1.229 - for (Iterator i = providers(); i.hasNext();) {
1.230 - CharsetProvider cp = (CharsetProvider)i.next();
1.231 - put(cp.charsets(), m);
1.232 - }
1.233 - return Collections.unmodifiableSortedMap(m);
1.234 - }
1.235 - });
1.236 + TreeMap<String, Charset> tm = new TreeMap<String,Charset>();
1.237 + tm.put("UTF-8", Charset.defaultCharset());
1.238 + return tm;
1.239 }
1.240
1.241 private static volatile Charset defaultCharset;
1.242 @@ -612,15 +462,7 @@
1.243 */
1.244 public static Charset defaultCharset() {
1.245 if (defaultCharset == null) {
1.246 - synchronized (Charset.class) {
1.247 - String csn = AccessController.doPrivileged(
1.248 - new GetPropertyAction("file.encoding"));
1.249 - Charset cs = lookup(csn);
1.250 - if (cs != null)
1.251 - defaultCharset = cs;
1.252 - else
1.253 - defaultCharset = forName("UTF-8");
1.254 - }
1.255 + defaultCharset = forName("UTF-8");
1.256 }
1.257 return defaultCharset;
1.258 }
1.259 @@ -805,16 +647,16 @@
1.260 *
1.261 * @return A char buffer containing the decoded characters
1.262 */
1.263 - public final CharBuffer decode(ByteBuffer bb) {
1.264 - try {
1.265 - return ThreadLocalCoders.decoderFor(this)
1.266 - .onMalformedInput(CodingErrorAction.REPLACE)
1.267 - .onUnmappableCharacter(CodingErrorAction.REPLACE)
1.268 - .decode(bb);
1.269 - } catch (CharacterCodingException x) {
1.270 - throw new Error(x); // Can't happen
1.271 - }
1.272 - }
1.273 +// public final CharBuffer decode(ByteBuffer bb) {
1.274 +// try {
1.275 +// return ThreadLocalCoders.decoderFor(this)
1.276 +// .onMalformedInput(CodingErrorAction.REPLACE)
1.277 +// .onUnmappableCharacter(CodingErrorAction.REPLACE)
1.278 +// .decode(bb);
1.279 +// } catch (CharacterCodingException x) {
1.280 +// throw new Error(x); // Can't happen
1.281 +// }
1.282 +// }
1.283
1.284 /**
1.285 * Convenience method that encodes Unicode characters into bytes in this
1.286 @@ -841,16 +683,16 @@
1.287 *
1.288 * @return A byte buffer containing the encoded characters
1.289 */
1.290 - public final ByteBuffer encode(CharBuffer cb) {
1.291 - try {
1.292 - return ThreadLocalCoders.encoderFor(this)
1.293 - .onMalformedInput(CodingErrorAction.REPLACE)
1.294 - .onUnmappableCharacter(CodingErrorAction.REPLACE)
1.295 - .encode(cb);
1.296 - } catch (CharacterCodingException x) {
1.297 - throw new Error(x); // Can't happen
1.298 - }
1.299 - }
1.300 +// public final ByteBuffer encode(CharBuffer cb) {
1.301 +// try {
1.302 +// return ThreadLocalCoders.encoderFor(this)
1.303 +// .onMalformedInput(CodingErrorAction.REPLACE)
1.304 +// .onUnmappableCharacter(CodingErrorAction.REPLACE)
1.305 +// .encode(cb);
1.306 +// } catch (CharacterCodingException x) {
1.307 +// throw new Error(x); // Can't happen
1.308 +// }
1.309 +// }
1.310
1.311 /**
1.312 * Convenience method that encodes a string into bytes in this charset.
1.313 @@ -865,9 +707,9 @@
1.314 *
1.315 * @return A byte buffer containing the encoded characters
1.316 */
1.317 - public final ByteBuffer encode(String str) {
1.318 - return encode(CharBuffer.wrap(str));
1.319 - }
1.320 +// public final ByteBuffer encode(String str) {
1.321 +// return encode(CharBuffer.wrap(str));
1.322 +// }
1.323
1.324 /**
1.325 * Compares this charset to another.