Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 22088 invoked from network); 13 Dec 2006 16:02:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Dec 2006 16:02:45 -0000 Received: (qmail 37793 invoked by uid 500); 13 Dec 2006 16:02:53 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 37663 invoked by uid 500); 13 Dec 2006 16:02:52 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 37610 invoked by uid 99); 13 Dec 2006 16:02:52 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Dec 2006 08:02:52 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Dec 2006 08:02:40 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 9C2791A981A; Wed, 13 Dec 2006 08:01:55 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r486702 [1/4] - in /harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset: ./ spi/ Date: Wed, 13 Dec 2006 16:01:54 -0000 To: commits@harmony.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061213160155.9C2791A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tellison Date: Wed Dec 13 08:01:53 2006 New Revision: 486702 URL: http://svn.apache.org/viewvc?view=rev&rev=486702 Log: Format code (removing tabs etc), adding generics. No functional change. Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharacterCodingException.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetDecoder.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetEncoder.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CoderMalfunctionError.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CoderResult.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CodingErrorAction.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/IllegalCharsetNameException.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/MalformedInputException.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/UnmappableCharacterException.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/UnsupportedCharsetException.java harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/spi/CharsetProvider.java Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharacterCodingException.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharacterCodingException.java?view=diff&rev=486702&r1=486701&r2=486702 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharacterCodingException.java (original) +++ harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharacterCodingException.java Wed Dec 13 08:01:53 2006 @@ -17,7 +17,6 @@ package java.nio.charset; - import java.io.IOException; /** @@ -27,22 +26,16 @@ */ public class CharacterCodingException extends IOException { - /* - * ------------------------------------------------------------------- - * Constants - * ------------------------------------------------------------------- - */ - - /* - * This constant is used during deserialization to check the J2SE version - * which created the serialized object. - */ - private static final long serialVersionUID = 8421532232154627783L; // J2SE 1.4.2 + /* + * This constant is used during deserialization to check the J2SE version + * which created the serialized object. + */ + private static final long serialVersionUID = 8421532232154627783L; - /** - * Default constructor. - */ - public CharacterCodingException() { - super(); - } + /** + * Default constructor. + */ + public CharacterCodingException() { + super(); + } } Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java?view=diff&rev=486702&r1=486701&r2=486702 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java (original) +++ harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java Wed Dec 13 08:01:53 2006 @@ -77,187 +77,187 @@ */ public abstract class Charset implements Comparable { - /* - * -------------------------------------------------------------------- - * Constants - * -------------------------------------------------------------------- - */ - - /* - * the name of configuration files where charset provider class names can be - * specified. - */ - private static final String PROVIDER_CONFIGURATION_FILE_NAME = "META-INF/services/java.nio.charset.spi.CharsetProvider"; //$NON-NLS-1$ - - /* - * the encoding of configuration files - */ - private static final String PROVIDER_CONFIGURATION_FILE_ENCODING = "UTF-8"; //$NON-NLS-1$ - - /* - * the comment string used in configuration files - */ - private static final String PROVIDER_CONFIGURATION_FILE_COMMENT = "#"; //$NON-NLS-1$ - + /* + * -------------------------------------------------------------------- + * Constants + * -------------------------------------------------------------------- + */ + + /* + * the name of configuration files where charset provider class names can be + * specified. + */ + private static final String PROVIDER_CONFIGURATION_FILE_NAME = "META-INF/services/java.nio.charset.spi.CharsetProvider"; //$NON-NLS-1$ + + /* + * the encoding of configuration files + */ + private static final String PROVIDER_CONFIGURATION_FILE_ENCODING = "UTF-8"; //$NON-NLS-1$ + + /* + * the comment string used in configuration files + */ + private static final String PROVIDER_CONFIGURATION_FILE_COMMENT = "#"; //$NON-NLS-1$ + private static ClassLoader systemClassLoader; - /* - * -------------------------------------------------------------------- - * Class variables - * -------------------------------------------------------------------- - */ - - // built in provider instance, assuming thread-safe - private static CharsetProviderICU _builtInProvider = null; - - // cached built in charsets - private static TreeMap _builtInCharsets = null; - - /* - * -------------------------------------------------------------------- - * Instance variables - * -------------------------------------------------------------------- - */ - - private final String canonicalName; - - // the aliases set - private final HashSet aliasesSet; - - // cached Charset table - private static HashMap cachedCharsetTable = new HashMap(); - - // cached CharsetDecoder table - private static HashMap cachedCharsetDecoderTable = new HashMap(); - - // cached CharsetEncoder table - private static HashMap cachedCharsetEncoderTable = new HashMap(); - - /* - * ------------------------------------------------------------------- - * Global initialization - * ------------------------------------------------------------------- - */ - static { - /* - * create built-in charset provider even if no privilege to access - * charset provider. - */ - _builtInProvider = AccessController - .doPrivileged(new PrivilegedAction() { - public CharsetProviderICU run() { - return new CharsetProviderICU(); - } - }); - } - - /* - * ------------------------------------------------------------------- - * Constructors - * ------------------------------------------------------------------- - */ - - /** - * Constructs a Charset object. Duplicated aliases are - * ignored. - * - * @param canonicalName - * the canonical name of the charset - * @param aliases - * an array containing all aliases of the charset - * @throws IllegalCharsetNameException - * on an illegal value being supplied for either - * canonicalName or for any element of - * aliases. - * - */ - protected Charset(String canonicalName, String[] aliases) - throws IllegalCharsetNameException { - // throw IllegalArgumentException if name is null - if (null == canonicalName) { - throw new NullPointerException(); - } - // check whether the given canonical name is legal - checkCharsetName(canonicalName); - this.canonicalName = canonicalName; - // check each alias and put into a set - this.aliasesSet = new HashSet(); - if (null != aliases) { - for (int i = 0; i < aliases.length; i++) { - checkCharsetName(aliases[i]); - this.aliasesSet.add(aliases[i]); - } - } - } - - /* - * ------------------------------------------------------------------- - * Methods - * ------------------------------------------------------------------- - */ - - /* - * Checks whether a character is a special character that can be used in - * charset names, other than letters and digits. - */ - private static boolean isSpecial(char c) { - return ('-' == c || '.' == c || ':' == c || '_' == c); - } - - /* - * Checks whether a character is a letter (ascii) which are defined in Java - * Spec. - */ - private static boolean isLetter(char c) { - return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); - } - - /* - * Checks whether a character is a digit (ascii) which are defined in Java - * Spec. - */ - private static boolean isDigit(char c) { - return ('0' <= c && c <= '9'); - } - - /* - * Checks whether a given string is a legal charset name. The argument name - * should not be null. - */ - private static void checkCharsetName(String name) { - // An empty string is illegal charset name - if (name.length() == 0) { - throw new IllegalCharsetNameException(name); - } - // The first character must be a letter or a digit - // This is related to HARMONY-68 (won't fix) - // char first = name.charAt(0); - // if (!isLetter(first) && !isDigit(first)) { - // throw new IllegalCharsetNameException(name); - // } - // Check the remaining characters - int length = name.length(); - for (int i = 0; i < length; i++) { - char c = name.charAt(i); - if (!isLetter(c) && !isDigit(c) && !isSpecial(c)) { - throw new IllegalCharsetNameException(name); - } - } - } - - /* - * Use privileged code to get the context class loader. - */ - private static ClassLoader getContextClassLoader() { - final Thread t = Thread.currentThread(); - return AccessController - .doPrivileged(new PrivilegedAction() { - public ClassLoader run() { - return t.getContextClassLoader(); - } - }); - } - + /* + * -------------------------------------------------------------------- + * Class variables + * -------------------------------------------------------------------- + */ + + // built in provider instance, assuming thread-safe + private static CharsetProviderICU _builtInProvider = null; + + // cached built in charsets + private static TreeMap _builtInCharsets = null; + + /* + * -------------------------------------------------------------------- + * Instance variables + * -------------------------------------------------------------------- + */ + + private final String canonicalName; + + // the aliases set + private final HashSet aliasesSet; + + // cached Charset table + private static HashMap cachedCharsetTable = new HashMap(); + + // cached CharsetDecoder table + private static HashMap cachedCharsetDecoderTable = new HashMap(); + + // cached CharsetEncoder table + private static HashMap cachedCharsetEncoderTable = new HashMap(); + + /* + * ------------------------------------------------------------------- + * Global initialization + * ------------------------------------------------------------------- + */ + static { + /* + * create built-in charset provider even if no privilege to access + * charset provider. + */ + _builtInProvider = AccessController + .doPrivileged(new PrivilegedAction() { + public CharsetProviderICU run() { + return new CharsetProviderICU(); + } + }); + } + + /* + * ------------------------------------------------------------------- + * Constructors + * ------------------------------------------------------------------- + */ + + /** + * Constructs a Charset object. Duplicated aliases are + * ignored. + * + * @param canonicalName + * the canonical name of the charset + * @param aliases + * an array containing all aliases of the charset + * @throws IllegalCharsetNameException + * on an illegal value being supplied for either + * canonicalName or for any element of + * aliases. + * + */ + protected Charset(String canonicalName, String[] aliases) + throws IllegalCharsetNameException { + // throw IllegalArgumentException if name is null + if (null == canonicalName) { + throw new NullPointerException(); + } + // check whether the given canonical name is legal + checkCharsetName(canonicalName); + this.canonicalName = canonicalName; + // check each alias and put into a set + this.aliasesSet = new HashSet(); + if (null != aliases) { + for (int i = 0; i < aliases.length; i++) { + checkCharsetName(aliases[i]); + this.aliasesSet.add(aliases[i]); + } + } + } + + /* + * ------------------------------------------------------------------- + * Methods + * ------------------------------------------------------------------- + */ + + /* + * Checks whether a character is a special character that can be used in + * charset names, other than letters and digits. + */ + private static boolean isSpecial(char c) { + return ('-' == c || '.' == c || ':' == c || '_' == c); + } + + /* + * Checks whether a character is a letter (ascii) which are defined in Java + * Spec. + */ + private static boolean isLetter(char c) { + return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); + } + + /* + * Checks whether a character is a digit (ascii) which are defined in Java + * Spec. + */ + private static boolean isDigit(char c) { + return ('0' <= c && c <= '9'); + } + + /* + * Checks whether a given string is a legal charset name. The argument name + * should not be null. + */ + private static void checkCharsetName(String name) { + // An empty string is illegal charset name + if (name.length() == 0) { + throw new IllegalCharsetNameException(name); + } + // The first character must be a letter or a digit + // This is related to HARMONY-68 (won't fix) + // char first = name.charAt(0); + // if (!isLetter(first) && !isDigit(first)) { + // throw new IllegalCharsetNameException(name); + // } + // Check the remaining characters + int length = name.length(); + for (int i = 0; i < length; i++) { + char c = name.charAt(i); + if (!isLetter(c) && !isDigit(c) && !isSpecial(c)) { + throw new IllegalCharsetNameException(name); + } + } + } + + /* + * Use privileged code to get the context class loader. + */ + private static ClassLoader getContextClassLoader() { + final Thread t = Thread.currentThread(); + return AccessController + .doPrivileged(new PrivilegedAction() { + public ClassLoader run() { + return t.getContextClassLoader(); + } + }); + } + /* * Use privileged code to get the system class loader. */ @@ -272,55 +272,55 @@ } } - /* - * Add the charsets supported by the given provider to the map. - */ - private static void addCharsets(CharsetProvider cp, - TreeMap charsets) { - Iterator it = cp.charsets(); - while (it.hasNext()) { - Charset cs = (Charset) it.next(); - // Only new charsets will be added - if (!charsets.containsKey(cs.name())) { - charsets.put(cs.name(), cs); - } - } - } - - /* - * Trim comment string, and then trim white spaces. - */ - private static String trimClassName(String name) { - String trimedName = name; - int index = name.indexOf(PROVIDER_CONFIGURATION_FILE_COMMENT); - // Trim comments - if (index != -1) { - trimedName = name.substring(0, index); - } - return trimedName.trim(); - } - - /* - * Read a configuration file and add the charsets supported by the providers - * specified by this configuration file to the map. - */ - private static void loadConfiguredCharsets(URL configFile, + /* + * Add the charsets supported by the given provider to the map. + */ + private static void addCharsets(CharsetProvider cp, + TreeMap charsets) { + Iterator it = cp.charsets(); + while (it.hasNext()) { + Charset cs = it.next(); + // Only new charsets will be added + if (!charsets.containsKey(cs.name())) { + charsets.put(cs.name(), cs); + } + } + } + + /* + * Trim comment string, and then trim white spaces. + */ + private static String trimClassName(String name) { + String trimedName = name; + int index = name.indexOf(PROVIDER_CONFIGURATION_FILE_COMMENT); + // Trim comments + if (index != -1) { + trimedName = name.substring(0, index); + } + return trimedName.trim(); + } + + /* + * Read a configuration file and add the charsets supported by the providers + * specified by this configuration file to the map. + */ + private static void loadConfiguredCharsets(URL configFile, ClassLoader contextClassLoader, TreeMap charsets) { - BufferedReader reader = null; - try { - InputStream is = configFile.openStream(); - // Read each line for charset provider class names - reader = new BufferedReader(new InputStreamReader(is, - PROVIDER_CONFIGURATION_FILE_ENCODING)); - String providerClassName = reader.readLine(); - while (null != providerClassName) { - providerClassName = trimClassName(providerClassName); - // Skip comments and blank lines - if (providerClassName.length() > 0) { // Non empty string - // Load the charset provider - Object cp = null; - try { - Class c = Class.forName(providerClassName, true, + BufferedReader reader = null; + try { + InputStream is = configFile.openStream(); + // Read each line for charset provider class names + reader = new BufferedReader(new InputStreamReader(is, + PROVIDER_CONFIGURATION_FILE_ENCODING)); + String providerClassName = reader.readLine(); + while (null != providerClassName) { + providerClassName = trimClassName(providerClassName); + // Skip comments and blank lines + if (providerClassName.length() > 0) { // Non empty string + // Load the charset provider + Object cp = null; + try { + Class c = Class.forName(providerClassName, true, contextClassLoader); cp = c.newInstance(); } catch (Exception ex) { @@ -328,62 +328,63 @@ // classloader failed to load config file. try { getSystemClassLoader(); - Class c = Class.forName(providerClassName, true, + Class c = Class.forName(providerClassName, true, systemClassLoader); cp = c.newInstance(); } catch (Exception e) { throw new Error(e.getMessage(), e); } } - // Put the charsets supported by this provider into the map - addCharsets((CharsetProvider) cp, charsets); - } - // Read the next line of the config file - providerClassName = reader.readLine(); - } - } catch (IOException ex) { - // Can't read this configuration file, ignore - } finally { - try { - if (null != reader) { - reader.close(); - } - } catch (IOException ex) { - // Ignore closing exception - } - } - } - - /** - * Gets a map of all available charsets supported by the runtime. - *

- * The returned map contains mappings from canonical names to corresponding - * instances of Charset. The canonical names can be - * considered as case-insensitive. - *

- * - * @return an unmodifiable map of all available charsets supported by the - * runtime - */ - public static SortedMap availableCharsets() { - // Initialize the built-in charsets map cache if necessary - if (null == _builtInCharsets) { - synchronized (Charset.class) { - if (null == _builtInCharsets) { - _builtInCharsets = new TreeMap( - IgnoreCaseComparator.getInstance()); - _builtInProvider.putCharsets(_builtInCharsets); - } - } - } - - // Add built-in charsets - TreeMap charsets = (TreeMap) _builtInCharsets - .clone(); + // Put the charsets supported by this provider into the map + addCharsets((CharsetProvider) cp, charsets); + } + // Read the next line of the config file + providerClassName = reader.readLine(); + } + } catch (IOException ex) { + // Can't read this configuration file, ignore + } finally { + try { + if (null != reader) { + reader.close(); + } + } catch (IOException ex) { + // Ignore closing exception + } + } + } + + /** + * Gets a map of all available charsets supported by the runtime. + *

+ * The returned map contains mappings from canonical names to corresponding + * instances of Charset. The canonical names can be + * considered as case-insensitive. + *

+ * + * @return an unmodifiable map of all available charsets supported by the + * runtime + */ + @SuppressWarnings("unchecked") + public static SortedMap availableCharsets() { + // Initialize the built-in charsets map cache if necessary + if (null == _builtInCharsets) { + synchronized (Charset.class) { + if (null == _builtInCharsets) { + _builtInCharsets = new TreeMap( + IgnoreCaseComparator.getInstance()); + _builtInProvider.putCharsets(_builtInCharsets); + } + } + } - // Collect all charsets provided by charset providers + // Add built-in charsets + TreeMap charsets = (TreeMap) _builtInCharsets + .clone(); + + // Collect all charsets provided by charset providers ClassLoader contextClassLoader = getContextClassLoader(); - Enumeration e = null; + Enumeration e = null; try { if (null != contextClassLoader) { e = contextClassLoader @@ -395,101 +396,102 @@ } // Examine each configuration file while (e.hasMoreElements()) { - loadConfiguredCharsets((URL) e.nextElement(), - contextClassLoader, charsets); + loadConfiguredCharsets(e.nextElement(), contextClassLoader, + charsets); } } catch (IOException ex) { // Unexpected ClassLoader exception, ignore - } - return Collections.unmodifiableSortedMap(charsets); - } - - /* - * Read a configuration file and try to find the desired charset among those - * which are supported by the providers specified in this configuration - * file. - */ - private static Charset searchConfiguredCharsets(String charsetName, + } + return Collections.unmodifiableSortedMap(charsets); + } + + /* + * Read a configuration file and try to find the desired charset among those + * which are supported by the providers specified in this configuration + * file. + */ + private static Charset searchConfiguredCharsets(String charsetName, ClassLoader contextClassLoader, URL configFile) { - BufferedReader reader = null; - try { - InputStream is = configFile.openStream(); - // Read each line for charset provider class names - reader = new BufferedReader(new InputStreamReader(is, - PROVIDER_CONFIGURATION_FILE_ENCODING)); - String providerClassName = reader.readLine(); - while (null != providerClassName) { - providerClassName = trimClassName(providerClassName); - if (providerClassName.length() > 0) { // Non empty string - // Load the charset provider - Object cp = null; - try { - Class c = Class.forName(providerClassName, true, contextClassLoader); + BufferedReader reader = null; + try { + InputStream is = configFile.openStream(); + // Read each line for charset provider class names + reader = new BufferedReader(new InputStreamReader(is, + PROVIDER_CONFIGURATION_FILE_ENCODING)); + String providerClassName = reader.readLine(); + while (null != providerClassName) { + providerClassName = trimClassName(providerClassName); + if (providerClassName.length() > 0) { // Non empty string + // Load the charset provider + Object cp = null; + try { + Class c = Class.forName(providerClassName, true, + contextClassLoader); cp = c.newInstance(); } catch (Exception ex) { // try to use system classloader when context // classloader failed to load config file. try { getSystemClassLoader(); - Class c = Class.forName(providerClassName, true, + Class c = Class.forName(providerClassName, true, systemClassLoader); cp = c.newInstance(); } catch (Exception e) { throw new Error(e.getMessage(), e); } } - // Try to get the desired charset from this provider - Charset cs = ((CharsetProvider) cp) - .charsetForName(charsetName); - if (null != cs) { - return cs; - } - } - // Read the next line of the config file - providerClassName = reader.readLine(); - } - return null; - } catch (IOException ex) { - // Can't read this configuration file - return null; - } finally { - try { - if (null != reader) { - reader.close(); - } - } catch (IOException ex) { - // Ignore closing exception - } - } - } - - /* - * Gets a Charset instance for the specified charset name. If - * the charset is not supported, returns null instead of throwing an - * exception. - */ - private static Charset forNameInternal(String charsetName) - throws IllegalCharsetNameException { - if (null == charsetName) { - throw new IllegalArgumentException(); - } - checkCharsetName(charsetName); - synchronized (Charset.class) { - // Try to get Charset from cachedCharsetTable - Charset cs = getCachedCharset(charsetName); - if (null != cs) { - return cs; - } - // Try built-in charsets - cs = _builtInProvider.charsetForName(charsetName); - if (null != cs) { - cacheCharset(cs); - return cs; - } + // Try to get the desired charset from this provider + Charset cs = ((CharsetProvider) cp) + .charsetForName(charsetName); + if (null != cs) { + return cs; + } + } + // Read the next line of the config file + providerClassName = reader.readLine(); + } + return null; + } catch (IOException ex) { + // Can't read this configuration file + return null; + } finally { + try { + if (null != reader) { + reader.close(); + } + } catch (IOException ex) { + // Ignore closing exception + } + } + } + + /* + * Gets a Charset instance for the specified charset name. If + * the charset is not supported, returns null instead of throwing an + * exception. + */ + private static Charset forNameInternal(String charsetName) + throws IllegalCharsetNameException { + if (null == charsetName) { + throw new IllegalArgumentException(); + } + checkCharsetName(charsetName); + synchronized (Charset.class) { + // Try to get Charset from cachedCharsetTable + Charset cs = getCachedCharset(charsetName); + if (null != cs) { + return cs; + } + // Try built-in charsets + cs = _builtInProvider.charsetForName(charsetName); + if (null != cs) { + cacheCharset(cs); + return cs; + } // Collect all charsets provided by charset providers ClassLoader contextClassLoader = getContextClassLoader(); - Enumeration e = null; + Enumeration e = null; try { if (null != contextClassLoader) { e = contextClassLoader @@ -502,7 +504,7 @@ // Examine each configuration file while (e.hasMoreElements()) { cs = searchConfiguredCharsets(charsetName, - contextClassLoader, (URL) e.nextElement()); + contextClassLoader, e.nextElement()); if (null != cs) { cacheCharset(cs); return cs; @@ -510,353 +512,356 @@ } } catch (IOException ex) { // Unexpected ClassLoader exception, ignore - } - } - return null; - } - - /* - * save charset into cachedCharsetTable - */ - private static void cacheCharset(Charset cs) { - cachedCharsetTable.put(cs.name(), cs); - Set aliasesSet = cs.aliases(); - if (null != aliasesSet) { - Iterator iter = aliasesSet.iterator(); - while (iter.hasNext()) { - String alias = (String) iter.next(); - cachedCharsetTable.put(alias, cs); - } - } - } - - /* - * get cached charset reference by name - */ - private static Charset getCachedCharset(String name) { - return (Charset) cachedCharsetTable.get(name); - } - - /** - * Gets a Charset instance for the specified charset name. - * - * @param charsetName - * the name of the charset - * @return a Charset instance for the specified charset name - * @throws IllegalCharsetNameException - * If the specified charset name is illegal. - * @throws UnsupportedCharsetException - * If the desired charset is not supported by this runtime. - */ - public static Charset forName(String charsetName) - throws IllegalCharsetNameException, UnsupportedCharsetException { - Charset c = forNameInternal(charsetName); - if (null == c) { - throw new UnsupportedCharsetException(charsetName); - } - return c; - } - - /** - * Determines whether the specified charset is supported by this runtime. - * - * @param charsetName - * the name of the charset - * @return true if the specified charset is supported, otherwise false - * @throws IllegalCharsetNameException - * If the specified charset name is illegal. - */ - public static boolean isSupported(String charsetName) - throws IllegalCharsetNameException { - Charset cs = forNameInternal(charsetName); - return (null != cs); - } - - /** - * Determines whether this charset is a super set of the given charset. - * - * @param charset - * a given charset - * @return true if this charset is a super set of the given charset, - * otherwise false - */ - public abstract boolean contains(Charset charset); - - /** - * Gets a new instance of encoder for this charset. - * - * @return a new instance of encoder for this charset - */ - public abstract CharsetEncoder newEncoder(); - - /** - * Gets a new instance of decoder for this charset. - * - * @return a new instance of decoder for this charset - */ - public abstract CharsetDecoder newDecoder(); - - /** - * Gets the canonical name of this charset. - * - * @return this charset's name in canonical form. - */ - public final String name() { - return this.canonicalName; - } - - /** - * Gets the set of this charset's aliases. - * - * @return an unmodifiable set of this charset's aliases - */ - public final Set aliases() { - return Collections.unmodifiableSet(this.aliasesSet); - } - - /** - * Gets the name of this charset for the default locale. - * - * @return the name of this charset for the default locale - */ - public String displayName() { - return this.canonicalName; - } - - /** - * Gets the name of this charset for the specified locale. - * - * @param l - * a certain locale - * @return the name of this charset for the specified locale - */ - public String displayName(Locale l) { - return this.canonicalName; - } - - /** - * Answers whether this charset is known to be registered in the IANA - * Charset Registry. - * - * @return true if the charset is known to be registered, otherwise returns - * false. - */ - public final boolean isRegistered() { - return !canonicalName.startsWith("x-") //$NON-NLS-1$ - && !canonicalName.startsWith("X-"); //$NON-NLS-1$ - } - - /** - * Answers true if this charset supports encoding, otherwise false. - * - * @return true - */ - public boolean canEncode() { - return true; - } - - /** - * Encodes the content of the give character buffer and outputs to a byte - * buffer that is to be returned. - *

- * The default action in case of encoding errors is - * CodingErrorAction.REPLACE. - *

- * - * @param buffer - * the character buffer containing the content to be encoded - * @return the result of the encoding - */ - synchronized public final ByteBuffer encode(CharBuffer buffer) { - CharsetEncoder e = getCachedCharsetEncoder(canonicalName); - try { - synchronized (e) { - return e.encode(buffer); - } - } catch (CharacterCodingException ex) { - throw new Error(ex.getMessage(), ex); - } - } - - /* - * get cached CharsetEncoder by canonical name - */ - private CharsetEncoder getCachedCharsetEncoder(String name) { - synchronized (cachedCharsetEncoderTable) { - CharsetEncoder e = (CharsetEncoder) cachedCharsetEncoderTable - .get(name); - if (null == e) { - e = this.newEncoder(); - e.onMalformedInput(CodingErrorAction.REPLACE); - e.onUnmappableCharacter(CodingErrorAction.REPLACE); - cachedCharsetEncoderTable.put(name, e); - } - return e; - } - } - - /** - * Encodes a string and outputs to a byte buffer that is to be returned. - *

- * The default action in case of encoding errors is - * CodingErrorAction.REPLACE. - *

- * - * @param s - * the string to be encoded - * @return the result of the encoding - */ - public final ByteBuffer encode(String s) { - return encode(CharBuffer.wrap(s)); - } - - /** - * Decodes the content of the give byte buffer and outputs to a character - * buffer that is to be returned. - *

- * The default action in case of decoding errors is - * CodingErrorAction.REPLACE. - *

- * - * @param buffer - * the byte buffer containing the content to be decoded - * @return a character buffer containing the output of the decoding - */ - public final CharBuffer decode(ByteBuffer buffer) { - CharsetDecoder d = getCachedCharsetDecoder(canonicalName); - try { - synchronized (d) { - return d.decode(buffer); - } - } catch (CharacterCodingException ex) { - throw new Error(ex.getMessage(), ex); - } - } - - /* - * get cached CharsetDecoder by canonical name - */ - private CharsetDecoder getCachedCharsetDecoder(String name) { - synchronized (cachedCharsetDecoderTable) { - CharsetDecoder d = (CharsetDecoder) cachedCharsetDecoderTable - .get(name); - if (null == d) { - d = this.newDecoder(); - d.onMalformedInput(CodingErrorAction.REPLACE); - d.onUnmappableCharacter(CodingErrorAction.REPLACE); - cachedCharsetDecoderTable.put(name, d); - } - return d; - } - } - - /* - * ------------------------------------------------------------------- - * Methods implementing parent interface Comparable - * ------------------------------------------------------------------- - */ - - /** - * Compares this charset with the given charset. - * - * @param charset - * the given object to be compared with - * @return a negative integer if less than the given object, a positive - * integer if larger than it, or 0 if equal to it - */ - public final int compareTo(Charset charset) { - return this.canonicalName.compareToIgnoreCase(charset.canonicalName); - } - - /* - * ------------------------------------------------------------------- - * Methods overriding parent class Object - * ------------------------------------------------------------------- - */ - - /** - * Determines whether this charset equals to the given object. They are - * considered to be equal if they have the same canonical name. - * - * @param obj - * the given object to be compared with - * @return true if they have the same canonical name, otherwise false - */ - public final boolean equals(Object obj) { - if (obj instanceof Charset) { - Charset that = (Charset) obj; - return this.canonicalName.equals(that.canonicalName); - } - return false; - } - - /** - * Gets the hash code of this charset. - * - * @return the hash code of this charset - */ - public final int hashCode() { - return this.canonicalName.hashCode(); - } - - /** - * Gets a string representation of this charset. Usually this contains the - * canonical name of the charset. - * - * @return a string representation of this charset - */ - public final String toString() { - return "Charset[" + this.canonicalName + "]"; //$NON-NLS-1$//$NON-NLS-2$ - } - - /** - * Gets the system default charset from jvm. - * - * @return the default charset - */ - public static Charset defaultCharset() { - Charset defaultCharset = null; - String encoding = AccessController - .doPrivileged(new PrivilegedAction() { - public String run() { - return System.getProperty("file.encoding"); //$NON-NLS-1$ - } - }); - try { - defaultCharset = Charset.forName(encoding); - } catch (UnsupportedCharsetException e) { - defaultCharset = Charset.forName("UTF-8"); //$NON-NLS-1$ - } - return defaultCharset; - } - - /** - * A comparator that ignores case. - */ - static class IgnoreCaseComparator implements Comparator { - - // the singleton - private static Comparator c = new IgnoreCaseComparator(); - - /* - * Default constructor. - */ - private IgnoreCaseComparator() { - // no action - } - - /* - * Gets a single instance. - */ - public static Comparator getInstance() { - return c; - } - - /* - * Compares two strings ignoring case. - */ - public int compare(String s1, String s2) { - return s1.compareToIgnoreCase(s2); - } - } + } + } + return null; + } + + /* + * save charset into cachedCharsetTable + */ + private static void cacheCharset(Charset cs) { + cachedCharsetTable.put(cs.name(), cs); + Set aliasesSet = cs.aliases(); + if (null != aliasesSet) { + Iterator iter = aliasesSet.iterator(); + while (iter.hasNext()) { + String alias = iter.next(); + cachedCharsetTable.put(alias, cs); + } + } + } + + /* + * get cached charset reference by name + */ + private static Charset getCachedCharset(String name) { + return cachedCharsetTable.get(name); + } + + /** + * Gets a Charset instance for the specified charset name. + * + * @param charsetName + * the name of the charset + * @return a Charset instance for the specified charset name + * @throws IllegalCharsetNameException + * If the specified charset name is illegal. + * @throws UnsupportedCharsetException + * If the desired charset is not supported by this runtime. + */ + public static Charset forName(String charsetName) + throws IllegalCharsetNameException, UnsupportedCharsetException { + Charset c = forNameInternal(charsetName); + if (null == c) { + throw new UnsupportedCharsetException(charsetName); + } + return c; + } + + /** + * Determines whether the specified charset is supported by this runtime. + * + * @param charsetName + * the name of the charset + * @return true if the specified charset is supported, otherwise false + * @throws IllegalCharsetNameException + * If the specified charset name is illegal. + */ + public static boolean isSupported(String charsetName) + throws IllegalCharsetNameException { + Charset cs = forNameInternal(charsetName); + return (null != cs); + } + + /** + * Determines whether this charset is a super set of the given charset. + * + * @param charset + * a given charset + * @return true if this charset is a super set of the given charset, + * otherwise false + */ + public abstract boolean contains(Charset charset); + + /** + * Gets a new instance of encoder for this charset. + * + * @return a new instance of encoder for this charset + */ + public abstract CharsetEncoder newEncoder(); + + /** + * Gets a new instance of decoder for this charset. + * + * @return a new instance of decoder for this charset + */ + public abstract CharsetDecoder newDecoder(); + + /** + * Gets the canonical name of this charset. + * + * @return this charset's name in canonical form. + */ + public final String name() { + return this.canonicalName; + } + + /** + * Gets the set of this charset's aliases. + * + * @return an unmodifiable set of this charset's aliases + */ + public final Set aliases() { + return Collections.unmodifiableSet(this.aliasesSet); + } + + /** + * Gets the name of this charset for the default locale. + * + * @return the name of this charset for the default locale + */ + public String displayName() { + return this.canonicalName; + } + + /** + * Gets the name of this charset for the specified locale. + * + * @param l + * a certain locale + * @return the name of this charset for the specified locale + */ + public String displayName(Locale l) { + return this.canonicalName; + } + + /** + * Answers whether this charset is known to be registered in the IANA + * Charset Registry. + * + * @return true if the charset is known to be registered, otherwise returns + * false. + */ + public final boolean isRegistered() { + return !canonicalName.startsWith("x-") //$NON-NLS-1$ + && !canonicalName.startsWith("X-"); //$NON-NLS-1$ + } + + /** + * Answers true if this charset supports encoding, otherwise false. + * + * @return true + */ + public boolean canEncode() { + return true; + } + + /** + * Encodes the content of the give character buffer and outputs to a byte + * buffer that is to be returned. + *

+ * The default action in case of encoding errors is + * CodingErrorAction.REPLACE. + *

+ * + * @param buffer + * the character buffer containing the content to be encoded + * @return the result of the encoding + */ + synchronized public final ByteBuffer encode(CharBuffer buffer) { + CharsetEncoder e = getCachedCharsetEncoder(canonicalName); + try { + synchronized (e) { + return e.encode(buffer); + } + } catch (CharacterCodingException ex) { + throw new Error(ex.getMessage(), ex); + } + } + + /* + * get cached CharsetEncoder by canonical name + */ + private CharsetEncoder getCachedCharsetEncoder(String name) { + synchronized (cachedCharsetEncoderTable) { + CharsetEncoder e = cachedCharsetEncoderTable + .get(name); + if (null == e) { + e = this.newEncoder(); + e.onMalformedInput(CodingErrorAction.REPLACE); + e.onUnmappableCharacter(CodingErrorAction.REPLACE); + cachedCharsetEncoderTable.put(name, e); + } + return e; + } + } + + /** + * Encodes a string and outputs to a byte buffer that is to be returned. + *

+ * The default action in case of encoding errors is + * CodingErrorAction.REPLACE. + *

+ * + * @param s + * the string to be encoded + * @return the result of the encoding + */ + public final ByteBuffer encode(String s) { + return encode(CharBuffer.wrap(s)); + } + + /** + * Decodes the content of the give byte buffer and outputs to a character + * buffer that is to be returned. + *

+ * The default action in case of decoding errors is + * CodingErrorAction.REPLACE. + *

+ * + * @param buffer + * the byte buffer containing the content to be decoded + * @return a character buffer containing the output of the decoding + */ + public final CharBuffer decode(ByteBuffer buffer) { + CharsetDecoder d = getCachedCharsetDecoder(canonicalName); + try { + synchronized (d) { + return d.decode(buffer); + } + } catch (CharacterCodingException ex) { + throw new Error(ex.getMessage(), ex); + } + } + + /* + * get cached CharsetDecoder by canonical name + */ + private CharsetDecoder getCachedCharsetDecoder(String name) { + synchronized (cachedCharsetDecoderTable) { + CharsetDecoder d = cachedCharsetDecoderTable + .get(name); + if (null == d) { + d = this.newDecoder(); + d.onMalformedInput(CodingErrorAction.REPLACE); + d.onUnmappableCharacter(CodingErrorAction.REPLACE); + cachedCharsetDecoderTable.put(name, d); + } + return d; + } + } + + /* + * ------------------------------------------------------------------- + * Methods implementing parent interface Comparable + * ------------------------------------------------------------------- + */ + + /** + * Compares this charset with the given charset. + * + * @param charset + * the given object to be compared with + * @return a negative integer if less than the given object, a positive + * integer if larger than it, or 0 if equal to it + */ + public final int compareTo(Charset charset) { + return this.canonicalName.compareToIgnoreCase(charset.canonicalName); + } + + /* + * ------------------------------------------------------------------- + * Methods overriding parent class Object + * ------------------------------------------------------------------- + */ + + /** + * Determines whether this charset equals to the given object. They are + * considered to be equal if they have the same canonical name. + * + * @param obj + * the given object to be compared with + * @return true if they have the same canonical name, otherwise false + */ + @Override + public final boolean equals(Object obj) { + if (obj instanceof Charset) { + Charset that = (Charset) obj; + return this.canonicalName.equals(that.canonicalName); + } + return false; + } + + /** + * Gets the hash code of this charset. + * + * @return the hash code of this charset + */ + @Override + public final int hashCode() { + return this.canonicalName.hashCode(); + } + + /** + * Gets a string representation of this charset. Usually this contains the + * canonical name of the charset. + * + * @return a string representation of this charset + */ + @Override + public final String toString() { + return "Charset[" + this.canonicalName + "]"; //$NON-NLS-1$//$NON-NLS-2$ + } + + /** + * Gets the system default charset from jvm. + * + * @return the default charset + */ + public static Charset defaultCharset() { + Charset defaultCharset = null; + String encoding = AccessController + .doPrivileged(new PrivilegedAction() { + public String run() { + return System.getProperty("file.encoding"); //$NON-NLS-1$ + } + }); + try { + defaultCharset = Charset.forName(encoding); + } catch (UnsupportedCharsetException e) { + defaultCharset = Charset.forName("UTF-8"); //$NON-NLS-1$ + } + return defaultCharset; + } + + /** + * A comparator that ignores case. + */ + static class IgnoreCaseComparator implements Comparator { + + // the singleton + private static Comparator c = new IgnoreCaseComparator(); + + /* + * Default constructor. + */ + private IgnoreCaseComparator() { + // no action + } + + /* + * Gets a single instance. + */ + public static Comparator getInstance() { + return c; + } + + /* + * Compares two strings ignoring case. + */ + public int compare(String s1, String s2) { + return s1.compareToIgnoreCase(s2); + } + } }