Java-Data Compression 2 (52), java52
Step 4: Modify the packaging class Myresponse2 and put the output data in a memory cache.
Package cn. hongxin. filter; public class GzipFilter2 implements Filter {public void init (FilterConfig filterConfig) throws ServletException {} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {HttpServletResponse resp = (HttpServletResponse) response; // declare the packaging Class Object MyResponse2 myresp = new MyResponse2 (resp); // allow and call oneServlet. doGet chain. doFilter (request, myresp); // Step 2: Read native data from myresp2 byte [] src = myresp. getSrc (); // Step 3: Start to compress ByteArrayOutputStream destBytes = new ByteArrayOutputStream (); GZIPOutputStream zip = new GZIPOutputStream (destBytes); zip. write (src); zip. close (); // Step 3: Output-use the native response resp. setContentType ("text/html; charset = UTF-8"); // get the Compressed Data byte [] dest = destBytes. toByteArray (); System. err. println ("before compression:" + src. length); System. err. println ("after compression:" + dest. length); // set the header resp. setContentLength (dest. length); resp. setHeader ("Content-Encoding", "gzip"); // output OutputStream out = resp. getOutputStream (); out. write (dest);} public void destroy (){}}
// Step 1: declare the response packaging class MyResponse2 extends HttpServletResponseWrapper {// set this container to/a.txt and declare the member variable private ByteArrayOutputStream srcByte; public MyResponse2 (HttpServletResponse response) {super (response);} // modify the enhanced getWtier method @ Override public PrintWriter getWriter () throws IOException {srcByte = new ByteArrayOutputStream (); printWriter out = new PrintWriter (new OutputStreamWriter (srcByte, "UTF-8"); return out;} // provides a way to get native data public byte [] getSrc () {return srcByte. toByteArray ();}}
Step 5: All jsp files must be compressed
If you wrap rersponse and modify the getWriter method, a printwiter object is returned. Declare a container object for storing the original data. Data Compression can be achieved.
Public class GzipFilter2 implements Filter {public void init (FilterConfig filterConfig) throws ServletException {} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {HttpServletResponse resp = (HttpServletResponse) response; // declare the packaging Class Object MyResponse2 myresp = new MyResponse2 (resp); // allow and call oneServlet. doGet chain. doFilter (request, myresp); // Step 2: Read native data from myresp2 byte [] src = myresp. getSrc (); // Step 3: Start to compress ByteArrayOutputStream destBytes = new ByteArrayOutputStream (); GZIPOutputStream zip = new GZIPOutputStream (destBytes); zip. write (src); zip. close (); // Step 3: Output-use the native response resp. setContentType ("text/html; charset = UTF-8"); // get the Compressed Data byte [] dest = destBytes. toByteArray (); System. err. println ("before compression:" + src. length); System. err. println ("after compression:" + dest. length); // set the header resp. setContentLength (dest. length); resp. setHeader ("Content-Encoding", "gzip"); // output OutputStream out = resp. getOutputStream (); out. write (dest);} public void destroy (){}}
// Step 1: declare the response packaging class MyResponse2 extends {// set this container to/a.txt and declare the member variable private ByteArrayOutputStream srcByte; private PrintWriter out; public MyResponse2 (HttpServletResponse response) {super (response) ;}// modify the enhanced getWtier method @ Override public PrintWriter getWriter () throws IOException {srcByte = new ByteArrayOutputStream (); out = new PrintWriter (new OutputStreamWriter (srcByte, "UTF-8"); return out;} // provides a way to get the native data public byte [] getSrc () {if (out! = Null) {out. close () ;}return srcByte. toByteArray ();}}
The configuration is as follows:
<Filter>
<Filter-name> zip2 </filter-name>
<Filter-class> cn. itcast. filter. GzipFilter2 </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> zip2 </filter-name>
<Url-pattern> *. jsp </url-pattern>
</Filter-mapping>
Getoutputstream and getWtier Compression
In the myrespons2 class, the getoutputstream must also be overwritten.
Returns an anonymous ServletOutputSteam object.
Implement the witer (int B) method to write data to bytearrayoyutpoutstream.
PublicServletOutputStream getOutputStream ()ThrowsIOException {
SrcByte =NewByteArrayOutputStream ();
ServletOutputStream out =NewServletOutputStream (){
// All IO files are written in bytes.
@ Override
Public VoidWrite (IntB)ThrowsIOException {
System.Err. Println (">>>:" + B );
SrcByte. write (B); // write it to your own cache-equivalent to StringBuffer. append ("" + B );
}
};
ReturnOut;
}