|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.io.OutputStream
javax.servlet.ServletOutputStream
org.hd.d.pg2k.webSvr.util.CompressionResponseStream
public final class CompressionResponseStream
Implementation of ServletOutputStream that works with CompressionServletResponseWrapper implementation.
| Field Summary | |
|---|---|
private byte[] |
buf1
Private per-instance buffer for write(int); never null. |
private java.io.ByteArrayOutputStream |
buffer
Buffer in which we accumulate output before starting compressed stream. |
private static int |
BUFSIZE
Minimum output buffer size to mop up output from GZIPOutputStream. |
protected boolean |
closed
Has this stream been closed? |
protected java.io.OutputStream |
compressedStream
The underlying gzip output stream to which we should write data. |
private int |
compressionThreshold
The threshold number which decides to compress or not; non-negative. |
private static boolean |
FLUSH_GZIP
If false, suppress flushes of the GZIP output stream itself until the stream is closed. |
private javax.servlet.ServletOutputStream |
output
The underlying servlet output stream to which we should write data; never null. |
private javax.servlet.http.HttpServletResponse |
response
The response with which this servlet output stream is associated; never null. |
private static boolean |
USE_JAZZLIB
If true, use JazzLib's flush()able Deflator rather than the JDK's. |
private CompressionServletResponseWrapper |
wrapper
The wrapper that created this; never null. |
| Constructor Summary | |
|---|---|
CompressionResponseStream(CompressionServletResponseWrapper wrapper,
javax.servlet.http.HttpServletResponse response)
Construct a servlet output stream associated with the specified Response. |
|
| Method Summary | |
|---|---|
void |
close()
Close this output stream. |
void |
flush()
Flush any buffered data for this output stream, and force the response to be committed. |
private void |
makeSureGZOSCreated()
Create the GZIPOutputStream if necessary and advertise the Content-Encoding in the HTTP headers. |
private void |
reapplySuppledContentHeaders()
Reapply user-supplied header values that would have conflicted with compression. |
protected void |
setBuffer(int threshold)
Set the compressionThreshold; must not be negative. |
void |
write(byte[] b)
Write the specified byte array to our output stream. |
void |
write(byte[] b,
int off,
int len)
Write the specified array portion to our output stream. |
void |
write(int b)
Write the specified byte to our output stream. |
private void |
writeToGZip(byte[] b,
int off,
int len)
Write the specified array portion to our compressed GZIP output stream. |
| 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 |
|---|
private int compressionThreshold
If positive, size of output below which we will not attempt to compress but will leave the output is it arrived.
private java.io.ByteArrayOutputStream buffer
protected java.io.OutputStream compressedStream
As an emergency swerve if we discover that someelse in the filter chain decided late to add a content encoding, this may in fact be a pass-through stream.
protected boolean closed
private final javax.servlet.http.HttpServletResponse response
private final CompressionServletResponseWrapper wrapper
private final javax.servlet.ServletOutputStream output
private static final int BUFSIZE
Using a BufferedOutputStream helps ensure that the chunks are large and efficent both to represent and to transmit.
private static final boolean USE_JAZZLIB
private static final boolean FLUSH_GZIP
If we use JazzLib then flush() is useful.
private final byte[] buf1
| Constructor Detail |
|---|
public CompressionResponseStream(CompressionServletResponseWrapper wrapper,
javax.servlet.http.HttpServletResponse response)
throws java.io.IOException
response - the associated response
java.io.IOException| Method Detail |
|---|
protected void setBuffer(int threshold)
This value may be ignored, ie treated as if zero.
public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in class java.io.OutputStreamjava.io.IOException
public void flush()
throws java.io.IOException
This will always ensure that the underlying stream is directly or indirectly flushed so as to commit the response.
flush in interface java.io.Flushableflush in class java.io.OutputStreamjava.io.IOException
private void makeSureGZOSCreated()
throws java.io.IOException
After this completes compressedStream will not be null.
Calling this commits us to whether we will use compression at all for this HTTP request. We will apply compression unless we find another Content-Encoding is being applied.
We wrap the output in a BufferedOutputStream first to soak up the single-byte leading and trailing writes that GZIPOutputStream up to JDK 1.4.0 does, and to avoid sending redundant blocks of data downstream (such as the gzip header) before we have to, since these will otherwise get sent as separate chunks/packets in some cases.
We also take this opportunity to add the "Content-Encoding" header to indicate that the output is GZIPed, and to add a "Vary" header to indicate that this content encoding was negotiated with the client so caches/proxies can handle differently-able clients properly.
We double-check that the a Content-Encoding has not now been applied since this might happen lazily. If it has then we duck and dive by using the raw underlying stream.
java.io.IOExceptionprivate void reapplySuppledContentHeaders()
We carefully avoid overwriting any values that have been set elsewhere.
public void write(int b)
throws java.io.IOException
write in class java.io.OutputStreamb - The byte to be written
java.io.IOException - if an input/output error occurs
public void write(byte[] b)
throws java.io.IOException
write in class java.io.OutputStreamb - The byte array to be written; never null
java.io.IOException - if an input/output error occurs
public void write(byte[] b,
int off,
int len)
throws java.io.IOException
write in class java.io.OutputStreamb - the byte array containing the bytes to be writtenoff - zero-relative starting offset of the bytes to be writtenlen - the number of bytes to be written
java.io.IOException - if an input/output error occurs
private void writeToGZip(byte[] b,
int off,
int len)
throws java.io.IOException
java.io.IOException - if an input/output error occurs
|
DHD Multimedia Gallery V1.53.0 | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||