Skip to content

Commit d95b3bf

Browse files
committed
Pass ByteBuffer bodies instead of byte[], close AsyncHttpClient#827
1 parent ee1a953 commit d95b3bf

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

src/main/java/com/ning/http/client/providers/netty/request/NettyRequestFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
import com.ning.http.client.providers.netty.request.body.NettyBody;
4848
import com.ning.http.client.providers.netty.request.body.NettyBodyBody;
4949
import com.ning.http.client.providers.netty.request.body.NettyByteArrayBody;
50+
import com.ning.http.client.providers.netty.request.body.NettyByteBufferBody;
5051
import com.ning.http.client.providers.netty.request.body.NettyCompositeByteArrayBody;
5152
import com.ning.http.client.providers.netty.request.body.NettyDirectBody;
5253
import com.ning.http.client.providers.netty.request.body.NettyFileBody;
5354
import com.ning.http.client.providers.netty.request.body.NettyInputStreamBody;
5455
import com.ning.http.client.providers.netty.request.body.NettyMultipartBody;
5556
import com.ning.http.client.providers.netty.spnego.SpnegoEngine;
5657
import com.ning.http.client.uri.Uri;
58+
import com.ning.http.util.StringUtils;
5759

5860
import java.io.IOException;
5961
import java.nio.charset.Charset;
@@ -220,7 +222,7 @@ else if (request.getCompositeByteData() != null)
220222
nettyBody = new NettyCompositeByteArrayBody(request.getCompositeByteData());
221223

222224
else if (request.getStringData() != null)
223-
nettyBody = new NettyByteArrayBody(request.getStringData().getBytes(bodyCharset));
225+
nettyBody = new NettyByteBufferBody(StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset));
224226

225227
else if (request.getStreamData() != null)
226228
nettyBody = new NettyInputStreamBody(request.getStreamData());
@@ -231,8 +233,7 @@ else if (isNonEmpty(request.getFormParams())) {
231233
if (!request.getHeaders().containsKey(HttpHeaders.Names.CONTENT_TYPE))
232234
contentType = HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED;
233235

234-
// FIXME use a ByteBuffer
235-
nettyBody = new NettyByteArrayBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType);
236+
nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType);
236237

237238
} else if (isNonEmpty(request.getParts()))
238239
nettyBody = new NettyMultipartBody(request.getParts(), request.getHeaders(), nettyConfig);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package com.ning.http.client.providers.netty.request.body;
15+
16+
import org.jboss.netty.buffer.ChannelBuffer;
17+
import org.jboss.netty.buffer.ChannelBuffers;
18+
19+
import java.nio.ByteBuffer;
20+
21+
public class NettyByteBufferBody extends NettyDirectBody {
22+
23+
private final ByteBuffer bb;
24+
private final String contentType;
25+
private final long length;
26+
27+
public NettyByteBufferBody(ByteBuffer bb) {
28+
this(bb, null);
29+
}
30+
31+
public NettyByteBufferBody(ByteBuffer bb, String contentType) {
32+
this.bb = bb;
33+
length = bb.remaining();
34+
bb.mark();
35+
this.contentType = contentType;
36+
}
37+
38+
@Override
39+
public long getContentLength() {
40+
return length;
41+
}
42+
43+
@Override
44+
public String getContentType() {
45+
return contentType;
46+
}
47+
48+
@Override
49+
public ChannelBuffer channelBuffer() {
50+
// for retry
51+
bb.reset();
52+
return ChannelBuffers.wrappedBuffer(bb);
53+
}
54+
}

src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*/
1313
package com.ning.http.util;
1414

15-
import static java.nio.charset.StandardCharsets.*;
1615
import static com.ning.http.util.MiscUtils.isNonEmpty;
16+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
1717

1818
import com.ning.http.client.AsyncHttpClientConfig;
1919
import com.ning.http.client.HttpResponseBodyPart;
@@ -26,6 +26,7 @@
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.io.UnsupportedEncodingException;
29+
import java.nio.ByteBuffer;
2930
import java.nio.charset.Charset;
3031
import java.util.List;
3132

@@ -177,8 +178,8 @@ public static StringBuilder urlEncodeFormParams0(List<Param> params) {
177178
return sb;
178179
}
179180

180-
public static byte[] urlEncodeFormParams(List<Param> params, Charset charset) {
181-
return StringUtils.charSequence2Bytes(urlEncodeFormParams0(params), charset);
181+
public static ByteBuffer urlEncodeFormParams(List<Param> params, Charset charset) {
182+
return StringUtils.charSequence2ByteBuffer(urlEncodeFormParams0(params), charset);
182183
}
183184

184185
private static void encodeAndAppendFormParam(final StringBuilder sb, final CharSequence name, final CharSequence value) {

0 commit comments

Comments
 (0)