KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filters > GzipResponseStream


1 package jodd.servlet.filters;
2
3 import java.io.IOException;
4 import java.util.zip.GZIPOutputStream;
5
6 import javax.servlet.ServletOutputStream;
7 import javax.servlet.http.HttpServletResponse;
8
9
10
11 public class GzipResponseStream extends ServletOutputStream {
12
13     /**
14      * Construct a servlet output stream associated with the specified Response.
15      *
16      * @param response The associated response
17      *
18      * @exception IOException
19      */

20     public GzipResponseStream(HttpServletResponse response) throws IOException {
21         super();
22         closed = false;
23         this.response = response;
24         this.output = response.getOutputStream();
25     }
26
27
28     /**
29      * The threshold number which decides to compress or not.
30      */

31     protected int compressionThreshold = 0;
32
33     /**
34      * The buffer through which all of our output bytes are passed.
35      */

36     protected byte[] buffer = null;
37
38     /**
39      * The number of data bytes currently in the buffer.
40      */

41     protected int bufferCount = 0;
42
43     /**
44      * The underlying gzip output stream to which we should write data.
45      */

46     protected GZIPOutputStream gzipstream = null;
47
48     /**
49      * Has this stream been closed?
50      */

51     protected boolean closed = false;
52
53     /**
54      * The content length past which we will not write, or -1 if there is no
55      * defined content length.
56      */

57     protected int length = -1;
58
59     /**
60      * The response with which this servlet output stream is associated.
61      */

62     protected HttpServletResponse response = null;
63
64     /**
65      * The underlying servket output stream to which we should write data.
66      */

67     protected ServletOutputStream output = null;
68
69
70
71     /**
72      * Set the compressionThreshold number and create buffer for this size
73      *
74      * @param threshold
75      */

76     protected void setBuffer(int threshold) {
77         compressionThreshold = threshold;
78         buffer = new byte[compressionThreshold];
79     }
80
81     /**
82      * Close this output stream, causing any buffered data to be flushed and any
83      * further output data to throw an IOException.
84      *
85      * @exception IOException
86      */

87     public void close() throws IOException {
88         if (closed == true) {
89             return;
90         }
91         if (gzipstream != null) {
92             flushToGZip();
93             gzipstream.close();
94             gzipstream = null;
95         } else {
96             if (bufferCount > 0) {
97                 output.write(buffer, 0, bufferCount);
98                 bufferCount = 0;
99             }
100         }
101         output.close();
102         closed = true;
103     }
104
105
106     /**
107      * Flush any buffered data for this output stream, which also causes the
108      * response to be committed.
109      *
110      * @exception IOException
111      */

112     public void flush() throws IOException {
113
114         if (closed) {
115             return;
116         }
117         if (gzipstream != null) {
118             gzipstream.flush();
119         }
120
121     }
122
123     public void flushToGZip() throws IOException {
124         if (bufferCount > 0) {
125             writeToGZip(buffer, 0, bufferCount);
126             bufferCount = 0;
127         }
128     }
129
130     /**
131      * Write the specified byte to our output stream.
132      *
133      * @param b The byte to be written
134      *
135      * @exception IOException
136      * if an input/output error occurs
137      */

138     public void write(int b) throws IOException {
139
140         if (closed) {
141             throw new IOException("Cannot write to a closed output stream");
142         }
143         if (bufferCount >= buffer.length) {
144             flushToGZip();
145         }
146         buffer[bufferCount++] = (byte) b;
147     }
148
149
150     /**
151      * Write <code>b.length</code> bytes from the specified byte array to our
152      * output stream.
153      *
154      * @param b byte array to be written
155      *
156      * @exception IOException
157      * if an input/output error occurs
158      */

159     public void write(byte b[]) throws IOException {
160         write(b, 0, b.length);
161     }
162
163
164     /**
165      * Write <code>len</code> bytes from the specified byte array, starting at
166      * the specified offset, to our output stream.
167      *
168      * @param b byte array containing the bytes to be written
169      * @param off zero-relative starting offset of the bytes to be written
170      * @param len number of bytes to be written
171      *
172      * @exception IOException
173      * if an input/output error occurs
174      */

175     public void write(byte b[], int off, int len) throws IOException {
176
177         if (closed) {
178             throw new IOException("Cannot write to a closed output stream");
179         }
180
181         if (len == 0) {
182             return;
183         }
184
185         // Can we write into buffer ?
186
if (len <= (buffer.length - bufferCount)) {
187             System.arraycopy(b, off, buffer, bufferCount, len);
188             bufferCount += len;
189             return;
190         }
191
192         // There is not enough space in buffer. Flush it ...
193
flushToGZip();
194
195         // ... and try again. Note, that bufferCount = 0 here !
196
if (len <= (buffer.length - bufferCount)) {
197             System.arraycopy(b, off, buffer, bufferCount, len);
198             bufferCount += len;
199             return;
200         }
201
202         // write direct to gzip
203
writeToGZip(b, off, len);
204     }
205
206     public void writeToGZip(byte b[], int off, int len) throws IOException {
207
208         if (gzipstream == null) {
209             gzipstream = new GZIPOutputStream(output);
210             response.addHeader("Content-Encoding", "gzip");
211         }
212         gzipstream.write(b, off, len);
213
214     }
215
216
217     /**
218      * Has this response stream been closed?
219      *
220      * @return true if stream has been closed
221      */

222     public boolean closed() {
223         return(this.closed);
224     }
225
226 }
227
Popular Tags