|
50 | 50 | import java.io.InputStreamReader;
|
51 | 51 | import java.io.OutputStream;
|
52 | 52 | import java.io.Reader;
|
53 |
| -import java.io.UnsupportedEncodingException; |
| 53 | +import java.io.StringReader; |
54 | 54 | import java.net.URLDecoder;
|
55 | 55 | import java.nio.charset.StandardCharsets;
|
56 | 56 | import java.util.Collection;
|
@@ -143,11 +143,7 @@ private static boolean includeRequestHeader(String headerName) {
|
143 | 143 | }
|
144 | 144 |
|
145 | 145 | private static String urlDecode(String input) {
|
146 |
| - try { |
147 |
| - return URLDecoder.decode(input, "UTF-8"); |
148 |
| - } catch (UnsupportedEncodingException e) { |
149 |
| - throw new SafeUncheckedIoException("Failed to decode path segment", e, UnsafeArg.of("encoded", input)); |
150 |
| - } |
| 146 | + return URLDecoder.decode(input, StandardCharsets.UTF_8); |
151 | 147 | }
|
152 | 148 |
|
153 | 149 | private static Optional<RequestBody> requestBody(Request request) {
|
@@ -239,6 +235,17 @@ public InputStream asInputStream() {
|
239 | 235 |
|
240 | 236 | @Override
|
241 | 237 | public Reader asReader() {
|
| 238 | + Integer maybeLength = length(); |
| 239 | + if (maybeLength != null && maybeLength < 8192) { |
| 240 | + // Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, |
| 241 | + // see https://github.com/FasterXML/jackson-core/pull/1081 |
| 242 | + try (InputStream inputStream = asInputStream()) { |
| 243 | + return new StringReader(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)); |
| 244 | + } catch (IOException e) { |
| 245 | + throw new SafeUncheckedIoException( |
| 246 | + "Failed to read response body", e, SafeArg.of("length", maybeLength)); |
| 247 | + } |
| 248 | + } |
242 | 249 | return new InputStreamReader(asInputStream(), StandardCharsets.UTF_8);
|
243 | 250 | }
|
244 | 251 |
|
|
0 commit comments