KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

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

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

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

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

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

48     protected GZIPOutputStream JavaDoc gzipstream = null;
49
50     /**
51      * Has this stream been closed?
52      */

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

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

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

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

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

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

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

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

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

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

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