Skip to content

Commit e9364a6

Browse files
committed
ByteBufUtils shouldn't release input ByteBufs, close AsyncHttpClient#1302
1 parent 2ded526 commit e9364a6

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,28 @@ public static String byteBuf2String(Charset charset, ByteBuf buf) throws Charact
3434
}
3535
}
3636

37+
public static String decodeNonOptimized(Charset charset, ByteBuf... bufs) {
38+
39+
CompositeByteBuf composite = Unpooled.compositeBuffer(bufs.length);
40+
41+
try {
42+
for (ByteBuf buf : bufs) {
43+
buf.retain();
44+
composite.addComponent(buf);
45+
}
46+
47+
return composite.toString(charset);
48+
49+
} finally {
50+
composite.release();
51+
}
52+
}
53+
3754
public static String byteBuf2String(Charset charset, ByteBuf... bufs) throws CharacterCodingException {
3855
if (charset.equals(UTF_8) || charset.equals(US_ASCII)) {
3956
return Utf8ByteBufCharsetDecoder.decodeUtf8(bufs);
4057
} else {
41-
CompositeByteBuf composite = Unpooled.compositeBuffer(bufs.length);
42-
43-
try {
44-
for (ByteBuf buf : bufs) {
45-
buf.retain();
46-
composite.addComponent(buf);
47-
}
48-
49-
return composite.toString(charset);
50-
51-
} finally {
52-
composite.release();
53-
}
58+
return decodeNonOptimized(charset, bufs);
5459
}
5560
}
5661

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import static java.nio.charset.StandardCharsets.UTF_8;
1717
import io.netty.buffer.ByteBuf;
18-
import io.netty.buffer.Unpooled;
1918

2019
import java.nio.ByteBuffer;
2120
import java.nio.CharBuffer;
@@ -194,12 +193,7 @@ public String decode(ByteBuf... bufs) throws CharacterCodingException {
194193
}
195194

196195
if (direct) {
197-
ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bufs);
198-
try {
199-
return wrappedBuffer.toString(UTF_8);
200-
} finally {
201-
wrappedBuffer.release();
202-
}
196+
return ByteBufUtils.decodeNonOptimized(UTF_8, bufs);
203197

204198
} else {
205199
ByteBuffer[] nioBuffers = new ByteBuffer[totalNioBuffers];

0 commit comments

Comments
 (0)