I'm trying to build a JSON handler for the Java 11 HttpClient.
The problem I'm having is that trying to compose with BodySubscribers.ofInputStream()
results in never reading any data from the stream, and hanging forever.
@Override
public HttpResponse.BodySubscriber<Void> apply(HttpResponse.ResponseInfo responseInfo) {
return HttpResponse.BodySubscribers.mapping(
HttpResponse.BodySubscribers.ofInputStream(),
inputStream -> {
try {
System.out.print(inputStream.read());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return null;
}
);
}
In contrast, returning the stream and reading it afterwards does work.
InputStream inputStream = client.send(request, HttpResponse.BodyHandlers.ofInputStream()).body();
System.out.print(inputStream.read()); // out: 123
What am I missing?