Skip to content

Commit 31b41ee

Browse files
committed
Optimize DialogueFeignClient small response reader
Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, see FasterXML/jackson-core#1081
1 parent 89e5797 commit 31b41ee

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

conjure-java-jaxrs-client/src/main/java/com/palantir/conjure/java/client/jaxrs/DialogueFeignClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@
4343
import com.palantir.logsafe.UnsafeArg;
4444
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
4545
import com.palantir.logsafe.exceptions.SafeRuntimeException;
46+
import com.palantir.logsafe.exceptions.SafeUncheckedIoException;
4647
import feign.Request;
4748
import java.io.ByteArrayInputStream;
4849
import java.io.IOException;
4950
import java.io.InputStream;
5051
import java.io.InputStreamReader;
5152
import java.io.OutputStream;
5253
import java.io.Reader;
54+
import java.io.StringReader;
5355
import java.io.UnsupportedEncodingException;
5456
import java.net.URLDecoder;
5557
import java.nio.charset.StandardCharsets;
@@ -239,6 +241,18 @@ public InputStream asInputStream() {
239241

240242
@Override
241243
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+
}
242256
return new InputStreamReader(asInputStream(), StandardCharsets.UTF_8);
243257
}
244258

0 commit comments

Comments
 (0)