@@ -109,7 +109,8 @@ public class CharsetMapping {
109109 new MysqlCharset (MYSQL_CHARSET_NAME_big5 , 2 , 0 , new String [] { "Big5" }),
110110 new MysqlCharset (MYSQL_CHARSET_NAME_gbk , 2 , 0 , new String [] { "GBK" }),
111111
112- new MysqlCharset (MYSQL_CHARSET_NAME_sjis , 2 , 0 , new String [] { "SHIFT_JIS" , "Cp943" , "WINDOWS-31J" }), // SJIS is alias for SHIFT_JIS, Cp943 is rather a cp932 but we map it to sjis for years
112+ // SJIS is alias for SHIFT_JIS, Cp943 is rather a cp932 but we map it to sjis for years
113+ new MysqlCharset (MYSQL_CHARSET_NAME_sjis , 2 , 0 , new String [] { "SHIFT_JIS" , "Cp943" , "WINDOWS-31J" }),
113114 new MysqlCharset (MYSQL_CHARSET_NAME_cp932 , 2 , 1 , new String [] { "WINDOWS-31J" }),
114115
115116 new MysqlCharset (MYSQL_CHARSET_NAME_gb2312 , 2 , 0 , new String [] { "GB2312" }),
@@ -135,7 +136,9 @@ public class CharsetMapping {
135136 new MysqlCharset (MYSQL_CHARSET_NAME_cp850 , 1 , 0 , new String [] { "Cp850" , "Cp437" }),
136137
137138 new MysqlCharset (MYSQL_CHARSET_NAME_cp852 , 1 , 0 , new String [] { "Cp852" }),
138- new MysqlCharset (MYSQL_CHARSET_NAME_keybcs2 , 1 , 0 , new String [] { "Cp852" }), // Kamenicky encoding usually known as Cp895 but there is no official cp895 specification; close to Cp852, see http://ftp.muni.cz/pub/localization/charsets/cs-encodings-faq
139+ // Kamenicky encoding usually known as Cp895 but there is no official cp895 specification; close to Cp852, see
140+ // http://ftp.muni.cz/pub/localization/charsets/cs-encodings-faq
141+ new MysqlCharset (MYSQL_CHARSET_NAME_keybcs2 , 1 , 0 , new String [] { "Cp852" }),
139142
140143 new MysqlCharset (MYSQL_CHARSET_NAME_cp866 , 1 , 0 , new String [] { "Cp866" }),
141144
@@ -541,13 +544,10 @@ public class CharsetMapping {
541544 protected static String getStaticMysqlCharsetForJavaEncoding (String javaEncoding , ServerVersion version ) {
542545 List <MysqlCharset > mysqlCharsets = CharsetMapping .JAVA_ENCODING_UC_TO_MYSQL_CHARSET .get (javaEncoding .toUpperCase (Locale .ENGLISH ));
543546 if (mysqlCharsets != null ) {
544- if (version == null ) {
545- return mysqlCharsets .get (0 ).charsetName ; // Take the first one we get
546- }
547547 MysqlCharset currentChoice = null ;
548548 for (MysqlCharset charset : mysqlCharsets ) {
549- if (charset .isOkayForVersion (version ) && (currentChoice == null || currentChoice .minimumVersion . compareTo ( charset . minimumVersion ) < 0
550- || currentChoice .priority < charset .priority && currentChoice .minimumVersion .compareTo (charset .minimumVersion ) == 0 )) {
549+ if (( version == null || charset .isOkayForVersion (version )) && (currentChoice == null || currentChoice .priority < charset . priority
550+ || currentChoice .priority == charset .priority && currentChoice .minimumVersion .compareTo (charset .minimumVersion ) < 0 )) {
551551 currentChoice = charset ;
552552 }
553553 }
@@ -681,7 +681,6 @@ class MysqlCharset {
681681 public final int priority ;
682682 public final List <String > javaEncodingsUc = new ArrayList <>();
683683 public final List <String > aliases = new ArrayList <>();
684-
685684 public final ServerVersion minimumVersion ;
686685
687686 /**
@@ -720,7 +719,6 @@ private void addEncodingMapping(String encoding) {
720719 public MysqlCharset (String charsetName , int mblen , int priority , String [] javaEncodings , ServerVersion minimumVersion ) {
721720 this .charsetName = charsetName ;
722721 this .mblen = mblen ;
723- this .priority = priority ;
724722
725723 for (int i = 0 ; i < javaEncodings .length ; i ++) {
726724 String encoding = javaEncodings [i ];
@@ -729,15 +727,19 @@ public MysqlCharset(String charsetName, int mblen, int priority, String[] javaEn
729727 addEncodingMapping (cs .name ());
730728 cs .aliases ().forEach (this ::addEncodingMapping );
731729 } catch (Exception e ) {
732- // if there is no support of this charset in JVM it's still possible to use our converter for 1-byte charsets
730+ // If there is no support of this charset in JVM it's still possible to use our converter for 1-byte charsets.
733731 if (mblen == 1 ) {
734732 addEncodingMapping (encoding );
735733 }
736734 }
737735 }
738736
739737 if (this .javaEncodingsUc .size () == 0 ) {
738+ // Drop priority to minimum and map to default charset.
739+ this .priority = -1 ;
740740 addEncodingMapping (mblen > 1 ? "UTF-8" : "Cp1252" );
741+ } else {
742+ this .priority = priority ;
741743 }
742744
743745 this .minimumVersion = minimumVersion ;
@@ -747,12 +749,12 @@ public MysqlCharset(String charsetName, int mblen, int priority, String[] javaEn
747749 public String toString () {
748750 StringBuilder asString = new StringBuilder ();
749751 asString .append ("[" );
750- asString .append ("charsetName=" );
751- asString .append (this .charsetName );
752- asString .append (",mblen=" );
753- asString .append (this .mblen );
754- // asString.append(",javaEncoding=" );
755- // asString.append(this.javaEncodings.toString() );
752+ asString .append ("charsetName=" ). append ( this . charsetName ) ;
753+ asString .append (",mblen=" ). append ( this .mblen );
754+ asString .append (",pirority=" ). append ( this . priority );
755+ asString .append (",javaEncoding=" ). append ( this .javaEncodingsUc );
756+ asString .append (",aliases=" ). append ( this . aliases );
757+ asString .append (",minimumVersion=" ). append ( this . minimumVersion );
756758 asString .append ("]" );
757759 return asString .toString ();
758760 }
@@ -800,14 +802,11 @@ public Collation(int index, String[] collationNames, int priority, String charse
800802 public String toString () {
801803 StringBuilder asString = new StringBuilder ();
802804 asString .append ("[" );
803- asString .append ("index=" );
804- asString .append (this .index );
805- asString .append (",collationNames=" );
806- asString .append (Arrays .toString (this .collationNames ));
807- asString .append (",charsetName=" );
808- asString .append (this .mysqlCharset .charsetName );
809- asString .append (",javaCharsetName=" );
810- asString .append (this .mysqlCharset .getMatchingJavaEncoding (null ));
805+ asString .append ("index=" ).append (this .index );
806+ asString .append (",collationNames=" ).append (Arrays .toString (this .collationNames ));
807+ asString .append (",priority=" ).append (this .priority );
808+ asString .append (",charsetName=" ).append (this .mysqlCharset .charsetName );
809+ asString .append (",javaCharsetName=" ).append (this .mysqlCharset .getMatchingJavaEncoding (null ));
811810 asString .append ("]" );
812811 return asString .toString ();
813812 }
0 commit comments