Skip to content

Commit 5b5b6aa

Browse files
author
Stephane Landelle
committed
Merge pull request AsyncHttpClient#286 from normanmaurer/master
Only optimize byte array access if its safe
2 parents 9af2694 + c087476 commit 5b5b6aa

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

providers/netty/src/main/java/com/ning/http/client/providers/netty/ResponseBodyPart.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,19 @@ public byte[] getBodyPartBytes() {
6767
}
6868

6969
ChannelBuffer b = getChannelBuffer();
70-
byte[] rb = b.toByteBuffer().array();
71-
bytes.set(rb);
72-
return rb;
70+
int readable = b.readableBytes();
71+
int readerIndex = b.readerIndex();
72+
if (b.hasArray()) {
73+
byte[] array = b.array();
74+
if (b.arrayOffset() == 0 && readerIndex == 0 && array.length == readable) {
75+
bytes.set(array);
76+
return array;
77+
}
78+
}
79+
byte[] array = new byte[readable];
80+
b.getBytes(readerIndex, array);
81+
bytes.set(array);
82+
return array;
7383
}
7484

7585
@Override

0 commit comments

Comments
 (0)