harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r383867 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src: main/java/java/nio/charset/Charset.java test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
Date Tue, 07 Mar 2006 12:44:29 GMT
Author: tellison
Date: Tue Mar  7 04:44:27 2006
New Revision: 383867

URL: http://svn.apache.org/viewcvs?rev=383867&view=rev
Log:
Fix for HARMONY-149 (java.nio.charset.Charset.forName(name) return different reference value
when requiring the same Charset)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java?rev=383867&r1=383866&r2=383867&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
Tue Mar  7 04:44:27 2006
@@ -28,6 +28,7 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Locale;
@@ -139,6 +140,8 @@
 	// the aliases set
 	private final HashSet aliasesSet;
 
+	// cached Charset table
+	private static HashMap cachedCharsetTable = new HashMap();
 	/*
 	 * -------------------------------------------------------------------
 	 * Global initialization
@@ -447,34 +450,63 @@
 			throw new IllegalArgumentException();
 		}
 		checkCharsetName(charsetName);
-		// Try built-in charsets
-		Charset cs = _builtInProvider.charsetForName(charsetName);
-		if (null != cs) {
-			return cs;
-		}
-
-		// Collect all charsets provided by charset providers
-		final ClassLoader cl = getContextClassLoader();
-		if (null != cl) {
-			try {
-				// Load all configuration files
-				Enumeration e = cl
-						.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
-				// Examine each configuration file
-				while (e.hasMoreElements()) {
-					cs = searchConfiguredCharsets(charsetName, (URL) e
-							.nextElement(), cl);
-					if (null != cs) {
-						return cs;
+		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
+			final ClassLoader cl = getContextClassLoader();
+			if (null != cl) {
+				try {
+					// Load all configuration files
+					Enumeration e = cl
+							.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
+					// Examine each configuration file
+					while (e.hasMoreElements()) {
+						cs = searchConfiguredCharsets(charsetName, (URL) e
+								.nextElement(), cl);
+						if (null != cs) {
+							cacheCharset(cs);
+							return cs;
+						}
 					}
+				} catch (IOException ex) {
+					// Unexpected ClassLoader exception, ignore
 				}
-			} 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 <code>Charset</code> instance for the specified charset name.
 	 * 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java?rev=383867&r1=383866&r2=383867&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
Tue Mar  7 04:44:27 2006
@@ -130,4 +130,25 @@
 			System.setProperties(oldProps);
 		}
 	}
+	
+    /**
+	 * @tests java.nio.charset.Charset#forName(java.lang.String)
+	 */
+	public void test_forNameLjava_lang_String() {
+		/*
+		 * invoke forName two times with the same canonical name, it
+		 * should return the same reference.
+		 */
+		Charset cs1 = Charset.forName("UTF-8");
+		Charset cs2 = Charset.forName("UTF-8");
+		assertSame(cs1, cs2);
+
+		/*
+		 * test forName: invoke forName two times for the same Charset using
+		 * canonical name and alias, it should return the same reference.
+		 */
+		Charset cs3 = Charset.forName("ASCII");
+		Charset cs4 = Charset.forName("US-ASCII");
+		assertSame(cs3, cs4);
+	} 
 }



Mime
View raw message