Rakesh API

org.rakeshv.filters
Class CachingResponseStream

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

public class CachingResponseStream
extends javax.servlet.ServletOutputStream

A sub-class of ServletOutputStream that is used to cache the response sent by the server to a client request. This stream writes every byte that is send to the client to the cache file also.

© Copyright 2005, Rakesh Vidyadharan

Version:
$Id: CachingResponseStream.java,v 1.6 2005/10/15 01:42:32 rakesh Exp $
Author:
Rakesh Vidyadharan 15th September 2005

Field Summary
protected  java.io.BufferedOutputStream cacheFile
          The file to which the responses are to be cached.
protected  boolean closed
          Has this stream been closed?
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.
private  TeeOutputStream tee
          The tee that represents the output and cacheFile.
 
Constructor Summary
CachingResponseStream(javax.servlet.http.HttpServletResponse response, java.io.File file)
          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.
 void closeCacheFile()
          Closes the cacheFile without closing the output.
 void flush()
          Flushes the underlying servlet output stream.
 boolean getClosed()
          Returns closed.
 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.
 
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

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.


cacheFile

protected java.io.BufferedOutputStream cacheFile
The file to which the responses are to be cached.


tee

private TeeOutputStream tee
The tee that represents the output and cacheFile.

Constructor Detail

CachingResponseStream

public CachingResponseStream(javax.servlet.http.HttpServletResponse response,
                             java.io.File file)
                      throws java.io.IOException
Initialise a new instance of this class with the specified servlet response. Initialises the tee.

Parameters:
response - The response to which the contents are to be sent.
file - The file to which the response is to be cached.
Throws:
java.io.IOException - If errors are encountered while writing to the streams.
Method Detail

write

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

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.

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 output and cacheFile.
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 output and cacheFile.

Overrides:
write in class java.io.OutputStream
Parameters:
buf - - The byte buffer from which the data is to read and written to the output and cacheFile.
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.

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.

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.

closeCacheFile

public void closeCacheFile()
                    throws java.io.IOException
Closes the cacheFile without closing the output.

Note: This method must be invoked by to ensure that the cache file that was generated is properly closed after the actual response was generated.

Throws:
java.io.IOException - - If errors are encountered while closing the file.

getClosed

public final boolean getClosed()
Returns closed.

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

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan