11package info .xiaomo .core .untils ;
22
33import java .io .UnsupportedEncodingException ;
4+ import java .nio .charset .StandardCharsets ;
45import java .util .Objects ;
56
67/**
@@ -17,7 +18,7 @@ public class CharUtil {
1718 public static String iso2gb (String text ) {
1819 String result ;
1920 try {
20- result = new String (text .getBytes ("ISO-8859-1" ), "GB2312" );
21+ result = new String (text .getBytes (StandardCharsets . ISO_8859_1 ), "GB2312" );
2122 } catch (UnsupportedEncodingException ex ) {
2223 result = ex .toString ();
2324 }
@@ -30,7 +31,7 @@ public static String iso2gb(String text) {
3031 public static String gb2iso (String text ) {
3132 String result = "" ;
3233 try {
33- result = new String (text .getBytes ("GB2312" ), "ISO-8859-1" );
34+ result = new String (text .getBytes ("GB2312" ), StandardCharsets . ISO_8859_1 );
3435 } catch (UnsupportedEncodingException ex ) {
3536 ex .printStackTrace ();
3637 }
@@ -51,7 +52,7 @@ public static String utf8urlencode(String text) {
5152
5253 byte [] b = new byte [0 ];
5354 try {
54- b = Character .toString (c ).getBytes ("UTF-8" );
55+ b = Character .toString (c ).getBytes (StandardCharsets . UTF_8 );
5556 } catch (Exception ignored ) {
5657 }
5758
@@ -72,7 +73,7 @@ public static String utf8urlencode(String text) {
7273 * Utf8URL解码
7374 */
7475 public static String utf8urldecode (String text ) {
75- String result = "" ;
76+ StringBuilder result = new StringBuilder () ;
7677 int p ;
7778 if (text != null && text .length () > 0 ) {
7879 text = text .toLowerCase ();
@@ -81,13 +82,13 @@ public static String utf8urldecode(String text) {
8182 return text ;
8283 }
8384 while (p != -1 ) {
84- result += text . substring ( 0 , p );
85- text = text .substring (p , text . length () );
85+ result . append ( text , 0 , p );
86+ text = text .substring (p );
8687 if (Objects .equals (text , "" ) || text .length () < 9 ) {
87- return result ;
88+ return result . toString () ;
8889 }
89- result += codetoword (text .substring (0 , 9 ));
90- text = text .substring (9 , text . length () );
90+ result . append ( codetoword (text .substring (0 , 9 ) ));
91+ text = text .substring (9 );
9192 p = text .indexOf ("%e" );
9293 }
9394 }
@@ -104,11 +105,7 @@ private static String codetoword(String text) {
104105 code [0 ] = (byte ) (Integer .parseInt (text .substring (1 , 3 ), 16 ) - 256 );
105106 code [1 ] = (byte ) (Integer .parseInt (text .substring (4 , 6 ), 16 ) - 256 );
106107 code [2 ] = (byte ) (Integer .parseInt (text .substring (7 , 9 ), 16 ) - 256 );
107- try {
108- result = new String (code , "UTF-8" );
109- } catch (UnsupportedEncodingException ex ) {
110- result = null ;
111- }
108+ result = new String (code , StandardCharsets .UTF_8 );
112109 } else {
113110 result = text ;
114111 }
0 commit comments