Skip to content

Commit 75a23e9

Browse files
author
Stephane Landelle
committed
Port Netty buffers usage
1 parent 232aab0 commit 75a23e9

File tree

6 files changed

+98
-47
lines changed

6 files changed

+98
-47
lines changed

api/src/main/java/com/ning/http/client/Response.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.InputStream;
2121
import java.net.MalformedURLException;
2222
import java.net.URI;
23+
import java.nio.ByteBuffer;
2324
import java.util.ArrayList;
2425
import java.util.Collections;
2526
import java.util.List;
@@ -33,22 +34,30 @@ public interface Response {
3334
*
3435
* @return The status code
3536
*/
36-
public int getStatusCode();
37+
int getStatusCode();
3738

3839
/**
3940
* Returns the status text for the request.
4041
*
4142
* @return The status text
4243
*/
43-
public String getStatusText();
44+
String getStatusText();
4445

4546
/**
4647
* Return the entire response body as a byte[].
4748
*
4849
* @return the entire response body as a byte[].
4950
* @throws IOException
5051
*/
51-
public byte[] getResponseBodyAsBytes() throws IOException;
52+
byte[] getResponseBodyAsBytes() throws IOException;
53+
54+
/**
55+
* Return the entire response body as a ByteBuffer.
56+
*
57+
* @return the entire response body as a ByteBuffer.
58+
* @throws IOException
59+
*/
60+
ByteBuffer getResponseBodyAsByteBuffer() throws IOException;
5261

5362
/**
5463
* Returns an input stream for the response body. Note that you should not try to get this more than once,
@@ -57,7 +66,7 @@ public interface Response {
5766
* @return The input stream
5867
* @throws java.io.IOException
5968
*/
60-
public InputStream getResponseBodyAsStream() throws IOException;
69+
InputStream getResponseBodyAsStream() throws IOException;
6170

6271
/**
6372
* Returns the first maxLength bytes of the response body as a string. Note that this does not check
@@ -69,7 +78,7 @@ public interface Response {
6978
* @return The response body
7079
* @throws java.io.IOException
7180
*/
72-
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException;
81+
String getResponseBodyExcerpt(int maxLength, String charset) throws IOException;
7382

7483
/**
7584
* Return the entire response body as a String.
@@ -78,7 +87,7 @@ public interface Response {
7887
* @return the entire response body as a String.
7988
* @throws IOException
8089
*/
81-
public String getResponseBody(String charset) throws IOException;
90+
String getResponseBody(String charset) throws IOException;
8291

8392
/**
8493
* Returns the first maxLength bytes of the response body as a string. Note that this does not check
@@ -89,15 +98,15 @@ public interface Response {
8998
* @return The response body
9099
* @throws java.io.IOException
91100
*/
92-
public String getResponseBodyExcerpt(int maxLength) throws IOException;
101+
String getResponseBodyExcerpt(int maxLength) throws IOException;
93102

94103
/**
95104
* Return the entire response body as a String.
96105
*
97106
* @return the entire response body as a String.
98107
* @throws IOException
99108
*/
100-
public String getResponseBody() throws IOException;
109+
String getResponseBody() throws IOException;
101110

102111
/**
103112
* Return the request {@link URI}. Note that if the request got redirected, the value of the {@link URI} will be
@@ -106,30 +115,30 @@ public interface Response {
106115
* @return the request {@link URI}.
107116
* @throws MalformedURLException
108117
*/
109-
public URI getUri() throws MalformedURLException;
118+
URI getUri() throws MalformedURLException;
110119

111120
/**
112121
* Return the content-type header value.
113122
*
114123
* @return the content-type header value.
115124
*/
116-
public String getContentType();
125+
String getContentType();
117126

118127
/**
119128
* Return the response header
120129
*
121130
* @return the response header
122131
*/
123-
public String getHeader(String name);
132+
String getHeader(String name);
124133

125134
/**
126135
* Return a {@link List} of the response header value.
127136
*
128137
* @return the response header
129138
*/
130-
public List<String> getHeaders(String name);
139+
List<String> getHeaders(String name);
131140

132-
public FluentCaseInsensitiveStringsMap getHeaders();
141+
FluentCaseInsensitiveStringsMap getHeaders();
133142

134143
/**
135144
* Return true if the response redirects to another object.
@@ -143,19 +152,19 @@ public interface Response {
143152
*
144153
* @return The textual representation
145154
*/
146-
public String toString();
155+
String toString();
147156

148157
/**
149158
* Return the list of {@link Cookie}.
150159
*/
151-
public List<Cookie> getCookies();
160+
List<Cookie> getCookies();
152161

153162
/**
154163
* Return true if the response's status has been computed by an {@link AsyncHandler}
155164
*
156165
* @return true if the response's status has been computed by an {@link AsyncHandler}
157166
*/
158-
public boolean hasResponseStatus();
167+
boolean hasResponseStatus();
159168

160169
/**
161170
* Return true if the response's headers has been computed by an {@link AsyncHandler} It will return false if the
@@ -164,7 +173,7 @@ public interface Response {
164173
*
165174
* @return true if the response's headers has been computed by an {@link AsyncHandler}
166175
*/
167-
public boolean hasResponseHeaders();
176+
boolean hasResponseHeaders();
168177

169178
/**
170179
* Return true if the response's body has been computed by an {@link AsyncHandler}. It will return false if the
@@ -173,8 +182,7 @@ public interface Response {
173182
*
174183
* @return true if the response's body has been computed by an {@link AsyncHandler}
175184
*/
176-
public boolean hasResponseBody();
177-
185+
boolean hasResponseBody();
178186

179187
public static class ResponseBuilder {
180188
private final List<HttpResponseBodyPart> bodies =

api/src/main/java/com/ning/http/client/providers/ResponseBase.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.net.URI;
6+
import java.nio.ByteBuffer;
67
import java.util.Collections;
78
import java.util.List;
89

@@ -42,7 +43,6 @@ public final String getStatusText() {
4243
return status.getStatusText();
4344
}
4445

45-
4646
/* @Override */
4747
public final URI getUri() /*throws MalformedURLException*/ {
4848
return status.getUrl();
@@ -78,6 +78,10 @@ public byte[] getResponseBodyAsBytes() throws IOException {
7878
return AsyncHttpProviderUtils.contentToBytes(bodyParts);
7979
}
8080

81+
public ByteBuffer getResponseBodyAsByteBuffer() throws IOException {
82+
return ByteBuffer.wrap(getResponseBodyAsBytes());
83+
}
84+
8185
/* @Override */
8286
public String getResponseBody() throws IOException {
8387
return getResponseBody(DEFAULT_CHARSET);

api/src/main/java/com/ning/http/client/webdav/WebDavResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.net.MalformedURLException;
2323
import java.net.URI;
24+
import java.nio.ByteBuffer;
2425
import java.util.List;
2526

2627
/**
@@ -49,6 +50,10 @@ public byte[] getResponseBodyAsBytes() throws IOException {
4950
return response.getResponseBodyAsBytes();
5051
}
5152

53+
public ByteBuffer getResponseBodyAsByteBuffer() throws IOException {
54+
return response.getResponseBodyAsByteBuffer();
55+
}
56+
5257
public InputStream getResponseBodyAsStream() throws IOException {
5358
return response.getResponseBodyAsStream();
5459
}

providers/netty/pom.xml

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,7 @@
2020
<dependency>
2121
<groupId>io.netty</groupId>
2222
<artifactId>netty</artifactId>
23-
<version>3.4.4.Final</version>
24-
<exclusions>
25-
<exclusion>
26-
<groupId>javax.servlet</groupId>
27-
<artifactId>servlet-api</artifactId>
28-
</exclusion>
29-
<exclusion>
30-
<groupId>commons-logging</groupId>
31-
<artifactId>commons-logging</artifactId>
32-
</exclusion>
33-
<exclusion>
34-
<groupId>org.slf4j</groupId>
35-
<artifactId>slf4j-api</artifactId>
36-
</exclusion>
37-
<exclusion>
38-
<groupId>log4j</groupId>
39-
<artifactId>log4j</artifactId>
40-
</exclusion>
41-
</exclusions>
23+
<version>3.6.3.Final</version>
4224
</dependency>
4325
</dependencies>
4426

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@
2323
import com.ning.http.util.AsyncHttpProviderUtils;
2424

2525
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.nio.ByteBuffer;
28+
import java.nio.charset.Charset;
2629
import java.util.ArrayList;
2730
import java.util.Collections;
2831
import java.util.List;
2932
import java.util.Map;
3033

34+
import org.jboss.netty.buffer.ChannelBuffer;
35+
import org.jboss.netty.buffer.ChannelBufferInputStream;
36+
import org.jboss.netty.buffer.ChannelBuffers;
37+
3138
/**
3239
* Wrapper around the {@link com.ning.http.client.Response} API.
3340
*/
@@ -65,4 +72,49 @@ protected List<Cookie> buildCookies() {
6572
}
6673
return Collections.unmodifiableList(cookies);
6774
}
75+
76+
/* @Override */
77+
public byte[] getResponseBodyAsBytes() throws IOException {
78+
return getResponseBodyAsByteBuffer().array();
79+
}
80+
81+
/* @Override */
82+
public ByteBuffer getResponseBodyAsByteBuffer() throws IOException {
83+
return getResponseBodyAsChannelBuffer().toByteBuffer();
84+
}
85+
86+
/* @Override */
87+
public String getResponseBody() throws IOException {
88+
return getResponseBody(null);
89+
}
90+
91+
/* @Override */
92+
public String getResponseBody(String charset) throws IOException {
93+
return getResponseBodyAsChannelBuffer().toString(Charset.forName(calculateCharset(charset)));
94+
}
95+
96+
/* @Override */
97+
public InputStream getResponseBodyAsStream() throws IOException {
98+
return new ChannelBufferInputStream(getResponseBodyAsChannelBuffer());
99+
}
100+
101+
public ChannelBuffer getResponseBodyAsChannelBuffer() throws IOException {
102+
ChannelBuffer b = null;
103+
switch (bodyParts.size()) {
104+
case 0:
105+
b = ChannelBuffers.EMPTY_BUFFER;
106+
break;
107+
case 1:
108+
b = ResponseBodyPart.class.cast(bodyParts.get(0)).getChannelBuffer();
109+
break;
110+
default:
111+
ChannelBuffer[] channelBuffers = new ChannelBuffer[bodyParts.size()];
112+
for (int i = 0; i < bodyParts.size(); i++) {
113+
channelBuffers[i] = ResponseBodyPart.class.cast(bodyParts.get(i)).getChannelBuffer();
114+
}
115+
b = ChannelBuffers.wrappedBuffer(channelBuffers);
116+
}
117+
118+
return b;
119+
}
68120
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
* A callback class used when an HTTP response body is received.
3434
*/
3535
public class ResponseBodyPart extends HttpResponseBodyPart {
36-
// Empty arrays are immutable, can freely reuse
37-
private final static byte[] NO_BYTES = new byte[0];
3836

3937
private final HttpChunk chunk;
4038
private final HttpResponse response;
@@ -68,11 +66,9 @@ public byte[] getBodyPartBytes() {
6866
return bp;
6967
}
7068

71-
ChannelBuffer b = (chunk != null) ? chunk.getContent() : response.getContent();
72-
int available = b.readableBytes();
73-
74-
final byte[] rb = (available == 0) ? NO_BYTES : new byte[available];
75-
b.getBytes(b.readerIndex(), rb, 0, available);
69+
ChannelBuffer b = getChannelBuffer();
70+
byte[] rb = b.toByteBuffer().array();
71+
bytes.set(rb);
7672
return rb;
7773
}
7874

@@ -89,7 +85,7 @@ public int length() {
8985

9086
@Override
9187
public int writeTo(OutputStream outputStream) throws IOException {
92-
ChannelBuffer b = (chunk != null) ? chunk.getContent() : response.getContent();
88+
ChannelBuffer b = getChannelBuffer();
9389
int available = b.readableBytes();
9490
if (available > 0) {
9591
b.getBytes(b.readerIndex(), outputStream, available);
@@ -102,6 +98,10 @@ public ByteBuffer getBodyByteBuffer() {
10298
return ByteBuffer.wrap(getBodyPartBytes());
10399
}
104100

101+
public ChannelBuffer getChannelBuffer() {
102+
return chunk != null ? chunk.getContent() : response.getContent();
103+
}
104+
105105
/**
106106
* {@inheritDoc}
107107
*/

0 commit comments

Comments
 (0)