Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit a2e7a14

Browse files
author
Stephane Landelle
committed
Fix and optimize Netty ResponseBodyParts bytes[], master close AsyncHttpClient#287
1 parent 330d91b commit a2e7a14

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
*/
1616
package com.ning.http.client.providers.netty;
1717

18-
import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
19-
import com.ning.http.client.Cookie;
20-
import com.ning.http.client.HttpResponseBodyPart;
21-
import com.ning.http.client.HttpResponseHeaders;
22-
import com.ning.http.client.HttpResponseStatus;
23-
import com.ning.http.client.providers.ResponseBase;
24-
import com.ning.http.util.AsyncHttpProviderUtils;
25-
2618
import java.io.IOException;
2719
import java.io.InputStream;
2820
import java.nio.ByteBuffer;
@@ -36,6 +28,15 @@
3628
import org.jboss.netty.buffer.ChannelBufferInputStream;
3729
import org.jboss.netty.buffer.ChannelBuffers;
3830

31+
import com.ning.http.client.Cookie;
32+
import com.ning.http.client.HttpResponseBodyPart;
33+
import com.ning.http.client.HttpResponseHeaders;
34+
import com.ning.http.client.HttpResponseStatus;
35+
import com.ning.http.client.providers.ResponseBase;
36+
import com.ning.http.client.providers.netty.util.ChannelBufferUtil;
37+
import com.ning.http.util.AsyncHttpProviderUtils;
38+
import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
39+
3940
/**
4041
* Wrapper around the {@link com.ning.http.client.Response} API.
4142
*/
@@ -75,7 +76,7 @@ protected List<Cookie> buildCookies() {
7576

7677
/* @Override */
7778
public byte[] getResponseBodyAsBytes() throws IOException {
78-
return getResponseBodyAsByteBuffer().array();
79+
return ChannelBufferUtil.channelBuffer2bytes(getResponseBodyAsChannelBuffer());
7980
}
8081

8182
/* @Override */

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
*/
1616
package com.ning.http.client.providers.netty;
1717

18-
import com.ning.http.client.AsyncHttpProvider;
19-
import com.ning.http.client.HttpResponseBodyPart;
20-
import org.jboss.netty.buffer.ChannelBuffer;
21-
import org.jboss.netty.handler.codec.http.HttpChunk;
22-
import org.jboss.netty.handler.codec.http.HttpResponse;
23-
2418
import java.io.ByteArrayInputStream;
2519
import java.io.IOException;
2620
import java.io.InputStream;
@@ -29,6 +23,14 @@
2923
import java.nio.ByteBuffer;
3024
import java.util.concurrent.atomic.AtomicReference;
3125

26+
import org.jboss.netty.buffer.ChannelBuffer;
27+
import org.jboss.netty.handler.codec.http.HttpChunk;
28+
import org.jboss.netty.handler.codec.http.HttpResponse;
29+
30+
import com.ning.http.client.AsyncHttpProvider;
31+
import com.ning.http.client.HttpResponseBodyPart;
32+
import com.ning.http.client.providers.netty.util.ChannelBufferUtil;
33+
3234
/**
3335
* A callback class used when an HTTP response body is received.
3436
*/
@@ -66,20 +68,9 @@ public byte[] getBodyPartBytes() {
6668
return bp;
6769
}
6870

69-
ChannelBuffer b = getChannelBuffer();
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;
71+
byte[] rb = ChannelBufferUtil.channelBuffer2bytes(getChannelBuffer());
72+
bytes.set(rb);
73+
return rb;
8374
}
8475

8576
@Override
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.util;
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+
}

0 commit comments

Comments
 (0)