KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > internetcds > jdbc > tds > EncodingHelper


1 package com.internetcds.jdbc.tds;
2
3 import java.io.UnsupportedEncodingException JavaDoc;
4 import java.util.Hashtable JavaDoc;
5
6 /**
7  * Helper class to handle server character set conversion.
8  *
9  * @author Stefan Bodewig <a HREF="mailto:stefan.bodewig@megabit.net">stefan.bodewig@megabit.net</a>
10  *
11  * @version $Id: EncodingHelper.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $
12  */

13 public class EncodingHelper {
14     public static final String JavaDoc cvsVersion = "$Id: EncodingHelper.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $";
15
16     /**
17      * The name of the encoding.
18      */

19     private String JavaDoc name;
20     /**
21      * Is this a DBCS charset (does it need more than one byte per character)?
22      */

23     private boolean wideChars;
24     /**
25      * A String containing all characters of the charset (if this is not
26      * a DBCS charset).
27      */

28     private String JavaDoc converted;
29
30     /**
31      * private so only the static accessor can be used.
32      */

33     private EncodingHelper(String JavaDoc name, boolean wideChars) {
34         this.name = name;
35         this.wideChars = wideChars;
36         if (!wideChars) {
37             converted = getString(convArray);
38         }
39     }
40
41     /**
42      * Translate the String into a byte[] in the server's encoding.
43      */

44     public byte[] getBytes(String JavaDoc value) {
45         try {
46             return value.getBytes(name);
47         } catch (UnsupportedEncodingException JavaDoc uee) {
48             return value.getBytes();
49         }
50     }
51
52     /**
53      * Translate the byte[] from the server's encoding to a Unicode String.
54      */

55     public String JavaDoc getString(byte[] value) {
56         return getString(value, 0, value.length);
57     }
58
59     /**
60      * Translate part of the byte[] from the server's encoding to a
61      * Unicode String.
62      *
63      * The subarray starting at index off and extending to off+len-1
64      * is translated.
65      */

66     public String JavaDoc getString(byte[] value, int off, int len) {
67         try {
68             return new String JavaDoc(value, off, len, name);
69         } catch (UnsupportedEncodingException JavaDoc uee) {
70             return new String JavaDoc(value, off, len);
71         }
72     }
73
74     /**
75      * Is this a DBCS charset (does it need more than one byte per character)?
76      */

77     public boolean isDBCS() {return wideChars;}
78
79     /**
80      * Can the given String be converted to the server's charset?
81      *
82      * <p>Does not work for DBCS charsets.
83      */

84     public boolean canBeConverted(String JavaDoc value) {
85         if (isDBCS()) {
86             throw new IllegalStateException JavaDoc(name+" is a DBCS charset");
87         }
88
89         int len = value.length();
90         for (int i=0; i<len; i++) {
91             if (converted.indexOf(value.charAt(i)) == -1) {
92                 return false;
93             }
94         }
95         return true;
96     }
97
98     /**
99      * Return the helper object for the given encoding.
100      */

101     public static EncodingHelper getHelper(String JavaDoc encodingName) {
102         if (!initialized) {
103             synchronized (com.internetcds.jdbc.tds.EncodingHelper.class) {
104                 if (!initialized) {
105                     initialize();
106                 }
107             }
108         }
109         return (EncodingHelper)knownEncodings.get(encodingName);
110     }
111
112     /**
113      * Array containig the bytes 0x00 - 0xFF.
114      */

115     private static byte[] convArray;
116     /**
117      * Hashtable holding instances for all known encodings.
118      */

119     private static Hashtable JavaDoc knownEncodings;
120     /**
121      * Simple boolean to ensure we initialize once and only once.
122      */

123     private static boolean initialized;
124     
125     /**
126      * Initialize the static variables.
127      *
128      * <p>Will be called from the static block below, but some VMs
129      * (notably Microsoft's) won't run this.
130      */

131     private synchronized static void initialize() {
132         convArray = new byte[256];
133         for (int i=0; i<256; i++) {
134             convArray[i] = (byte)i;
135         }
136
137         knownEncodings = new Hashtable JavaDoc();
138         EncodingHelper e = new EncodingHelper("ISO8859_1", false);
139         knownEncodings.put("iso_1", e);
140         knownEncodings.put("cp1252", e);
141
142         try {
143             // simple test for the presence of i18n.jar
144
"a".getBytes("Cp437");
145
146             knownEncodings.put("cp437", new EncodingHelper("Cp437", false));
147             knownEncodings.put("cp850", new EncodingHelper("Cp850", false));
148             knownEncodings.put("cp1250", new EncodingHelper("Cp1250", false));
149             knownEncodings.put("cp1251", new EncodingHelper("Cp1251", false));
150             knownEncodings.put("cp1253", new EncodingHelper("Cp1253", false));
151             knownEncodings.put("cp1254", new EncodingHelper("Cp1254", false));
152             knownEncodings.put("cp1255", new EncodingHelper("Cp1255", false));
153             knownEncodings.put("cp1256", new EncodingHelper("Cp1256", false));
154             knownEncodings.put("cp1257", new EncodingHelper("Cp1257", false));
155             
156             /*
157              * XXX are the CpXXX different from MSXXX? Used MS to be save.
158              */

159             //thai
160
knownEncodings.put("cp874", new EncodingHelper("MS874", true));
161             //japanese
162
knownEncodings.put("cp932", new EncodingHelper("MS932", true));
163             //simplified chinese
164
knownEncodings.put("cp932", new EncodingHelper("MS932", true));
165             //korean
166
knownEncodings.put("cp949", new EncodingHelper("MS949", true));
167             //traditional chinese
168
knownEncodings.put("cp950", new EncodingHelper("MS950", true));
169         } catch (UnsupportedEncodingException JavaDoc uee) {
170             // i18n.jar not present, only ISO-8859-1 is available
171
}
172
173         initialized = true;
174     }
175
176     static {
177         initialize();
178     }
179 }
180
Popular Tags