Skip to content

Commit fd907c0

Browse files
committed
Minor ByteBufUtils clean up
1 parent 65e5f38 commit fd907c0

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

netty-utils/src/main/java/org/asynchttpclient/netty/util/ByteBufUtils.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

netty-utils/src/main/java/org/asynchttpclient/netty/util/Utf8ByteBufCharsetDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public String decode(ByteBuf... bufs) throws CharacterCodingException {
193193
}
194194

195195
if (withoutArray) {
196-
return ByteBufUtils.decodeNonOptimized(UTF_8, bufs);
196+
return ByteBufUtils.byteBuf2StringDefault(UTF_8, bufs);
197197

198198
} else {
199199
ByteBuffer[] nioBuffers = new ByteBuffer[totalNioBuffers];

0 commit comments

Comments
 (0)