1
1
package info .xiaomo .core .untils ;
2
2
3
3
import java .io .UnsupportedEncodingException ;
4
+ import java .nio .charset .StandardCharsets ;
4
5
import java .util .Objects ;
5
6
6
7
/**
@@ -17,7 +18,7 @@ public class CharUtil {
17
18
public static String iso2gb (String text ) {
18
19
String result ;
19
20
try {
20
- result = new String (text .getBytes ("ISO-8859-1" ), "GB2312" );
21
+ result = new String (text .getBytes (StandardCharsets . ISO_8859_1 ), "GB2312" );
21
22
} catch (UnsupportedEncodingException ex ) {
22
23
result = ex .toString ();
23
24
}
@@ -30,7 +31,7 @@ public static String iso2gb(String text) {
30
31
public static String gb2iso (String text ) {
31
32
String result = "" ;
32
33
try {
33
- result = new String (text .getBytes ("GB2312" ), "ISO-8859-1" );
34
+ result = new String (text .getBytes ("GB2312" ), StandardCharsets . ISO_8859_1 );
34
35
} catch (UnsupportedEncodingException ex ) {
35
36
ex .printStackTrace ();
36
37
}
@@ -51,7 +52,7 @@ public static String utf8urlencode(String text) {
51
52
52
53
byte [] b = new byte [0 ];
53
54
try {
54
- b = Character .toString (c ).getBytes ("UTF-8" );
55
+ b = Character .toString (c ).getBytes (StandardCharsets . UTF_8 );
55
56
} catch (Exception ignored ) {
56
57
}
57
58
@@ -72,7 +73,7 @@ public static String utf8urlencode(String text) {
72
73
* Utf8URL解码
73
74
*/
74
75
public static String utf8urldecode (String text ) {
75
- String result = "" ;
76
+ StringBuilder result = new StringBuilder () ;
76
77
int p ;
77
78
if (text != null && text .length () > 0 ) {
78
79
text = text .toLowerCase ();
@@ -81,13 +82,13 @@ public static String utf8urldecode(String text) {
81
82
return text ;
82
83
}
83
84
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 );
86
87
if (Objects .equals (text , "" ) || text .length () < 9 ) {
87
- return result ;
88
+ return result . toString () ;
88
89
}
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 );
91
92
p = text .indexOf ("%e" );
92
93
}
93
94
}
@@ -104,11 +105,7 @@ private static String codetoword(String text) {
104
105
code [0 ] = (byte ) (Integer .parseInt (text .substring (1 , 3 ), 16 ) - 256 );
105
106
code [1 ] = (byte ) (Integer .parseInt (text .substring (4 , 6 ), 16 ) - 256 );
106
107
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 );
112
109
} else {
113
110
result = text ;
114
111
}
0 commit comments