10.2. Data Compression
Data compression is a technique that packs the data from its raw format into compact bundles, thereby reducing its size. There are many algorithms available for compressing data. One of the most popular of these is the "deflate" algorithm, which is a combination of Lempel-Ziv (LZ) 77 and Huffman encoding. The GNU zip, better known as gzip, is based on this algorithm. If you desire to read more about gzip and the algorithms it's based on, then the Wikipedia page on the topic, accessible online at http://en.wikipedia.org/wiki/Gzip, would be a good starting point.
BlazeDS, as you know well, is a Java Servlets–based web application. As data from a BlazeDS server leaves for the Flex client, it can be intercepted using the familiar Java Servlet filter mechanism that you saw in action earlier in Chapter 7. Servlet filters can intercept requests to and responses from a Java Servlet.
In order to gzip the data transferred from the server to the client, I create a filter to manipulate and compress the outgoing response. Then I configure it for my BlazeDS instance. I don't need any changes in the BlazeDS code. Destinations called by the Flex client that return the data are totally unaware of this filter.
To get started and gain an understanding of how the filter works, first browse through the filter code in Listing 10-1.
Example 10.1. GzipFilter Java Source
package dsadapters.filters.gzip; //Import statements -- available in the code download but removed to avoid superfluous ... |
Get Professional BlazeDS: Creating Rich Internet Applications with Flex® and Java® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.