CompressionResponseStreampublic class CompressionResponseStream extends ServletOutputStream Implementation of ServletOutputStream that works with
the CompressionServletResponseWrapper implementation. |
Fields Summary |
---|
protected int | compressionThresholdThe threshold number which decides to compress or not.
Users can configure in web.xml to set it to fit their needs. | private int | debugDebug level | protected byte[] | bufferThe buffer through which all of our output bytes are passed. | protected int | bufferCountThe number of data bytes currently in the buffer. | protected GZIPOutputStream | gzipstreamThe underlying gzip output stream to which we should write data. | protected boolean | closedHas this stream been closed? | protected int | lengthThe content length past which we will not write, or -1 if there is
no defined content length. | protected HttpServletResponse | responseThe response with which this servlet output stream is associated. | protected ServletOutputStream | outputThe 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.
super();
closed = false;
this.response = response;
this.output = response.getOutputStream();
|
Methods Summary |
---|
public void | close()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 boolean | closed()Has this response stream been closed?
return (this.closed);
| public void | flush()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 void | flushToGZip()
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 void | setBuffer(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 void | setDebugLevel(int debug)Set debug level
// --------------------------------------------------------- Public Methods
this.debug = debug;
| public void | write(int b)Write the specified byte to our output stream.
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 void | write(byte[] b)Write b.length bytes from the specified byte array
to our output stream.
write(b, 0, b.length);
| public 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.
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 void | writeToGZip(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");
}
response.addHeader("Content-Encoding", "gzip");
gzipstream = new GZIPOutputStream(output);
}
gzipstream.write(b, off, len);
|
|