1 19 package com.mysql.jdbc; 20 21 import java.io.UnsupportedEncodingException ; 22 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 27 34 public class SingleByteCharsetConverter { 35 private static byte[] unknownCharsMap = new byte[65536]; 39 private static final int BYTE_RANGE = (1 + Byte.MAX_VALUE) - Byte.MIN_VALUE; 40 private static final Map CONVERTER_MAP = new HashMap (); 41 private static byte[] allBytes = new byte[BYTE_RANGE]; 42 43 static { 44 for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { 45 allBytes[i - Byte.MIN_VALUE] = (byte) i; 46 } 47 48 for (int i = 0; i < unknownCharsMap.length; i++) { 49 unknownCharsMap[i] = (byte) '?'; } 51 } 52 53 private char[] byteToChars = new char[BYTE_RANGE]; 54 private byte[] charToByteMap = new byte[65536]; 55 56 64 private SingleByteCharsetConverter(String encodingName) 65 throws UnsupportedEncodingException { 66 String allBytesString = new String (allBytes, 0, BYTE_RANGE, encodingName); 67 int allBytesLen = allBytesString.length(); 68 69 System.arraycopy(unknownCharsMap, 0, charToByteMap, 0, 70 charToByteMap.length); 71 72 for (int i = 0; (i < BYTE_RANGE) && (i < allBytesLen); i++) { 73 char c = allBytesString.charAt(i); 74 byteToChars[i] = c; 75 charToByteMap[c] = allBytes[i]; 76 } 77 } 78 79 89 public static synchronized SingleByteCharsetConverter getInstance( 90 String encodingName) throws UnsupportedEncodingException { 91 SingleByteCharsetConverter instance = (SingleByteCharsetConverter) CONVERTER_MAP 92 .get(encodingName); 93 94 if (instance == null) { 95 instance = initCharset(encodingName); 96 } 97 98 return instance; 99 } 100 101 113 public static SingleByteCharsetConverter initCharset( 114 String javaEncodingName) throws UnsupportedEncodingException { 115 String mysqlEncodingName = (String ) CharsetMapping.JAVA_TO_MYSQL_CHARSET_MAP 116 .get(javaEncodingName); 117 118 if (mysqlEncodingName == null) { 119 return null; 120 } 121 122 if (CharsetMapping.MULTIBYTE_CHARSETS.containsKey(mysqlEncodingName)) { 123 return null; 124 } 125 126 SingleByteCharsetConverter converter = new SingleByteCharsetConverter(javaEncodingName); 127 128 CONVERTER_MAP.put(javaEncodingName, converter); 129 130 return converter; 131 } 132 133 143 public static String toStringDefaultEncoding(byte[] buffer, int startPos, 144 int length) { 145 return new String (buffer, startPos, length); 146 } 147 148 155 public final byte[] toBytes(String s) { 156 if (s == null) { 157 return null; 158 } 159 160 int length = s.length(); 161 byte[] bytes = new byte[length]; 162 163 for (int i = 0; i < length; i++) { 164 char c = s.charAt(i); 165 bytes[i] = charToByteMap[c]; 166 } 167 168 return bytes; 169 } 170 171 private final static byte[] EMPTY_BYTE_ARRAY = new byte[0]; 172 173 182 public final byte[] toBytes(String s, int offset, int length) { 183 if (s == null) { 184 return null; 185 } 186 187 if (length == 0) { 188 return EMPTY_BYTE_ARRAY; 189 } 190 191 int stringLength = s.length(); 192 byte[] bytes = new byte[length]; 193 194 195 for (int i = 0; (i < length); i++) { 196 char c = s.charAt(i + offset); 197 bytes[i] = charToByteMap[c]; 198 } 199 200 return bytes; 201 } 202 203 211 public final String toString(byte[] buffer) { 212 return toString(buffer, 0, buffer.length); 213 } 214 215 225 public final String toString(byte[] buffer, int startPos, int length) { 226 char[] charArray = new char[length]; 227 int readpoint = startPos; 228 229 for (int i = 0; i < length; i++) { 230 charArray[i] = byteToChars[(int) buffer[readpoint] - Byte.MIN_VALUE]; 231 readpoint++; 232 } 233 234 return new String (charArray); 235 } 236 } 237 | Popular Tags |