KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > filter > GZIPResponseStream


1 package com.blandware.atleap.webapp.filter;
2
3 import javax.servlet.ServletOutputStream JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.util.zip.GZIPOutputStream JavaDoc;
9
10
11 /**
12  * <p> Wraps Response Stream for GZipFilter</p>
13  *
14  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
15  * @version $Revision: 1.3 $ $Date: 2005/02/03 08:42:00 $
16  */

17 public class GZIPResponseStream extends ServletOutputStream JavaDoc {
18     // abstraction of the output stream used for compression
19
protected OutputStream JavaDoc bufferedOutput = null;
20
21     // state keeping variable for if close() has been called
22
protected boolean closed = false;
23
24     // reference to original response
25
protected HttpServletResponse JavaDoc response = null;
26
27     // reference to the output stream to the client's browser
28
protected ServletOutputStream JavaDoc output = null;
29
30     // default size of the in-memory buffer
31
protected int bufferSize = 50000;
32
33     public GZIPResponseStream(HttpServletResponse JavaDoc response)
34             throws IOException JavaDoc {
35         super();
36         closed = false;
37         this.response = response;
38         this.output = response.getOutputStream();
39         bufferedOutput = new ByteArrayOutputStream JavaDoc();
40     }
41
42     public void close() throws IOException JavaDoc {
43         // verify the stream is yet to be closed
44
if ( closed ) {
45             throw new IOException JavaDoc("This output stream has already been closed");
46         }
47
48         // if we buffered everything in memory, gzip it
49
if ( bufferedOutput instanceof ByteArrayOutputStream JavaDoc ) {
50             // get the content
51
ByteArrayOutputStream JavaDoc baos = (ByteArrayOutputStream JavaDoc) bufferedOutput;
52
53             // prepare a gzip stream
54
ByteArrayOutputStream JavaDoc compressedContent =
55                     new ByteArrayOutputStream JavaDoc();
56             GZIPOutputStream JavaDoc gzipstream =
57                     new GZIPOutputStream JavaDoc(compressedContent);
58             byte[] bytes = baos.toByteArray();
59             gzipstream.write(bytes);
60             gzipstream.finish();
61
62             // get the compressed content
63
byte[] compressedBytes = compressedContent.toByteArray();
64
65             // set appropriate HTTP headers
66
response.setContentLength(compressedBytes.length);
67             response.addHeader("Content-Encoding", "gzip");
68             output.write(compressedBytes);
69             output.flush();
70             output.close();
71             closed = true;
72         }
73         // if things were not buffered in memory, finish the GZIP stream and response
74
else if ( bufferedOutput instanceof GZIPOutputStream JavaDoc ) {
75             // cast to appropriate type
76
GZIPOutputStream JavaDoc gzipstream = (GZIPOutputStream JavaDoc) bufferedOutput;
77
78             // finish the compression
79
gzipstream.finish();
80
81             // finish the response
82
output.flush();
83             output.close();
84             closed = true;
85         }
86     }
87
88     public void flush() throws IOException JavaDoc {
89         if ( closed ) {
90             throw new IOException JavaDoc("Cannot flush a closed output stream");
91         }
92
93         bufferedOutput.flush();
94     }
95
96     public void write(int b) throws IOException JavaDoc {
97         if ( closed ) {
98             throw new IOException JavaDoc("Cannot write to a closed output stream");
99         }
100
101         // make sure we aren't over the buffer's limit
102
checkBufferSize(1);
103
104         // write the byte to the temporary output
105
bufferedOutput.write((byte) b);
106     }
107
108     protected void checkBufferSize(int length) throws IOException JavaDoc {
109         // check if we are buffering too large of a file
110
if ( bufferedOutput instanceof ByteArrayOutputStream JavaDoc ) {
111             ByteArrayOutputStream JavaDoc baos = (ByteArrayOutputStream JavaDoc) bufferedOutput;
112
113             if ( (baos.size() + length) > bufferSize ) {
114                 // files too large to keep in memory are sent to the client without Content-Length specified
115
response.addHeader("Content-Encoding", "gzip");
116
117                 // get existing bytes
118
byte[] bytes = baos.toByteArray();
119
120                 // make new gzip stream using the response output stream
121
GZIPOutputStream JavaDoc gzipstream = new GZIPOutputStream JavaDoc(output);
122                 gzipstream.write(bytes);
123
124                 // we are no longer buffering, send content via gzipstream
125
bufferedOutput = gzipstream;
126             }
127         }
128     }
129
130     public void write(byte[] b) throws IOException JavaDoc {
131         write(b, 0, b.length);
132     }
133
134     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
135
136         if ( closed ) {
137             throw new IOException JavaDoc("Cannot write to a closed output stream");
138         }
139
140         // make sure we aren't over the buffer's limit
141
checkBufferSize(len);
142
143         // write the content to the buffer
144
bufferedOutput.write(b, off, len);
145     }
146
147     public boolean closed() {
148         return (this.closed);
149     }
150
151     public void reset() {
152         //noop
153
}
154 }
155
Popular Tags