org.apache.click.extras.filter
Class CompressionResponseStream

java.lang.Object
  extended by java.io.OutputStream
      extended by javax.servlet.ServletOutputStream
          extended by org.apache.click.extras.filter.CompressionResponseStream
All Implemented Interfaces:
Closeable, Flushable

public class CompressionResponseStream
extends ServletOutputStream

Provides an implementation of ServletOutputStream that works with the CompressionServletResponseWrapper implementation.

Note this Jakarta Tomcat examples Filter is packaged in Click Extras for convenience.


Field Summary
protected  byte[] buffer
          The buffer through which all of our output bytes are passed.
protected  int bufferCount
          The number of data bytes currently in the buffer.
protected  boolean closed
          Has this stream been closed?
protected  int compressionThreshold
          The threshold number which decides to compress or not.
protected  OutputStream gzipstream
          The underlying gzip output stream to which we should write data.
protected  int length
          The content length past which we will not write, or -1 if there is no defined content length.
protected  OutputStream output
          The underlying output stream, either gzipped or servlet, to which we should write data.
protected  HttpServletRequest request
          The request with which this servlet is associated.
protected  HttpServletResponse response
          The response with which this servlet output stream is associated.
 
Constructor Summary
CompressionResponseStream(HttpServletResponse response, HttpServletRequest request)
          Construct a servlet output stream associated with the specified Response.
 
Method Summary
 void close()
          Close this output stream, causing any buffered data to be flushed.
 boolean closed()
          Has this response stream been closed?
 void flush()
          Flush any buffered data for this output stream, which also causes the response to be committed.
 void flushToGZip()
          Flush the buffer to the gzip stream.
protected  void initializeGzip()
          Initialize the GZip output stream.
protected  void setBuffer(int threshold)
          Set the compressionThreshold number and create buffer for this size.
protected  boolean setContentEncodingGZip()
          Set the "Content-Encoding" header of the response to "gzip", returning true if the header was set, false otherwise.
 void write(byte[] b)
          Write b.length bytes from the specified byte array to our output stream.
 void write(byte[] b, int off, int len)
          Write len bytes from the specified byte array, starting at the specified offset, to our output stream.
 void write(int b)
          Write the specified byte to our output stream.
 void writeToGZip(byte[] b, int off, int len)
          Writes array of bytes to the compressed output stream.
 
Methods inherited from class javax.servlet.ServletOutputStream
print, print, print, print, print, print, print, println, println, println, println, println, println, println, println
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

compressionThreshold

protected int compressionThreshold
The threshold number which decides to compress or not. Users can configure in web.xml to set it to fit their needs.


buffer

protected byte[] buffer
The buffer through which all of our output bytes are passed.


bufferCount

protected int bufferCount
The number of data bytes currently in the buffer.


gzipstream

protected OutputStream gzipstream
The underlying gzip output stream to which we should write data.


closed

protected boolean closed
Has this stream been closed?


length

protected int length
The content length past which we will not write, or -1 if there is no defined content length.


response

protected HttpServletResponse response
The response with which this servlet output stream is associated.


request

protected HttpServletRequest request
The request with which this servlet is associated.


output

protected OutputStream output
The underlying output stream, either gzipped or servlet, to which we should write data.

Constructor Detail

CompressionResponseStream

public CompressionResponseStream(HttpServletResponse response,
                                 HttpServletRequest request)
                          throws IOException
Construct a servlet output stream associated with the specified Response.

Parameters:
response - The associated response
request - The associated request
Throws:
IOException - if an IO error occurs reading the response stream
Method Detail

setBuffer

protected void setBuffer(int threshold)
Set the compressionThreshold number and create buffer for this size.

Parameters:
threshold - the compression threshold in bytes

close

public void close()
           throws IOException
Close this output stream, causing any buffered data to be flushed. Consecutive calls to this method will be ignored.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException - if an error occurs closing the response

flush

public void flush()
           throws IOException
Flush any buffered data for this output stream, which also causes the response to be committed.

Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException - if an error occurs flushing the gzip stream

flushToGZip

public void flushToGZip()
                 throws IOException
Flush the buffer to the gzip stream.

Throws:
IOException - if an error occurs flushing the buffer

write

public void write(int b)
           throws IOException
Write the specified byte to our output stream.

Specified by:
write in class OutputStream
Parameters:
b - The byte to be written
Throws:
IOException - if an input/output error occurs

write

public void write(byte[] b)
           throws IOException
Write b.length bytes from the specified byte array to our output stream.

Overrides:
write in class OutputStream
Parameters:
b - The byte array to be written
Throws:
IOException - if an input/output error occurs

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Write len bytes from the specified byte array, starting at the specified offset, to our output stream.

Overrides:
write in class OutputStream
Parameters:
b - The byte array containing the bytes to be written
off - Zero-relative starting offset of the bytes to be written
len - The number of bytes to be written
Throws:
IOException - if an input/output error occurs

writeToGZip

public void writeToGZip(byte[] b,
                        int off,
                        int len)
                 throws IOException
Writes array of bytes to the compressed output stream. This method will block until all the bytes are written.

Parameters:
b - the data to be written
off - the start offset of the data
len - the length of the data
Throws:
IOException - If an I/O error has occurred.

closed

public boolean closed()
Has this response stream been closed?

Returns:
true if the response stream has been closed

initializeGzip

protected void initializeGzip()
                       throws IOException
Initialize the GZip output stream.

This method delegates to setContentEncodingGZip() to set the GZip response Content-Encoding header.

Throws:
IOException - If an I/O error has occurred

setContentEncodingGZip

protected boolean setContentEncodingGZip()
Set the "Content-Encoding" header of the response to "gzip", returning true if the header was set, false otherwise.

This method will return false when it is invoked from a server side include (<jsp:include>), since its not possible to alter the headers of an included response.

Returns:
true if the content encoding was set, false otherwise