KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > utils > CharsetUtils


1 package SnowMailClient.utils;
2
3 import java.util.*;
4 import java.nio.*;
5 import java.nio.charset.*;
6
7 public final class CharsetUtils
8 {
9
10   private CharsetUtils()
11   {
12   }
13
14   /** @return true if the text can be encoded using the given charset.
15   */

16   public static boolean isEncodingSupported(String JavaDoc text, String JavaDoc charsetName)
17   {
18     try
19     {
20       Charset charset = Charset.forName(charsetName);
21       CharsetEncoder ce = charset.newEncoder();
22       //ce.onUnmappableCharacter(CodingErrorAction.REPORT); // Cause exception
23
return ce.canEncode(text);
24       //return true;
25
}
26     catch(Exception JavaDoc e)
27     {
28       //System.out.println("Cannot encode "+e.getMessage());
29
return false;
30     }
31   }
32
33   public static int getFirstEncodingError(String JavaDoc text, String JavaDoc charsetName)
34   {
35       Charset charset = Charset.forName(charsetName);
36       CharsetEncoder ce = charset.newEncoder();
37       for(int i=0; i<text.length(); i++)
38       {
39         if(!ce.canEncode(text.charAt(i))) return i;
40       }
41
42       return -1;
43    }
44
45
46   /** @return a suitable charset name for encoding the text
47       try first 7bit, 8bit, 9bit.
48   */

49   public static String JavaDoc getMinimalEncodingCharset(String JavaDoc text)
50   {
51     String JavaDoc[] charsets = new String JavaDoc[]{"us-ascii", "iso-8859-1", "iso-8859-2", "iso-8859-4", "utf-8"};
52     for(String JavaDoc cs : charsets)
53     {
54       if(isEncodingSupported(text, cs)) return cs;
55     }
56
57     // SHOULD NEVER OCCURS
58
System.out.println("NO CHARSET FOUND ????????????");
59     return "utf-8";
60   }
61
62   public static void main(String JavaDoc[] a )
63   {
64     System.out.println(isEncodingSupported("éàè", "us-ascii"));
65     System.out.println(isEncodingSupported("éàèë\u0106", "iso-8859-1"));
66     System.out.println(isEncodingSupported("éàèë", "iso-8859-2"));
67
68     System.out.println(getMinimalEncodingCharset("ABC"));
69     System.out.println(getMinimalEncodingCharset("ABCéüè"));
70     System.out.println(getMinimalEncodingCharset("ABC\u0106"));
71     System.out.println(getMinimalEncodingCharset("ABC\u0106éüëà"));
72
73   }
74
75
76 } // CharsetUtils
Popular Tags