| 
43 | 43 | import com.palantir.logsafe.UnsafeArg;  | 
44 | 44 | import com.palantir.logsafe.exceptions.SafeIllegalStateException;  | 
45 | 45 | import com.palantir.logsafe.exceptions.SafeRuntimeException;  | 
 | 46 | +import com.palantir.logsafe.exceptions.SafeUncheckedIoException;  | 
46 | 47 | import feign.Request;  | 
47 | 48 | import java.io.ByteArrayInputStream;  | 
48 | 49 | import java.io.IOException;  | 
49 | 50 | import java.io.InputStream;  | 
50 | 51 | import java.io.InputStreamReader;  | 
51 | 52 | import java.io.OutputStream;  | 
52 | 53 | import java.io.Reader;  | 
 | 54 | +import java.io.StringReader;  | 
53 | 55 | import java.io.UnsupportedEncodingException;  | 
54 | 56 | import java.net.URLDecoder;  | 
55 | 57 | import java.nio.charset.StandardCharsets;  | 
@@ -239,6 +241,18 @@ public InputStream asInputStream() {  | 
239 | 241 | 
 
  | 
240 | 242 |         @Override  | 
241 | 243 |         public Reader asReader() {  | 
 | 244 | +            Integer maybeLength = length();  | 
 | 245 | +            if (maybeLength != null && maybeLength.intValue() < 8192) {  | 
 | 246 | +                // Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs,  | 
 | 247 | +                // see https://github.com/FasterXML/jackson-core/pull/1081  | 
 | 248 | +                try (InputStream inputStream = asInputStream()) {  | 
 | 249 | +                    String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);  | 
 | 250 | +                    return new StringReader(content);  | 
 | 251 | +                } catch (IOException e) {  | 
 | 252 | +                    throw new SafeUncheckedIoException(  | 
 | 253 | +                            "Failed to read response body", e, SafeArg.of("length", maybeLength));  | 
 | 254 | +                }  | 
 | 255 | +            }  | 
242 | 256 |             return new InputStreamReader(asInputStream(), StandardCharsets.UTF_8);  | 
243 | 257 |         }  | 
244 | 258 | 
 
  | 
 | 
0 commit comments