Skip to content

Commit 38704f1

Browse files
author
Stephane Landelle
committed
Fix and optimize Netty ResponseBodyParts bytes[], close AsyncHttpClient#287
1 parent c640c57 commit 38704f1

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2010 Ning, Inc.
3+
*
4+
* Ning licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.ning.http.client.providers.netty;
17+
18+
import org.jboss.netty.buffer.ChannelBuffer;
19+
20+
public class ChannelBufferUtil {
21+
22+
public static byte[] channelBuffer2bytes(ChannelBuffer b) {
23+
int readable = b.readableBytes();
24+
int readerIndex = b.readerIndex();
25+
if (b.hasArray()) {
26+
byte[] array = b.array();
27+
if (b.arrayOffset() == 0 && readerIndex == 0 && array.length == readable) {
28+
return array;
29+
}
30+
}
31+
byte[] array = new byte[readable];
32+
b.getBytes(readerIndex, array);
33+
return array;
34+
}
35+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public String getStatusText() {
7676

7777
/* @Override */
7878
public byte[] getResponseBodyAsBytes() throws IOException {
79-
return getResponseBodyAsByteBuffer().array();
79+
return ChannelBufferUtil.channelBuffer2bytes(getResponseBodyAsChannelBuffer());
8080
}
8181

8282
public ByteBuffer getResponseBodyAsByteBuffer() throws IOException {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ResponseBodyPart extends HttpResponseBodyPart {
3434

3535
private final HttpChunk chunk;
3636
private final HttpResponse response;
37-
private final AtomicReference<byte[]> bytes = new AtomicReference(null);
37+
private final AtomicReference<byte[]> bytes = new AtomicReference<byte[]>(null);
3838
private final boolean isLast;
3939
private boolean closeConnection = false;
4040

@@ -63,8 +63,7 @@ public byte[] getBodyPartBytes() {
6363
return bytes.get();
6464
}
6565

66-
ChannelBuffer b = getChannelBuffer();
67-
byte[] rb = b.toByteBuffer().array();
66+
byte[] rb = ChannelBufferUtil.channelBuffer2bytes(getChannelBuffer());
6867
bytes.set(rb);
6968
return rb;
7069
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package com.ning.http.client.providers.netty;
1414

1515
import com.ning.http.util.Base64;
16-
import org.jboss.netty.util.CharsetUtil;
1716

1817
import java.io.UnsupportedEncodingException;
1918
import java.security.MessageDigest;

0 commit comments

Comments
 (0)