directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r432291 - /directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
Date Thu, 17 Aug 2006 16:40:44 GMT
Author: elecharny
Date: Thu Aug 17 09:40:44 2006
New Revision: 432291

URL: http://svn.apache.org/viewvc?rev=432291&view=rev
Log:
Replcae the jdk 1.5 dependent method getdefaultCharsetName by
the one which works with a 1.4 JDK - thanks to Jörg for the trick -

Modified:
    directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java

Modified: directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java?rev=432291&r1=432290&r2=432291&view=diff
==============================================================================
--- directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
(original)
+++ directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
Thu Aug 17 09:40:44 2006
@@ -25,6 +25,7 @@
 import java.util.List;
 import java.util.Map;
 import java.io.FileFilter;
+import java.lang.reflect.Method;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.regex.Pattern;
@@ -44,9 +45,9 @@
 public class StringTools
 {
     /** The default charset, because it's not provided by JDK 1.5 */
-    private static final String DEFAULT_CHARSET_JDK_1_4 = new OutputStreamWriter( new ByteArrayOutputStream()
).getEncoding();
-    private static final String DEFAULT_CHARSET_JDK_1_5 = Charset.defaultCharset().name();
-    private static final String JAVA_VERSION = System.getProperty( "java.version" );
+	static String defaultCharset = null;
+	
+
     
     // ~ Static fields/initializers
     // -----------------------------------------------------------------
@@ -2617,21 +2618,30 @@
     }
 
     /**
+     * Get the default charset
+     * 
      * @return The default charset
      */
     public static final String getDefaultCharsetName()
     {
-        if ( JAVA_VERSION.startsWith( "1.4" ) )
-        {
-            return DEFAULT_CHARSET_JDK_1_4;
-        }
-        else
-        {
-            return DEFAULT_CHARSET_JDK_1_5;
-        }
-    }
-
+    	if (null == defaultCharset) 
+    	{
+    		try 
+    		{
+    			// Try with jdk 1.5 method, if we are using a 1.5 jdk :)
+    			Method method = Charset.class.getMethod( "defaultCharset", new Class[0] );
+    			defaultCharset = ((Charset) method.invoke( null, new Object[0]) ).name();
+    		} 
+    		catch (Exception e) 
+    		{
+    			// fall back to old method
+    			defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
+    		}
+    	}
 
+    	return defaultCharset;
+    }
+    
     /**
      * Decodes values of attributes in the DN encoded in hex into a UTF-8 
      * String.  RFC2253 allows a DN's attribute to be encoded in hex.



Mime
View raw message