Skip to content

Commit 39485c1

Browse files
committed
Small optimization for issue 99
1 parent e47344f commit 39485c1

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,24 @@ public final static String contentToString(Collection<HttpResponseBodyPart> body
185185
}
186186

187187
public final static byte[] contentToByte(Collection<HttpResponseBodyPart> bodyParts) throws UnsupportedEncodingException {
188-
int size = 0;
189-
for (HttpResponseBodyPart body : bodyParts) {
190-
size += body.getBodyPartBytes().length;
191-
}
192-
byte[] bytes = new byte[size];
193-
int offset = 0;
194-
for (HttpResponseBodyPart body : bodyParts) {
195-
byte[] bodyBytes = body.getBodyPartBytes();
196-
System.arraycopy(bodyBytes, 0, bytes, offset, bodyBytes.length);
197-
offset += bodyBytes.length;
198-
}
188+
if (bodyParts.size() == 1) {
189+
return bodyParts.iterator().next().getBodyPartBytes();
199190

200-
return bytes;
191+
} else {
192+
int size = 0;
193+
for (HttpResponseBodyPart body : bodyParts) {
194+
size += body.getBodyPartBytes().length;
195+
}
196+
byte[] bytes = new byte[size];
197+
int offset = 0;
198+
for (HttpResponseBodyPart body : bodyParts) {
199+
byte[] bodyBytes = body.getBodyPartBytes();
200+
System.arraycopy(bodyBytes, 0, bytes, offset, bodyBytes.length);
201+
offset += bodyBytes.length;
202+
}
203+
204+
return bytes;
205+
}
201206
}
202207

203208
public final static URI getRedirectUri(URI uri, String location) {

0 commit comments

Comments
 (0)