@@ -25,15 +25,15 @@ public final class ByteBufUtils {
2525 private ByteBufUtils () {
2626 }
2727
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 );
3430 }
3531
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+ }
3737
3838 for (ByteBuf buf : bufs ) {
3939 buf .retain ();
@@ -43,18 +43,17 @@ public static String decodeNonOptimized(Charset charset, ByteBuf... bufs) {
4343
4444 try {
4545 return composite .toString (charset );
46-
4746 } finally {
4847 composite .release ();
4948 }
5049 }
5150
51+ public static String byteBuf2String (Charset charset , ByteBuf buf ) throws CharacterCodingException {
52+ return isUtf8OrUsAscii (charset ) ? Utf8ByteBufCharsetDecoder .decodeUtf8 (buf ) : buf .toString (charset );
53+ }
54+
5255 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 );
5857 }
5958
6059 public static byte [] byteBuf2Bytes (ByteBuf buf ) {
0 commit comments