1 package com.internetcds.jdbc.tds; 2 3 import java.io.UnsupportedEncodingException ; 4 import java.util.Hashtable ; 5 6 13 public class EncodingHelper { 14 public static final String cvsVersion = "$Id: EncodingHelper.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $"; 15 16 19 private String name; 20 23 private boolean wideChars; 24 28 private String converted; 29 30 33 private EncodingHelper(String name, boolean wideChars) { 34 this.name = name; 35 this.wideChars = wideChars; 36 if (!wideChars) { 37 converted = getString(convArray); 38 } 39 } 40 41 44 public byte[] getBytes(String value) { 45 try { 46 return value.getBytes(name); 47 } catch (UnsupportedEncodingException uee) { 48 return value.getBytes(); 49 } 50 } 51 52 55 public String getString(byte[] value) { 56 return getString(value, 0, value.length); 57 } 58 59 66 public String getString(byte[] value, int off, int len) { 67 try { 68 return new String (value, off, len, name); 69 } catch (UnsupportedEncodingException uee) { 70 return new String (value, off, len); 71 } 72 } 73 74 77 public boolean isDBCS() {return wideChars;} 78 79 84 public boolean canBeConverted(String value) { 85 if (isDBCS()) { 86 throw new IllegalStateException (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 101 public static EncodingHelper getHelper(String 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 115 private static byte[] convArray; 116 119 private static Hashtable knownEncodings; 120 123 private static boolean initialized; 124 125 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 (); 138 EncodingHelper e = new EncodingHelper("ISO8859_1", false); 139 knownEncodings.put("iso_1", e); 140 knownEncodings.put("cp1252", e); 141 142 try { 143 "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 159 knownEncodings.put("cp874", new EncodingHelper("MS874", true)); 161 knownEncodings.put("cp932", new EncodingHelper("MS932", true)); 163 knownEncodings.put("cp932", new EncodingHelper("MS932", true)); 165 knownEncodings.put("cp949", new EncodingHelper("MS949", true)); 167 knownEncodings.put("cp950", new EncodingHelper("MS950", true)); 169 } catch (UnsupportedEncodingException uee) { 170 } 172 173 initialized = true; 174 } 175 176 static { 177 initialize(); 178 } 179 } 180 | Popular Tags |