Skip to content

Commit 9985d91

Browse files
author
Adrian Cole
committed
Avoids InputStream.available when determining if a stream is empty
InputStream.available isn't a reliable api. This uses an alternative approach, which is to read the first byte to see if it is present. This allows us to continue to avoid "No content to map due to end-of-input" errors, but in a more supportable way. Fixes OpenFeign#250
1 parent b2b4085 commit 9985d91

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

jackson/src/main/java/feign/jackson/JacksonDecoder.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
2222

23+
import java.io.BufferedReader;
2324
import java.io.IOException;
24-
import java.io.InputStream;
25+
import java.io.Reader;
2526
import java.lang.reflect.Type;
2627
import java.util.Collections;
2728

@@ -50,12 +51,18 @@ public Object decode(Response response, Type type) throws IOException {
5051
if (response.body() == null) {
5152
return null;
5253
}
53-
InputStream inputStream = response.body().asInputStream();
54+
Reader reader = response.body().asReader();
55+
if (!reader.markSupported()) {
56+
reader = new BufferedReader(reader, 1);
57+
}
5458
try {
55-
if (inputStream.available() <= 0){
56-
return null;
59+
// Read the first byte to see if we have any data
60+
reader.mark(1);
61+
if (reader.read() == -1) {
62+
return null; // Eagerly returning null avoids "No content to map due to end-of-input"
5763
}
58-
return mapper.readValue(inputStream, mapper.constructType(type));
64+
reader.reset();
65+
return mapper.readValue(reader, mapper.constructType(type));
5966
} catch (RuntimeJsonMappingException e) {
6067
if (e.getCause() != null && e.getCause() instanceof IOException) {
6168
throw IOException.class.cast(e.getCause());

0 commit comments

Comments
 (0)