I'm trying to convert the InputStream to GZIPInputStream using BodySubscribers.mapping as below.
Java Doc says "The mapping function is executed using the client's executor, and can therefore be used to map any response body type, including blocking InputStream, as shown in the following example which uses a well-known JSON parser to convert an InputStream into any annotated Java type."
But it doesn't work as described in the doc. It seems to be it gone into deadlock.
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodySubscriber;
import static java.net.http.HttpResponse.BodySubscribers;
import java.net.URI;
import java.io.InputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class GZip {
public static void main(String args[]) throws Exception {
HttpClient httpClient = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(args[0])).build();
BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream();
BodySubscriber<InputStream> downstream = BodySubscribers.mapping(upstream, is -> {
try (is) {
return new GZIPInputStream(is);
} catch (IOException e) {}
return null;
});
BodyHandler<InputStream> bh0 = rsp -> {
return downstream;
};
var response = httpClient.sendAsync(request, bh0);
var res = response.join();
System.err.println("res:" + res.body());
}
}