Implementing compression is straightforward since support for the gzip format is built in to the java programming language by classes in java.util.zip.
// Change the definition of "out" depending on whether
// or not gzip is supported.
PrintWriter out;
if (GzipUtilities.isGzipSupported(request) &&
!GzipUtilities.isGzipDisabled(request)) {
out = GzipUtilities.getGzipWriter(response);
response.setHeader("Content-Encoding", "gzip");
} else {
out = response.getWriter();
}
...
out.close(); // Needed for gzip; optional otherwise.
// Change the definition of "out" depending on whether
// or not gzip is supported.
PrintWriter out;
if (GzipUtilities.isGzipSupported(request) &&
!GzipUtilities.isGzipDisabled(request)) {
out = GzipUtilities.getGzipWriter(response);
response.setHeader("Content-Encoding", "gzip");
} else {
out = response.getWriter();
}
...
out.close(); // Needed for gzip; optional otherwise.
public static boolean isGzipSupported
(HttpServletRequest request) {
String encodings = request.getHeader("Accept-Encoding");
return((encodings != null) &&
(encodings.indexOf("gzip") != -1));
}
/** Return gzipping PrintWriter for response. */
public static PrintWriter getGzipWriter
(HttpServletResponse response) throws IOException {
return(new PrintWriter
(new GZIPOutputStream
(response.getOutputStream())));
}
Comments
Post a Comment