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