} else {
// Tell browser we are sending it gzipped data.
res.setHeader("Content-Encoding", "gzip");
// Invoke resource, accumulating output in the wrapper.
ByteArrayResponseWrapper responseWrapper =
new ByteArrayResponseWrapper(response);
chain.doFilter(req, responseWrapper);
ByteArrayOutputStream outputStream = responseWrapper.getByteArrayOutputStream();
// Get character array representing output.
mLogger.debug("Pre-zip size:" + outputStream.size());
// Make a writer that compresses data and puts
// it into a byte array.
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
GZIPOutputStream zipOut = new GZIPOutputStream(byteStream);
// Compress original output and put it into byte array.
zipOut.write(responseWrapper.getByteArrayOutputStream().toByteArray());
// Gzip streams must be explicitly closed.
zipOut.close();
mLogger.debug("Gzip size:" + byteStream.size());
// Update the Content-Length header.
res.setContentLength(byteStream.size());
ByteArrayOutputStreamWrapper newOut =
(ByteArrayOutputStreamWrapper) responseWrapper.getOutputStream();
newOut.clear();
newOut.setFinallized();
/* now force close of OutputStream */
newOut.write(byteStream.toByteArray());