org.weborganic.bastille.cache.util
Class GZIPUtils

java.lang.Object
  extended by org.weborganic.bastille.cache.util.GZIPUtils

public final class GZIPUtils
extends Object

A utility class for GZIP compression.

See Also:
GZIP file format specification version 4.3

Method Summary
static void addGzipHeader(javax.servlet.http.HttpServletResponse res)
          Adds the gzip HTTP header to the response to indicates to clients that the response body is compressed.
static byte[] gzip(byte[] ungzipped)
          Gzip the specified content.
static boolean isGzipped(byte[] candidate)
          Checks the first two bytes of the candidate byte array for the magic number 0x677a.
static boolean shouldGzippedBodyBeZero(byte[] compressed)
          Checks whether a gzipped body is actually empty and should just be zero.
static byte[] ungzip(byte[] gzipped)
          A highly performant ungzip implementation.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isGzipped

public static boolean isGzipped(byte[] candidate)
Checks the first two bytes of the candidate byte array for the magic number 0x677a.

This magic number was obtained from /usr/share/file/magic. The line for gzip is:

 >>14    beshort 0x677a          (gzipped)
 

Parameters:
candidate - the byte array to check
Returns:
true if gzipped; false if null, less than two bytes or not gzipped.

shouldGzippedBodyBeZero

public static boolean shouldGzippedBodyBeZero(byte[] compressed)
Checks whether a gzipped body is actually empty and should just be zero.

When the compressedBytes is EMPTY_GZIPPED_CONTENT_SIZE it should be zero.

Parameters:
compressed - the gzipped response body
Returns:
true if the response should be 0, even if it is isn't.

addGzipHeader

public static void addGzipHeader(javax.servlet.http.HttpServletResponse res)
                          throws javax.servlet.ServletException
Adds the gzip HTTP header to the response to indicates to clients that the response body is compressed.

This method checks that the header has actually been included.

Parameters:
res - the response which will have a header added to it
Throws:
javax.servlet.ServletException - if the response is committed or set header is ignored.

gzip

public static byte[] gzip(byte[] ungzipped)
                   throws IOException
Gzip the specified content.

Parameters:
ungzipped - the bytes to be gzipped
Returns:
gzipped bytes
Throws:
IOException - the content was already compressed.

ungzip

public static byte[] ungzip(byte[] gzipped)
                     throws IOException
A highly performant ungzip implementation.

Parameters:
gzipped - the gzipped content
Returns:
an ungzipped byte[]
Throws:
IOException - Should an error occur while ungzipping the content