FileDocCategorySizeDatePackage
CompressionResponseStream.javaAPI DocApache Tomcat 6.0.149321Fri Jul 20 04:20:32 BST 2007compressionFilters

CompressionResponseStream

public class CompressionResponseStream extends ServletOutputStream
Implementation of ServletOutputStream that works with the CompressionServletResponseWrapper implementation.
author
Amy Roh
author
Dmitri Valdin
version
$Revision: 500668 $, $Date: 2007-01-28 00:07:51 +0100 (dim., 28 janv. 2007) $

Fields Summary
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.
private int
debug
Debug level
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 OutputStream
gzipstream
The underlying gzip output stream to which we should write data.
protected boolean
closed
Has this stream been closed?
protected int
length
The content length past which we will not write, or -1 if there is no defined content length.
protected HttpServletResponse
response
The response with which this servlet output stream is associated.
protected ServletOutputStream
output
The underlying servket output stream to which we should write data.
Constructors Summary
public CompressionResponseStream(HttpServletResponse response)
Construct a servlet output stream associated with the specified Response.

param
response The associated response


        super();
        closed = false;
        this.response = response;
        this.output = response.getOutputStream();

    
Methods Summary
public voidclose()
Close this output stream, causing any buffered data to be flushed and any further output data to throw an IOException.


        if (debug > 1) {
            System.out.println("close() @ CompressionResponseStream");
        }
        if (closed)
            throw new IOException("This output stream has already been closed");

        if (gzipstream != null) {
            flushToGZip();
            gzipstream.close();
            gzipstream = null;
        } else {
            if (bufferCount > 0) {
                if (debug > 2) {
                    System.out.print("output.write(");
                    System.out.write(buffer, 0, bufferCount);
                    System.out.println(")");
                }
                output.write(buffer, 0, bufferCount);
                bufferCount = 0;
            }
        }

        output.close();
        closed = true;

    
public booleanclosed()
Has this response stream been closed?


        return (this.closed);

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


        if (debug > 1) {
            System.out.println("flush() @ CompressionResponseStream");
        }
        if (closed) {
            throw new IOException("Cannot flush a closed output stream");
        }

        if (gzipstream != null) {
            gzipstream.flush();
        }

    
public voidflushToGZip()


        if (debug > 1) {
            System.out.println("flushToGZip() @ CompressionResponseStream");
        }
        if (bufferCount > 0) {
            if (debug > 1) {
                System.out.println("flushing out to GZipStream, bufferCount = " + bufferCount);
            }
            writeToGZip(buffer, 0, bufferCount);
            bufferCount = 0;
        }

    
protected voidsetBuffer(int threshold)
Set the compressionThreshold number and create buffer for this size

        compressionThreshold = threshold;
        buffer = new byte[compressionThreshold];
        if (debug > 1) {
            System.out.println("buffer is set to "+compressionThreshold);
        }
    
public voidsetDebugLevel(int debug)
Set debug level



    // --------------------------------------------------------- Public Methods

            
        
        this.debug = debug;
    
public voidwrite(int b)
Write the specified byte to our output stream.

param
b The byte to be written
exception
IOException if an input/output error occurs


        if (debug > 1) {
            System.out.println("write "+b+" in CompressionResponseStream ");
        }
        if (closed)
            throw new IOException("Cannot write to a closed output stream");

        if (bufferCount >= buffer.length) {
            flushToGZip();
        }

        buffer[bufferCount++] = (byte) b;

    
public voidwrite(byte[] b)
Write b.length bytes from the specified byte array to our output stream.

param
b The byte array to be written
exception
IOException if an input/output error occurs


        write(b, 0, b.length);

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

param
b The byte array containing the bytes to be written
param
off Zero-relative starting offset of the bytes to be written
param
len The number of bytes to be written
exception
IOException if an input/output error occurs


        if (debug > 1) {
            System.out.println("write, bufferCount = " + bufferCount + " len = " + len + " off = " + off);
        }
        if (debug > 2) {
            System.out.print("write(");
            System.out.write(b, off, len);
            System.out.println(")");
        }

        if (closed)
            throw new IOException("Cannot write to a closed output stream");

        if (len == 0)
            return;

        // Can we write into buffer ?
        if (len <= (buffer.length - bufferCount)) {
            System.arraycopy(b, off, buffer, bufferCount, len);
            bufferCount += len;
            return;
        }

        // There is not enough space in buffer. Flush it ...
        flushToGZip();

        // ... and try again. Note, that bufferCount = 0 here !
        if (len <= (buffer.length - bufferCount)) {
            System.arraycopy(b, off, buffer, bufferCount, len);
            bufferCount += len;
            return;
        }

        // write direct to gzip
        writeToGZip(b, off, len);
    
public voidwriteToGZip(byte[] b, int off, int len)


        if (debug > 1) {
            System.out.println("writeToGZip, len = " + len);
        }
        if (debug > 2) {
            System.out.print("writeToGZip(");
            System.out.write(b, off, len);
            System.out.println(")");
        }
        if (gzipstream == null) {
            if (debug > 1) {
                System.out.println("new GZIPOutputStream");
            }
            if (response.isCommitted()) {
                if (debug > 1)
                    System.out.print("Response already committed. Using original output stream");
                gzipstream = output;
            } else {
                response.addHeader("Content-Encoding", "gzip");
                gzipstream = new GZIPOutputStream(output);
            }
        }
        gzipstream.write(b, off, len);