|
15 | 15 | */ |
16 | 16 | package feign.httpclient; |
17 | 17 |
|
18 | | -import feign.Client; |
19 | | -import feign.Request; |
20 | | -import feign.Response; |
21 | | -import feign.Util; |
22 | 18 | import org.apache.http.Header; |
23 | 19 | import org.apache.http.HttpEntity; |
24 | 20 | import org.apache.http.HttpResponse; |
|
35 | 31 | import org.apache.http.impl.client.HttpClientBuilder; |
36 | 32 | import org.apache.http.util.EntityUtils; |
37 | 33 |
|
38 | | -import java.io.ByteArrayInputStream; |
39 | 34 | import java.io.IOException; |
40 | 35 | import java.io.InputStream; |
41 | 36 | import java.io.InputStreamReader; |
|
50 | 45 | import java.util.List; |
51 | 46 | import java.util.Map; |
52 | 47 |
|
| 48 | +import feign.Client; |
| 49 | +import feign.Request; |
| 50 | +import feign.Response; |
| 51 | +import feign.Util; |
| 52 | + |
| 53 | +import static feign.Util.UTF_8; |
| 54 | + |
53 | 55 | /** |
54 | 56 | * This module directs Feign's http requests to Apache's |
55 | 57 | * <a href="https://hc.apache.org/httpcomponents-client-ga/">HttpClient</a>. Ex. |
@@ -165,43 +167,36 @@ Response toFeignResponse(HttpResponse httpResponse) throws IOException { |
165 | 167 | } |
166 | 168 |
|
167 | 169 | Response.Body toFeignBody(HttpResponse httpResponse) throws IOException { |
168 | | - HttpEntity entity = httpResponse.getEntity(); |
169 | | - final Integer length = entity != null && entity.getContentLength() != -1 ? |
170 | | - (int) entity.getContentLength() : |
171 | | - null; |
172 | | - final InputStream input = entity != null ? |
173 | | - new ByteArrayInputStream(EntityUtils.toByteArray(entity)) : |
174 | | - null; |
175 | | - |
| 170 | + final HttpEntity entity = httpResponse.getEntity(); |
| 171 | + if (entity == null) { |
| 172 | + return null; |
| 173 | + } |
176 | 174 | return new Response.Body() { |
177 | 175 |
|
178 | | - @Override |
179 | | - public void close() throws IOException { |
180 | | - if (input != null) { |
181 | | - input.close(); |
182 | | - } |
183 | | - } |
184 | | - |
185 | 176 | @Override |
186 | 177 | public Integer length() { |
187 | | - return length; |
| 178 | + return entity.getContentLength() < 0 ? (int) entity.getContentLength() : null; |
188 | 179 | } |
189 | 180 |
|
190 | 181 | @Override |
191 | 182 | public boolean isRepeatable() { |
192 | | - return false; |
| 183 | + return entity.isRepeatable(); |
193 | 184 | } |
194 | 185 |
|
195 | 186 | @Override |
196 | 187 | public InputStream asInputStream() throws IOException { |
197 | | - return input; |
| 188 | + return entity.getContent(); |
198 | 189 | } |
199 | 190 |
|
200 | 191 | @Override |
201 | 192 | public Reader asReader() throws IOException { |
202 | | - return new InputStreamReader(input); |
| 193 | + return new InputStreamReader(asInputStream(), UTF_8); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void close() throws IOException { |
| 198 | + EntityUtils.consume(entity); |
203 | 199 | } |
204 | 200 | }; |
205 | 201 | } |
206 | | - |
207 | 202 | } |
0 commit comments