Skip to content

Commit baa8744

Browse files
arebyavelo
authored andcommitted
support charset when build Reader for Response.Body (#766)
* add charset to Response.Body when create Reader * format * support charset when build Reader for Response.Body * support charset when build Reader for Response.Body * support charset when build Reader for Response.Body * format header * format Response
1 parent b257369 commit baa8744

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

core/src/main/java/feign/Response.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ public interface Body extends Closeable {
221221
* It is the responsibility of the caller to close the stream.
222222
*/
223223
Reader asReader() throws IOException;
224+
225+
/**
226+
*
227+
*/
228+
Reader asReader(Charset charset) throws IOException;
224229
}
225230

226231
private static final class InputStreamBody implements Response.Body {
@@ -260,6 +265,12 @@ public Reader asReader() throws IOException {
260265
return new InputStreamReader(inputStream, UTF_8);
261266
}
262267

268+
@Override
269+
public Reader asReader(Charset charset) throws IOException {
270+
checkNotNull(charset, "charset should not be null");
271+
return new InputStreamReader(inputStream, charset);
272+
}
273+
263274
@Override
264275
public void close() throws IOException {
265276
inputStream.close();
@@ -309,6 +320,12 @@ public Reader asReader() throws IOException {
309320
return new InputStreamReader(asInputStream(), UTF_8);
310321
}
311322

323+
@Override
324+
public Reader asReader(Charset charset) throws IOException {
325+
checkNotNull(charset, "charset should not be null");
326+
return new InputStreamReader(asInputStream(), charset);
327+
}
328+
312329
@Override
313330
public void close() throws IOException {}
314331

core/src/test/java/feign/FeignBuilderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.Reader;
24+
import java.nio.charset.Charset;
2425
import java.lang.reflect.InvocationHandler;
2526
import java.lang.reflect.Method;
2627
import java.lang.reflect.Type;
@@ -399,6 +400,11 @@ public Reader asReader() throws IOException {
399400
return original.body().asReader();
400401
}
401402

403+
@Override
404+
public Reader asReader(Charset charset) throws IOException {
405+
return original.body().asReader(charset);
406+
}
407+
402408
@Override
403409
public void close() throws IOException {
404410
closed.set(true);

httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.InputStream;
3535
import java.io.InputStreamReader;
3636
import java.io.Reader;
37+
import java.nio.charset.Charset;
3738
import java.io.UnsupportedEncodingException;
3839
import java.net.MalformedURLException;
3940
import java.net.URI;
@@ -52,7 +53,7 @@
5253
/**
5354
* This module directs Feign's http requests to Apache's
5455
* <a href="https://hc.apache.org/httpcomponents-client-ga/">HttpClient</a>. Ex.
55-
*
56+
*
5657
* <pre>
5758
* GitHub github = Feign.builder().client(new ApacheHttpClient()).target(GitHub.class,
5859
* "https://api.github.com");
@@ -224,6 +225,12 @@ public Reader asReader() throws IOException {
224225
return new InputStreamReader(asInputStream(), UTF_8);
225226
}
226227

228+
@Override
229+
public Reader asReader(Charset charset) throws IOException {
230+
Util.checkNotNull(charset, "charset should not be null");
231+
return new InputStreamReader(asInputStream(), charset);
232+
}
233+
227234
@Override
228235
public void close() throws IOException {
229236
EntityUtils.consume(entity);

okhttp/src/main/java/feign/okhttp/OkHttpClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.Reader;
25+
import java.nio.charset.Charset;
2526
import java.util.Collection;
2627
import java.util.Map;
2728
import java.util.concurrent.TimeUnit;
@@ -142,6 +143,11 @@ public InputStream asInputStream() throws IOException {
142143
public Reader asReader() throws IOException {
143144
return input.charStream();
144145
}
146+
147+
@Override
148+
public Reader asReader(Charset charset) throws IOException {
149+
return asReader();
150+
}
145151
};
146152
}
147153

0 commit comments

Comments
 (0)