View Javadoc

1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import java.io.ByteArrayOutputStream;
14  import java.io.OutputStreamWriter;
15  import java.lang.reflect.Method;
16  import java.nio.charset.Charset;
17  
18  public class CharSetUtils extends org.apache.commons.lang.CharSetUtils
19  {
20      public static String defaultCharsetName()
21      {
22          try
23          {
24              if (SystemUtils.IS_JAVA_1_4)
25              {
26                  return new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
27              }
28              else
29              {
30                  Class target = Charset.class;
31                  Method defaultCharset = target.getMethod("defaultCharset", ArrayUtils.EMPTY_CLASS_ARRAY);
32                  Charset cs = (Charset) defaultCharset.invoke(target, (Class[]) null);
33                  return cs.name();
34              }
35          }
36          catch (Exception ex)
37          {
38              throw new Error(ex);
39          }
40      }
41  }
42  
43