org.ibisph.filter.gzip
Class ResponseStream

java.lang.Object
  extended by java.io.OutputStream
      extended by javax.servlet.ServletOutputStream
          extended by org.ibisph.filter.gzip.ResponseStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class ResponseStream
extends javax.servlet.ServletOutputStream

Provides gzip stream compression for the gzip filter and response wrapper.

Author:
Garth Braithwaite, STG

Field Summary
protected  javax.servlet.ServletOutputStream destinationOutputStream
           
protected  java.util.zip.GZIPOutputStream gzipOutputStream
           
 
Constructor Summary
ResponseStream(javax.servlet.http.HttpServletResponse response)
          Constructor which saves original destination response and creates a new gzip output stream.
 
Method Summary
 void close()
          Core method that causes the captured content to be zipped and streamed back.
 void flush()
          Flushes the captured gzipped output stream but does not close.
 void write(byte b)
          Overridden method that captures data to the buffered byte array which is then gzipped.
 void write(byte[] b)
          Overridden method that captures data to the buffered byte array which is then gzipped.
 void write(byte[] b, int off, int len)
          Overridden method that captures data to the buffered byte array which is then gzipped.
 void write(int b)
          Overridden method that captures data to the buffered byte array which is then gzipped.
 
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

destinationOutputStream

protected javax.servlet.ServletOutputStream destinationOutputStream

gzipOutputStream

protected java.util.zip.GZIPOutputStream gzipOutputStream
Constructor Detail

ResponseStream

public ResponseStream(javax.servlet.http.HttpServletResponse response)
               throws java.io.IOException
Constructor which saves original destination response and creates a new gzip output stream. Saves the original response's output stream to be used as the compressions output stream. Creates a new gzipped output stream which will feed data via this objects write methods. The saved/ original response will have the "Content-Encoding" header with a value of "gzip" added.

Parameters:
response - Original response object which contains the "real" destination output stream.
Throws:
java.io.IOException
Method Detail

write

public void write(byte b)
           throws java.io.IOException
Overridden method that captures data to the buffered byte array which is then gzipped.

Parameters:
b - Value to be written to the output stream.
Throws:
java.io.IOException - standard IOException associated with write operation.

write

public void write(int b)
           throws java.io.IOException
Overridden method that captures data to the buffered byte array which is then gzipped.

Specified by:
write in class java.io.OutputStream
Parameters:
b - Value to be written to the output stream.
Throws:
java.io.IOException

write

public void write(byte[] b)
           throws java.io.IOException
Overridden method that captures data to the buffered byte array which is then gzipped.

Overrides:
write in class java.io.OutputStream
Parameters:
b - Array of values to be written to the output stream.
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Overridden method that captures data to the buffered byte array which is then gzipped.

Overrides:
write in class java.io.OutputStream
Parameters:
b - Array of values to be written to the output stream.
off - Offset of where to write the bytes to the new captured output stream.
len - Number of bytes to the new captured output stream.
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Core method that causes the captured content to be zipped and streamed back. Finishes the captured gzipped output stream (actually causes the gzip object to process the data), it then flushes the "real" destination output stream and closes it.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flushes the captured gzipped output stream but does not close.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException