@@ -25,15 +25,15 @@ public final class ByteBufUtils {
25
25
private ByteBufUtils () {
26
26
}
27
27
28
- public static String byteBuf2String (Charset charset , ByteBuf buf ) throws CharacterCodingException {
29
- if (charset .equals (UTF_8 ) || charset .equals (US_ASCII )) {
30
- return Utf8ByteBufCharsetDecoder .decodeUtf8 (buf );
31
- } else {
32
- return buf .toString (charset );
33
- }
28
+ public static boolean isUtf8OrUsAscii (Charset charset ) {
29
+ return charset .equals (UTF_8 ) || charset .equals (US_ASCII );
34
30
}
35
31
36
- public static String decodeNonOptimized (Charset charset , ByteBuf ... bufs ) {
32
+ public static String byteBuf2StringDefault (Charset charset , ByteBuf ... bufs ) {
33
+
34
+ if (bufs .length == 1 ) {
35
+ return bufs [0 ].toString (charset );
36
+ }
37
37
38
38
for (ByteBuf buf : bufs ) {
39
39
buf .retain ();
@@ -43,18 +43,17 @@ public static String decodeNonOptimized(Charset charset, ByteBuf... bufs) {
43
43
44
44
try {
45
45
return composite .toString (charset );
46
-
47
46
} finally {
48
47
composite .release ();
49
48
}
50
49
}
51
50
51
+ public static String byteBuf2String (Charset charset , ByteBuf buf ) throws CharacterCodingException {
52
+ return isUtf8OrUsAscii (charset ) ? Utf8ByteBufCharsetDecoder .decodeUtf8 (buf ) : buf .toString (charset );
53
+ }
54
+
52
55
public static String byteBuf2String (Charset charset , ByteBuf ... bufs ) throws CharacterCodingException {
53
- if (charset .equals (UTF_8 ) || charset .equals (US_ASCII )) {
54
- return Utf8ByteBufCharsetDecoder .decodeUtf8 (bufs );
55
- } else {
56
- return decodeNonOptimized (charset , bufs );
57
- }
56
+ return isUtf8OrUsAscii (charset ) ? Utf8ByteBufCharsetDecoder .decodeUtf8 (bufs ) : byteBuf2StringDefault (charset , bufs );
58
57
}
59
58
60
59
public static byte [] byteBuf2Bytes (ByteBuf buf ) {
0 commit comments