Skip to content

Commit 0409872

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 7a83d78 commit 0409872

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ public interface Body extends Closeable {
218218

219219
/** It is the responsibility of the caller to close the stream. */
220220
Reader asReader() throws IOException;
221+
222+
/** */
223+
Reader asReader(Charset charset) throws IOException;
221224
}
222225

223226
private static final class InputStreamBody implements Response.Body {
@@ -257,6 +260,12 @@ public Reader asReader() throws IOException {
257260
return new InputStreamReader(inputStream, UTF_8);
258261
}
259262

263+
@Override
264+
public Reader asReader(Charset charset) throws IOException {
265+
checkNotNull(charset, "charset should not be null");
266+
return new InputStreamReader(inputStream, charset);
267+
}
268+
260269
@Override
261270
public void close() throws IOException {
262271
inputStream.close();
@@ -306,6 +315,12 @@ public Reader asReader() throws IOException {
306315
return new InputStreamReader(asInputStream(), UTF_8);
307316
}
308317

318+
@Override
319+
public Reader asReader(Charset charset) throws IOException {
320+
checkNotNull(charset, "charset should not be null");
321+
return new InputStreamReader(asInputStream(), charset);
322+
}
323+
309324
@Override
310325
public void close() throws IOException {}
311326

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.lang.reflect.InvocationHandler;
2929
import java.lang.reflect.Method;
3030
import java.lang.reflect.Type;
31+
import java.nio.charset.Charset;
3132
import java.util.Arrays;
3233
import java.util.Collections;
3334
import java.util.HashMap;
@@ -402,6 +403,11 @@ public Reader asReader() throws IOException {
402403
return original.body().asReader();
403404
}
404405

406+
@Override
407+
public Reader asReader(Charset charset) throws IOException {
408+
return original.body().asReader(charset);
409+
}
410+
405411
@Override
406412
public void close() throws IOException {
407413
closed.set(true);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.MalformedURLException;
2828
import java.net.URI;
2929
import java.net.URISyntaxException;
30+
import java.nio.charset.Charset;
3031
import java.util.ArrayList;
3132
import java.util.Collection;
3233
import java.util.HashMap;
@@ -226,6 +227,12 @@ public Reader asReader() throws IOException {
226227
return new InputStreamReader(asInputStream(), UTF_8);
227228
}
228229

230+
@Override
231+
public Reader asReader(Charset charset) throws IOException {
232+
Util.checkNotNull(charset, "charset should not be null");
233+
return new InputStreamReader(asInputStream(), charset);
234+
}
235+
229236
@Override
230237
public void close() throws IOException {
231238
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
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.io.InputStream;
1919
import java.io.Reader;
20+
import java.nio.charset.Charset;
2021
import java.util.Collection;
2122
import java.util.Map;
2223
import java.util.concurrent.TimeUnit;
@@ -143,6 +144,11 @@ public InputStream asInputStream() throws IOException {
143144
public Reader asReader() throws IOException {
144145
return input.charStream();
145146
}
147+
148+
@Override
149+
public Reader asReader(Charset charset) throws IOException {
150+
return asReader();
151+
}
146152
};
147153
}
148154

0 commit comments

Comments
 (0)