264. Compression

Enabling .gzip compression on the server is a common practice that's meant to significantly improve the site's load time. But JDK 11's HTTP Client API doesn't take advantage of .gzip compression. In other words, the HTTP Client API doesn't require compressed responses and doesn't know how to deal with such responses.

To request compressed responses, we have to send the Accept-Encoding header with the .gzip value. This header is not added by the HTTP Client API, so we will add it as follows:

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
.header("Accept-Encoding", "gzip")
.uri(URI.create("https://davidwalsh.name"))
.build();

This is just half of the job. So far, if the gzip encoding is enabled on the server, then we will receive a compressed response. To detect whether the response is compressed or not, we have to check the Encoding header, as follows:

HttpResponse<InputStream> response = client.send(
request, HttpResponse.BodyHandlers.ofInputStream());

String encoding = response.headers()
.firstValue("Content-Encoding").orElse("");

if ("gzip".equals(encoding)) {
String gzipAsString = gZipToString(response.body());
System.out.println(gzipAsString);
} else {
String isAsString = isToString(response.body());
System.out.println(isAsString);
}

The gZipToString() method is a helper method that takes InputStream and treats it as GZIPInputStream. In other words, this method reads the bytes from the given input stream and uses them to create a string:

public static String gzipToString(InputStream gzip) 
throws IOException {

byte[] allBytes;
try (InputStream fromIs = new GZIPInputStream(gzip)) {
allBytes = fromIs.readAllBytes();
}

return new String(allBytes, StandardCharsets.UTF_8);
}

If the response is not compressed, then isToString() is the helper method that we need:

public static String isToString(InputStream is) throws IOException {

byte[] allBytes;
try (InputStream fromIs = is) {
allBytes = fromIs.readAllBytes();
}

return new String(allBytes, StandardCharsets.UTF_8);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.79.241