Rakesh API

org.rakeshv.filters
Class CompressionResponseStream

java.lang.Object
  extended by java.io.OutputStream
      extended by javax.servlet.ServletOutputStream
          extended by org.rakeshv.filters.CompressionResponseStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class CompressionResponseStream
extends javax.servlet.ServletOutputStream

A sub-class of ServletOutputStream that is used to compress the response sent by the server to a client request. The only compression method supported are those that are available in the java.util.zip package.

© Copyright 2003, Rakesh Vidyadharan

Version:
$Id: CompressionResponseStream.java,v 1.5 2005/10/15 01:10:20 rakesh Exp $
Author:
Rakesh Vidyadharan 2nd September 2003

Field Summary
protected  boolean closed
          Has this stream been closed?
protected  int compressionType
          The type of compression algorithm to use.
protected  java.util.zip.DeflaterOutputStream deflaterStream
          The underlying gzip output stream to which we should write data.
protected  javax.servlet.ServletOutputStream output
          The underlying servlet output stream to which we should write data.
protected  javax.servlet.http.HttpServletResponse response
          The response with which this servlet output stream is associated.
 
Constructor Summary
CompressionResponseStream(javax.servlet.http.HttpServletResponse response)
          Initialise a new instance of this class with the specified servlet response.
 
Method Summary
 void close()
          Close this output stream, causing any buffered data to be flushed and any further output data to throw an IOException.
protected  void createDeflaterStream()
          Initialise the deflaterStream with the appropriate type of compressed output stream based upon the value in the compressionType instance variable.
 void finish()
          Finishes writing compressed data to the output stream without closing the underlying stream.
 void flush()
          Flushes the underlying servlet output stream.
 boolean getClosed()
          Returns closed.
 int getCompressionType()
          Returns compressionType.
 void setCompressionType(int compressionType)
          Set compressionType.
 void write(byte[] buf)
          Over-ridden to ensure that the method just invokes the write( byte[], int, int ) method.
 void write(byte[] buf, int off, int len)
          Over-ridden form of the method.
 void write(int value)
          Over-ridden form of the method.
protected  void writeToDeflater(byte[] buf, int off, int len)
          Writes the date from the specified byte array to the deflaterStream.
protected  void writeToDeflater(int value)
          Write the specified value to the deflaterStream.
 
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

deflaterStream

protected java.util.zip.DeflaterOutputStream deflaterStream
The underlying gzip output stream to which we should write data.


closed

protected boolean closed
Has this stream been closed?


response

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


output

protected javax.servlet.ServletOutputStream output
The underlying servlet output stream to which we should write data.


compressionType

protected int compressionType
The type of compression algorithm to use. The supported algorithms are Deflate, GZIP, Zip.

Constructor Detail

CompressionResponseStream

public CompressionResponseStream(javax.servlet.http.HttpServletResponse response)
                          throws java.io.IOException
Initialise a new instance of this class with the specified servlet response.

Parameters:
response - The response to which the contents are to be sent.
Throws:
java.io.IOException
Method Detail

write

public void write(int value)
           throws java.io.IOException
Over-ridden form of the method. Writes the specified int to the deflaterStream if the stream has not been closed. Uses the writeToDeflater( int ) method to write the specified value to the deflaterStream.

Specified by:
write in class java.io.OutputStream
Parameters:
value - - The integer value that is being written.
Throws:
java.io.IOException - - If the stream has already been closed.

writeToDeflater

protected void writeToDeflater(int value)
                        throws java.io.IOException
Write the specified value to the deflaterStream. Checks to ensure that the deflaterStream has been created, and if not, then creates the appropriate instance of the deflaterStream using the compressionType value.

Parameters:
value - - The value to write.
Throws:
java.io.IOException - - If errors are encountered while trying to write to the stream.

write

public void write(byte[] buf)
           throws java.io.IOException
Over-ridden to ensure that the method just invokes the write( byte[], int, int ) method.

Overrides:
write in class java.io.OutputStream
Parameters:
buf - - The byte buffer from which the data is to read and written to the deflaterStream.
Throws:
java.io.IOException - - If errors are encountered while writing to the stream.

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws java.io.IOException
Over-ridden form of the method. Writes the byte array to the deflaterStream using writeToDeflater( byte[], int, int).

Overrides:
write in class java.io.OutputStream
Parameters:
buf - - The byte buffer from which the data is to read and written to the deflaterStream.
off - - The offset from the start of the buffer from which to start reading data.
len - - The length of data that is to be read from the array.
Throws:
java.io.IOException - - If errors are encountered while writing to the stream.

writeToDeflater

protected void writeToDeflater(byte[] buf,
                               int off,
                               int len)
                        throws java.io.IOException
Writes the date from the specified byte array to the deflaterStream. Checks to ensure that the deflaterStream has been created, and if not, then it creates the appropriate instance of the deflaterStream based upon the value of compressionType.

Parameters:
buf - - The byte buffer from which the data is to read and written to the deflaterStream.
off - - The offset from the start of the buffer from which to start reading data.
len - - The length of data that is to be read from the array.
Throws:
java.io.IOException - - If errors are encountered while writing to the stream.

createDeflaterStream

protected void createDeflaterStream()
                             throws java.io.IOException
Initialise the deflaterStream with the appropriate type of compressed output stream based upon the value in the compressionType instance variable. If the value of compressionType does not match one of the following, then Deflate compression is used.
  1. CompressionFilter.GZIP_COMPRESSION
  2. CompressionFilter.ZIP_COMPRESSION

Note: If zip compression is requested, then the response will contain a ZipEntry object that contains the data as required by the Zip format. The file name for the ZipEntry object is arbitrarily set as the value of System.currentTimeMillis()

.

Throws:
java.io.IOException - - If errors are encountered while creating the stream, or setting its header.

close

public void close()
           throws java.io.IOException
Close this output stream, causing any buffered data to be flushed and any further output data to throw an IOException.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - - If the stream has already been closed.

finish

public void finish()
            throws java.io.IOException
Finishes writing compressed data to the output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream.

Throws:
java.io.IOException - - If errors are encountered while writing to the stream. An exception will be thrown if this method is invoked after the stream has been closed.

flush

public void flush()
           throws java.io.IOException
Flushes the underlying servlet output stream. Flushes any unwritten content in the buffer to the servlet output stream.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - - If the stream has already been closed.

getClosed

public final boolean getClosed()
Returns closed.

Returns:
boolean - The value/reference of/to closed.

getCompressionType

public final int getCompressionType()
Returns compressionType.

Returns:
int - The value/reference of/to compressionType.

setCompressionType

public final void setCompressionType(int compressionType)
Set compressionType.

Parameters:
compressionType - - The value to set.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan