Skip to content

Commit 29d812e

Browse files
committed
Fixed ApacheResponse.getResponseBodyAsStream() to return all the bytes in the body
ApacheResponse.getResponseBodyAsStream() was returning an InputStream that only provided bytes from the first body part. Pulled JDKResponse's ByteArrayCollectionInputStream into its own class, HttpResponseBodyPartsInputStream, so it could be shared with ApacheResponse.
1 parent 72a080a commit 29d812e

File tree

3 files changed

+67
-46
lines changed

3 files changed

+67
-46
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2010 Ning, Inc.
3+
*
4+
* Ning licenses this file to you under the Apache License, version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at:
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ning.http.client;
14+
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
18+
/**
19+
* An {@link InputStream} that reads all the elements in an array of {@link HttpResponseBodyPart}s.
20+
*/
21+
public class HttpResponseBodyPartsInputStream extends InputStream {
22+
23+
private final HttpResponseBodyPart[] parts;
24+
25+
private int currentPos = 0;
26+
private int bytePos = -1;
27+
private byte[] active;
28+
private int available = 0;
29+
30+
public HttpResponseBodyPartsInputStream(HttpResponseBodyPart[] parts) {
31+
this.parts = parts;
32+
active = parts[0].getBodyPartBytes();
33+
computeLength(parts);
34+
}
35+
36+
private void computeLength(HttpResponseBodyPart[] parts) {
37+
if (available == 0) {
38+
for (HttpResponseBodyPart p : parts) {
39+
available += p.getBodyPartBytes().length;
40+
}
41+
}
42+
}
43+
44+
@Override
45+
public int available() throws IOException {
46+
return available;
47+
}
48+
49+
@Override
50+
public int read() throws IOException {
51+
if (++bytePos >= active.length) {
52+
// No more bytes, so step to the next array.
53+
if (++currentPos >= parts.length) {
54+
return -1;
55+
}
56+
57+
bytePos = 0;
58+
active = parts[currentPos].getBodyPartBytes();
59+
}
60+
61+
return active[bytePos] & 0xFF;
62+
}
63+
}

src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.ning.http.client.Cookie;
1616
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1717
import com.ning.http.client.HttpResponseBodyPart;
18+
import com.ning.http.client.HttpResponseBodyPartsInputStream;
1819
import com.ning.http.client.HttpResponseHeaders;
1920
import com.ning.http.client.HttpResponseStatus;
2021
import com.ning.http.client.Response;
@@ -91,7 +92,7 @@ public String getResponseBody(String charset) throws IOException {
9192
/* @Override */
9293
public InputStream getResponseBodyAsStream() throws IOException {
9394
if (bodyParts.size() > 0) {
94-
return new ByteArrayInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()])[0].getBodyPartBytes());
95+
return new HttpResponseBodyPartsInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
9596
} else {
9697
return new ByteArrayInputStream("".getBytes());
9798
}

src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.ning.http.client.Cookie;
1616
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1717
import com.ning.http.client.HttpResponseBodyPart;
18+
import com.ning.http.client.HttpResponseBodyPartsInputStream;
1819
import com.ning.http.client.HttpResponseHeaders;
1920
import com.ning.http.client.HttpResponseStatus;
2021
import com.ning.http.client.Response;
@@ -102,56 +103,12 @@ public InputStream getResponseBodyAsStream() throws IOException {
102103
}
103104

104105
if (bodyParts.size() > 0) {
105-
return new ByteArrayCollectionInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
106+
return new HttpResponseBodyPartsInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
106107
} else {
107108
return new ByteArrayInputStream("".getBytes());
108109
}
109110
}
110111

111-
private static class ByteArrayCollectionInputStream extends InputStream {
112-
113-
private final HttpResponseBodyPart[] parts;
114-
115-
private int currentPos = 0;
116-
private int bytePos = -1;
117-
private byte[] active;
118-
private int available = 0;
119-
120-
public ByteArrayCollectionInputStream(HttpResponseBodyPart[] parts) {
121-
this.parts = parts;
122-
active = parts[0].getBodyPartBytes();
123-
computeLength(parts);
124-
}
125-
126-
private void computeLength(HttpResponseBodyPart[] parts) {
127-
if (available == 0) {
128-
for (HttpResponseBodyPart p : parts) {
129-
available += p.getBodyPartBytes().length;
130-
}
131-
}
132-
}
133-
134-
@Override
135-
public int available() throws IOException {
136-
return available;
137-
}
138-
139-
@Override
140-
public int read() throws IOException {
141-
if (++bytePos >= active.length) {
142-
// No more bytes, so step to the next array.
143-
if (++currentPos >= parts.length) {
144-
return -1;
145-
}
146-
147-
bytePos = 0;
148-
active = parts[currentPos].getBodyPartBytes();
149-
}
150-
151-
return active[bytePos] & 0xFF;
152-
}
153-
}
154-
155112
/* @Override */
156113

157114
public String getResponseBodyExcerpt(int maxLength) throws IOException {

0 commit comments

Comments
 (0)