Browse Source

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
pull/770/head
smithya 6 years ago committed by Marvin Froeder
parent
commit
cb611eff4d
  1. 17
      core/src/main/java/feign/Response.java
  2. 6
      core/src/test/java/feign/FeignBuilderTest.java
  3. 9
      httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java
  4. 6
      okhttp/src/main/java/feign/okhttp/OkHttpClient.java

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

@ -221,6 +221,11 @@ public final class Response implements Closeable { @@ -221,6 +221,11 @@ public final class Response implements Closeable {
* It is the responsibility of the caller to close the stream.
*/
Reader asReader() throws IOException;
/**
*
*/
Reader asReader(Charset charset) throws IOException;
}
private static final class InputStreamBody implements Response.Body {
@ -260,6 +265,12 @@ public final class Response implements Closeable { @@ -260,6 +265,12 @@ public final class Response implements Closeable {
return new InputStreamReader(inputStream, UTF_8);
}
@Override
public Reader asReader(Charset charset) throws IOException {
checkNotNull(charset, "charset should not be null");
return new InputStreamReader(inputStream, charset);
}
@Override
public void close() throws IOException {
inputStream.close();
@ -309,6 +320,12 @@ public final class Response implements Closeable { @@ -309,6 +320,12 @@ public final class Response implements Closeable {
return new InputStreamReader(asInputStream(), UTF_8);
}
@Override
public Reader asReader(Charset charset) throws IOException {
checkNotNull(charset, "charset should not be null");
return new InputStreamReader(asInputStream(), charset);
}
@Override
public void close() throws IOException {}

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

@ -21,6 +21,7 @@ import org.junit.Test; @@ -21,6 +21,7 @@ import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
@ -399,6 +400,11 @@ public class FeignBuilderTest { @@ -399,6 +400,11 @@ public class FeignBuilderTest {
return original.body().asReader();
}
@Override
public Reader asReader(Charset charset) throws IOException {
return original.body().asReader(charset);
}
@Override
public void close() throws IOException {
closed.set(true);

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

@ -34,6 +34,7 @@ import java.io.IOException; @@ -34,6 +34,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
@ -52,7 +53,7 @@ import static feign.Util.UTF_8; @@ -52,7 +53,7 @@ import static feign.Util.UTF_8;
/**
* This module directs Feign's http requests to Apache's
* <a href="https://hc.apache.org/httpcomponents-client-ga/">HttpClient</a>. Ex.
*
*
* <pre>
* GitHub github = Feign.builder().client(new ApacheHttpClient()).target(GitHub.class,
* "https://api.github.com");
@ -224,6 +225,12 @@ public final class ApacheHttpClient implements Client { @@ -224,6 +225,12 @@ public final class ApacheHttpClient implements Client {
return new InputStreamReader(asInputStream(), UTF_8);
}
@Override
public Reader asReader(Charset charset) throws IOException {
Util.checkNotNull(charset, "charset should not be null");
return new InputStreamReader(asInputStream(), charset);
}
@Override
public void close() throws IOException {
EntityUtils.consume(entity);

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

@ -22,6 +22,7 @@ import okhttp3.ResponseBody; @@ -22,6 +22,7 @@ import okhttp3.ResponseBody;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -142,6 +143,11 @@ public final class OkHttpClient implements Client { @@ -142,6 +143,11 @@ public final class OkHttpClient implements Client {
public Reader asReader() throws IOException {
return input.charStream();
}
@Override
public Reader asReader(Charset charset) throws IOException {
return asReader();
}
};
}

Loading…
Cancel
Save