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 16 public static boolean isEncodingSupported(String text, String charsetName) 17 { 18 try 19 { 20 Charset charset = Charset.forName(charsetName); 21 CharsetEncoder ce = charset.newEncoder(); 22 return ce.canEncode(text); 24 } 26 catch(Exception e) 27 { 28 return false; 30 } 31 } 32 33 public static int getFirstEncodingError(String text, String 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 49 public static String getMinimalEncodingCharset(String text) 50 { 51 String [] charsets = new String []{"us-ascii", "iso-8859-1", "iso-8859-2", "iso-8859-4", "utf-8"}; 52 for(String cs : charsets) 53 { 54 if(isEncodingSupported(text, cs)) return cs; 55 } 56 57 System.out.println("NO CHARSET FOUND ????????????"); 59 return "utf-8"; 60 } 61 62 public static void main(String [] 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 } | Popular Tags |