📄 charsetmapping.java
字号:
/* Copyright (C) 2002-2006 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.mysql.jdbc;import java.sql.SQLException;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.TreeMap;/** * Mapping between MySQL charset names and Java charset names. I've investigated * placing these in a .properties file, but unfortunately under most appservers * this complicates configuration because the security policy needs to be * changed by the user to allow the driver to read them :( * * @author Mark Matthews */public class CharsetMapping { private static final Properties CHARSET_CONFIG = new Properties(); /** * Map of MySQL-4.1 charset indexes to Java encoding names */ public static final String[] INDEX_TO_CHARSET; /** * Map of MySQL-4.1 collation index to collation names */ public static final String[] INDEX_TO_COLLATION; /** Mapping of Java charset names to MySQL charset names */ private static final Map JAVA_TO_MYSQL_CHARSET_MAP; private static final Map JAVA_UC_TO_MYSQL_CHARSET_MAP; private static final Map ERROR_MESSAGE_FILE_TO_MYSQL_CHARSET_MAP; /** Map/List of multibyte character sets (using MySQL names) */ private static final Map MULTIBYTE_CHARSETS; private static final Map MYSQL_TO_JAVA_CHARSET_MAP; private static final String NOT_USED = "ISO8859_1"; // punting for not-used character sets public static final Map STATIC_CHARSET_TO_NUM_BYTES_MAP; static { HashMap tempNumBytesMap = new HashMap(); tempNumBytesMap.put("big5", new Integer(2)); tempNumBytesMap.put("dec8" , new Integer(1)); tempNumBytesMap.put("cp850", new Integer(1)); tempNumBytesMap.put("hp8", new Integer(1)); tempNumBytesMap.put("koi8r", new Integer(1)); tempNumBytesMap.put("latin1", new Integer(1)); tempNumBytesMap.put("latin2", new Integer(1)); tempNumBytesMap.put("swe7", new Integer(1)); tempNumBytesMap.put("ascii", new Integer(1)); tempNumBytesMap.put("ujis", new Integer(3)); tempNumBytesMap.put("sjis", new Integer(2)); tempNumBytesMap.put("hebrew", new Integer(1)); tempNumBytesMap.put("tis620", new Integer(1)); tempNumBytesMap.put("euckr", new Integer(2)); tempNumBytesMap.put("koi8u", new Integer(1)); tempNumBytesMap.put("gb2312", new Integer(2)); tempNumBytesMap.put("greek", new Integer(1)); tempNumBytesMap.put("cp1250", new Integer(1)); tempNumBytesMap.put("gbk", new Integer(2)); tempNumBytesMap.put("latin5", new Integer(1)); tempNumBytesMap.put("armscii8", new Integer(1)); tempNumBytesMap.put("utf8", new Integer(3)); tempNumBytesMap.put("ucs2", new Integer(2)); tempNumBytesMap.put("cp866", new Integer(1)); tempNumBytesMap.put("keybcs2", new Integer(1)); tempNumBytesMap.put("macce", new Integer(1)); tempNumBytesMap.put("macroman", new Integer(1)); tempNumBytesMap.put("cp852" , new Integer(1)); tempNumBytesMap.put("latin7", new Integer(1)); tempNumBytesMap.put("cp1251", new Integer(1)); tempNumBytesMap.put("cp1256" , new Integer(1)); tempNumBytesMap.put("cp1257", new Integer(1)); tempNumBytesMap.put("binary", new Integer(1)); tempNumBytesMap.put("geostd8", new Integer(1)); tempNumBytesMap.put("cp932", new Integer(2)); tempNumBytesMap.put("eucjpms", new Integer(3)); STATIC_CHARSET_TO_NUM_BYTES_MAP = Collections.unmodifiableMap( tempNumBytesMap); CHARSET_CONFIG.setProperty("javaToMysqlMappings", // // Note: This used to be stored in Charsets.properties, // but turned out to be problematic when dealing with // Tomcat classloaders when the security manager was // enabled // // Java Encoding MySQL Name (and version, '*' // denotes preferred value) // "US-ASCII = usa7," + "US-ASCII = >4.1.0 ascii," + "Big5 = big5," + "GBK = gbk," + "SJIS = sjis," + "EUC_CN = gb2312," + "EUC_JP = ujis," + "EUC_JP_Solaris = >5.0.3 eucjpms," + "EUC_KR = euc_kr," + "EUC_KR = >4.1.0 euckr," + "ISO8859_1 = *latin1," + "ISO8859_1 = latin1_de," + "ISO8859_1 = german1," + "ISO8859_1 = danish," + "ISO8859_2 = latin2," + "ISO8859_2 = czech," + "ISO8859_2 = hungarian," + "ISO8859_2 = croat," + "ISO8859_7 = greek," + "ISO8859_7 = latin7," + "ISO8859_8 = hebrew," + "ISO8859_9 = latin5," + "ISO8859_13 = latvian," + "ISO8859_13 = latvian1," + "ISO8859_13 = estonia," + "Cp437 = *>4.1.0 cp850," + "Cp437 = dos," + "Cp850 = cp850," + "Cp852 = cp852," + "Cp866 = cp866," + "KOI8_R = koi8_ru," + "KOI8_R = >4.1.0 koi8r," + "TIS620 = tis620," + "Cp1250 = cp1250," + "Cp1250 = win1250," + "Cp1251 = *>4.1.0 cp1251," + "Cp1251 = win1251," + "Cp1251 = cp1251cias," + "Cp1251 = cp1251csas," + "Cp1256 = cp1256," + "Cp1251 = win1251ukr," + "Cp1252 = latin1,"
+ "Cp1257 = cp1257," + "MacRoman = macroman," + "MacCentralEurope = macce," + "UTF-8 = utf8," + "UnicodeBig = ucs2," + "US-ASCII = binary," + "Cp943 = sjis," + "MS932 = sjis," + "MS932 = >4.1.11 cp932," + "WINDOWS-31J = sjis," + "WINDOWS-31J = >4.1.11 cp932," + "CP932 = sjis," + "CP932 = *>4.1.11 cp932," + "SHIFT_JIS = sjis," + "ASCII = ascii," + "LATIN5 = latin5," + "LATIN7 = latin7," + "HEBREW = hebrew," + "GREEK = greek," + "EUCKR = euckr," + "GB2312 = gb2312," + "LATIN2 = latin2"); HashMap javaToMysqlMap = new HashMap(); populateMapWithKeyValuePairs("javaToMysqlMappings", javaToMysqlMap, true, false); JAVA_TO_MYSQL_CHARSET_MAP = Collections.unmodifiableMap(javaToMysqlMap); HashMap mysqlToJavaMap = new HashMap(); Set keySet = JAVA_TO_MYSQL_CHARSET_MAP.keySet(); Iterator javaCharsets = keySet.iterator(); while (javaCharsets.hasNext()) { Object javaEncodingName = javaCharsets.next(); List mysqlEncodingList = (List) JAVA_TO_MYSQL_CHARSET_MAP .get(javaEncodingName); Iterator mysqlEncodings = mysqlEncodingList.iterator(); String mysqlEncodingName = null; while (mysqlEncodings.hasNext()) { VersionedStringProperty mysqlProp = (VersionedStringProperty) mysqlEncodings .next(); mysqlEncodingName = mysqlProp.toString(); mysqlToJavaMap.put(mysqlEncodingName, javaEncodingName); mysqlToJavaMap.put(mysqlEncodingName .toUpperCase(Locale.ENGLISH), javaEncodingName); } } // we don't want CP932 to map to CP932 mysqlToJavaMap.put("cp932", "Windows-31J"); mysqlToJavaMap.put("CP932", "Windows-31J"); MYSQL_TO_JAVA_CHARSET_MAP = Collections.unmodifiableMap(mysqlToJavaMap); TreeMap ucMap = new TreeMap(String.CASE_INSENSITIVE_ORDER); Iterator javaNamesKeys = JAVA_TO_MYSQL_CHARSET_MAP.keySet().iterator(); while (javaNamesKeys.hasNext()) { String key = (String) javaNamesKeys.next(); ucMap.put(key.toUpperCase(Locale.ENGLISH), JAVA_TO_MYSQL_CHARSET_MAP.get(key)); } JAVA_UC_TO_MYSQL_CHARSET_MAP = Collections.unmodifiableMap(ucMap); // // Character sets that we can't convert // ourselves. // HashMap tempMapMulti = new HashMap(); CHARSET_CONFIG.setProperty("multibyteCharsets", // // Note: This used to be stored in Charsets.properties, // but turned out to be problematic when dealing with // Tomcat classloaders when the security manager was // enabled // // Java Name MySQL Name (not currently used) // "Big5 = big5," + "GBK = gbk," + "SJIS = sjis," + "EUC_CN = gb2312," + "EUC_JP = ujis," + "EUC_JP_Solaris = eucjpms," + "EUC_KR = euc_kr," + "EUC_KR = >4.1.0 euckr," + "Cp943 = sjis," + "Cp943 = cp943," + "WINDOWS-31J = sjis," + "WINDOWS-31J = cp932," + "CP932 = cp932," + "MS932 = sjis," + "MS932 = cp932," + "SHIFT_JIS = sjis," + "EUCKR = euckr," + "GB2312 = gb2312," + "UTF-8 = utf8," + "utf8 = utf8," + "UnicodeBig = ucs2"); populateMapWithKeyValuePairs("multibyteCharsets", tempMapMulti, false, true); MULTIBYTE_CHARSETS = Collections.unmodifiableMap(tempMapMulti); INDEX_TO_CHARSET = new String[211]; try { INDEX_TO_CHARSET[1] = getJavaEncodingForMysqlEncoding("big5", null); INDEX_TO_CHARSET[2] = getJavaEncodingForMysqlEncoding("czech", null); INDEX_TO_CHARSET[3] = "ISO8859_1"; // punting for "dec8" INDEX_TO_CHARSET[4] = "ISO8859_1"; // punting for "dos" INDEX_TO_CHARSET[5] = getJavaEncodingForMysqlEncoding("german1", null); INDEX_TO_CHARSET[6] = "ISO8859_1"; // punting for "hp8" INDEX_TO_CHARSET[7] = getJavaEncodingForMysqlEncoding("koi8_ru", null); INDEX_TO_CHARSET[8] = getJavaEncodingForMysqlEncoding("latin1", null); INDEX_TO_CHARSET[9] = getJavaEncodingForMysqlEncoding("latin2", null); INDEX_TO_CHARSET[10] = "ISO8859_1"; // punting for "swe7" INDEX_TO_CHARSET[11] = getJavaEncodingForMysqlEncoding("usa7", null); INDEX_TO_CHARSET[12] = getJavaEncodingForMysqlEncoding("ujis", null); INDEX_TO_CHARSET[13] = getJavaEncodingForMysqlEncoding("sjis", null); INDEX_TO_CHARSET[14] = getJavaEncodingForMysqlEncoding("cp1251", null); INDEX_TO_CHARSET[15] = getJavaEncodingForMysqlEncoding("danish", null); INDEX_TO_CHARSET[16] = getJavaEncodingForMysqlEncoding("hebrew", null); INDEX_TO_CHARSET[17] = NOT_USED; // not used in the server INDEX_TO_CHARSET[18] = getJavaEncodingForMysqlEncoding("tis620", null); INDEX_TO_CHARSET[19] = getJavaEncodingForMysqlEncoding("euc_kr", null); INDEX_TO_CHARSET[20] = getJavaEncodingForMysqlEncoding("estonia", null); INDEX_TO_CHARSET[21] = getJavaEncodingForMysqlEncoding("hungarian", null); INDEX_TO_CHARSET[22] = "KOI8_R"; //punting for "koi8_ukr" INDEX_TO_CHARSET[23] = getJavaEncodingForMysqlEncoding( "win1251ukr", null); INDEX_TO_CHARSET[24] = getJavaEncodingForMysqlEncoding("gb2312", null); INDEX_TO_CHARSET[25] = getJavaEncodingForMysqlEncoding("greek", null); INDEX_TO_CHARSET[26] = getJavaEncodingForMysqlEncoding("win1250",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -