FileDocCategorySizeDatePackage
CompressionResponseWrapper.javaAPI DocExample4365Thu Oct 21 10:16:26 BST 2004com.example

CompressionResponseWrapper

public class CompressionResponseWrapper extends HttpServletResponseWrapper
This response wrapper decorates the original response object by add a compression decorator on the original servlet output stream.

Fields Summary
private GZIPServletOutputStream
servletGzipOS
the compressed output stream for the servlet response
private PrintWriter
pw
the PrintWriter object to the compressed output stream
private Object
streamUsed
This state variable holds the first stream object created.
Constructors Summary
CompressionResponseWrapper(HttpServletResponse resp)
The constructor performs the Decorator responsibility of storing a reference to the object being decorated, in this case the HTTP response object.


                           
    
    super(resp);
  
Methods Summary
public java.util.zip.GZIPOutputStreamgetGZIPOutputStream()
This gives the compression filter a handle on the GZIP output stream so that it (the filter) can "finish" and flush the GZIP stream.

    return this.servletGzipOS.internalGzipOS;
  
public javax.servlet.ServletOutputStreamgetOutputStream()
Provide access to a decorated servlet output stream.


             
       
      // Allow the servlet to access a servlet output stream (SOS)
      // only if the servlet has not already accessed the print writer.
    if ( (streamUsed != null) && (streamUsed != servletGzipOS) ) {
      throw new IllegalStateException();
    }
    // Wrap the original servlet output stream with our compression SOS.
    // We'll look at this class in a minute.
    if ( servletGzipOS == null ) {
      servletGzipOS
	  = new GZIPServletOutputStream(getResponse()
					.getOutputStream());
      streamUsed = servletGzipOS;
    }
    return servletGzipOS;
  
public java.io.PrintWritergetWriter()
Provide access to a decorated print writer.

    // Allow the servlet to access a print writer only if the
    // servlet has not already accessed the servlet output stream.
    if ( (streamUsed != null) && (streamUsed != pw) ) {
      throw new IllegalStateException();
    }
    // To make a print writer, we have to first wrap the servlet output stream
    // and then wrap the compression SOS in two additional OS decorators:
    // OutputStreamWriter which converts characters into bytes, and then a
    // PrintWriter on top of the OSWriter object.
    if ( pw == null ) {
      servletGzipOS
	  = new GZIPServletOutputStream(getResponse().getOutputStream());
      // Use the response charset to create the OSWriter
      OutputStreamWriter osw
	  = new OutputStreamWriter(servletGzipOS,
				   getResponse().getCharacterEncoding());
      // Wrap the OSWriter in the PrintWriter
      pw = new PrintWriter(osw);
      streamUsed = pw;
    }
    return pw;
  
public voidsetContentLength(int len)
Ignore this method, because the real output will be compressed.