1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package compressionFilters;
18
19 import java.io.IOException;
20 import java.util.zip.GZIPOutputStream;
21
22 import javax.servlet.ServletOutputStream;
23 import javax.servlet.http.HttpServletResponse;
24
25
26
27
28
29
30
31
32
33
34
35 public class CompressionResponseStream
36 extends ServletOutputStream {
37
38
39
40
41
42
43
44
45
46 public CompressionResponseStream(HttpServletResponse response) throws IOException {
47
48 super();
49 closed = false;
50 this.response = response;
51 this.output = response.getOutputStream();
52
53 }
54
55
56
57
58
59
60
61
62 protected int compressionThreshold = 0;
63
64
65
66
67 private int debug = 0;
68
69
70
71
72 protected byte[] buffer = null;
73
74
75
76
77 protected int bufferCount = 0;
78
79
80
81
82 protected GZIPOutputStream gzipstream = null;
83
84
85
86
87 protected boolean closed = false;
88
89
90
91
92
93 protected int length = -1;
94
95
96
97
98 protected HttpServletResponse response = null;
99
100
101
102
103 protected ServletOutputStream output = null;
104
105
106
107
108
109
110 public void setDebugLevel(int debug) {
111 this.debug = debug;
112 }
113
114
115
116
117
118 protected void setBuffer(int threshold) {
119 compressionThreshold = threshold;
120 buffer = new byte[compressionThreshold];
121 if (debug > 1) {
122 System.out.println("buffer is set to " + compressionThreshold);
123 }
124 }
125
126
127
128
129
130 public void close() throws IOException {
131
132 if (debug > 1) {
133 System.out.println("close() @ CompressionResponseStream");
134 }
135 if (closed)
136 throw new IOException("This output stream has already been closed");
137
138 if (gzipstream != null) {
139 flushToGZip();
140 gzipstream.close();
141 gzipstream = null;
142 } else {
143 if (bufferCount > 0) {
144 if (debug > 2) {
145 System.out.print("output.write(");
146 System.out.write(buffer, 0, bufferCount);
147 System.out.println(")");
148 }
149 output.write(buffer, 0, bufferCount);
150 bufferCount = 0;
151 }
152 }
153
154 output.close();
155 closed = true;
156
157 }
158
159
160
161
162
163
164 public void flush() throws IOException {
165
166 if (debug > 1) {
167 System.out.println("flush() @ CompressionResponseStream");
168 }
169 if (closed) {
170 throw new IOException("Cannot flush a closed output stream");
171 }
172
173 if (gzipstream != null) {
174 gzipstream.flush();
175 }
176
177 }
178
179 public void flushToGZip() throws IOException {
180
181 if (debug > 1) {
182 System.out.println("flushToGZip() @ CompressionResponseStream");
183 }
184 if (bufferCount > 0) {
185 if (debug > 1) {
186 System.out.println("flushing out to GZipStream, bufferCount = " + bufferCount);
187 }
188 writeToGZip(buffer, 0, bufferCount);
189 bufferCount = 0;
190 }
191
192 }
193
194
195
196
197
198
199
200 public void write(int b) throws IOException {
201
202 if (debug > 1) {
203 System.out.println("write " + b + " in CompressionResponseStream ");
204 }
205 if (closed)
206 throw new IOException("Cannot write to a closed output stream");
207
208 if (bufferCount >= buffer.length) {
209 flushToGZip();
210 }
211
212 buffer[bufferCount++] = (byte) b;
213
214 }
215
216
217
218
219
220
221
222
223
224 public void write(byte b[]) throws IOException {
225
226 write(b, 0, b.length);
227
228 }
229
230
231
232
233
234
235
236
237
238
239
240 public void write(byte b[], int off, int len) throws IOException {
241
242 if (debug > 1) {
243 System.out.println("write, bufferCount = " + bufferCount + " len = " + len + " off = " + off);
244 }
245 if (debug > 2) {
246 System.out.print("write(");
247 System.out.write(b, off, len);
248 System.out.println(")");
249 }
250
251 if (closed)
252 throw new IOException("Cannot write to a closed output stream");
253
254 if (len == 0)
255 return;
256
257
258 if (len <= (buffer.length - bufferCount)) {
259 System.arraycopy(b, off, buffer, bufferCount, len);
260 bufferCount += len;
261 return;
262 }
263
264
265 flushToGZip();
266
267
268 if (len <= (buffer.length - bufferCount)) {
269 System.arraycopy(b, off, buffer, bufferCount, len);
270 bufferCount += len;
271 return;
272 }
273
274
275 writeToGZip(b, off, len);
276 }
277
278 public void writeToGZip(byte b[], int off, int len) throws IOException {
279
280 if (debug > 1) {
281 System.out.println("writeToGZip, len = " + len);
282 }
283 if (debug > 2) {
284 System.out.print("writeToGZip(");
285 System.out.write(b, off, len);
286 System.out.println(")");
287 }
288 if (gzipstream == null) {
289 if (debug > 1) {
290 System.out.println("new GZIPOutputStream");
291 }
292 response.addHeader("Content-Encoding", "gzip");
293 gzipstream = new GZIPOutputStream(output);
294 }
295 gzipstream.write(b, off, len);
296
297 }
298
299
300
301
302
303
304
305 public boolean closed() {
306
307 return (this.closed);
308
309 }
310
311 }