1 25 26 package compressionFilters; 27 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.util.zip.GZIPOutputStream ; 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 35 43 44 public class CompressionResponseStream 45 extends ServletOutputStream { 46 47 48 50 51 56 public CompressionResponseStream(HttpServletResponse response) throws IOException { 57 58 super(); 59 closed = false; 60 this.response = response; 61 this.output = response.getOutputStream(); 62 63 } 64 65 66 68 69 73 protected int compressionThreshold = 0; 74 75 78 private int debug = 0; 79 80 83 protected byte[] buffer = null; 84 85 88 protected int bufferCount = 0; 89 90 93 protected GZIPOutputStream gzipstream = null; 94 95 98 protected boolean closed = false; 99 100 104 protected int length = -1; 105 106 109 protected HttpServletResponse response = null; 110 111 114 protected ServletOutputStream output = null; 115 116 117 119 122 public void setDebugLevel(int debug) { 123 this.debug = debug; 124 } 125 126 127 130 protected void setBuffer(int threshold) { 131 compressionThreshold = threshold; 132 buffer = new byte[compressionThreshold]; 133 if (debug > 1) { 134 System.out.println("buffer is set to "+compressionThreshold); 135 } 136 } 137 138 142 public void close() throws IOException { 143 144 if (debug > 1) { 145 System.out.println("close() @ CompressionResponseStream"); 146 } 147 if (closed) 148 throw new IOException ("This output stream has already been closed"); 149 150 if (gzipstream != null) { 151 flushToGZip(); 152 gzipstream.close(); 153 gzipstream = null; 154 } else { 155 if (bufferCount > 0) { 156 if (debug > 2) { 157 System.out.print("output.write("); 158 System.out.write(buffer, 0, bufferCount); 159 System.out.println(")"); 160 } 161 output.write(buffer, 0, bufferCount); 162 bufferCount = 0; 163 } 164 } 165 166 output.close(); 167 closed = true; 168 169 } 170 171 172 176 public void flush() throws IOException { 177 178 if (debug > 1) { 179 System.out.println("flush() @ CompressionResponseStream"); 180 } 181 if (closed) { 182 throw new IOException ("Cannot flush a closed output stream"); 183 } 184 185 if (gzipstream != null) { 186 gzipstream.flush(); 187 } 188 189 } 190 191 public void flushToGZip() throws IOException { 192 193 if (debug > 1) { 194 System.out.println("flushToGZip() @ CompressionResponseStream"); 195 } 196 if (bufferCount > 0) { 197 if (debug > 1) { 198 System.out.println("flushing out to GZipStream, bufferCount = " + bufferCount); 199 } 200 writeToGZip(buffer, 0, bufferCount); 201 bufferCount = 0; 202 } 203 204 } 205 206 213 public void write(int b) throws IOException { 214 215 if (debug > 1) { 216 System.out.println("write "+b+" in CompressionResponseStream "); 217 } 218 if (closed) 219 throw new IOException ("Cannot write to a closed output stream"); 220 221 if (bufferCount >= buffer.length) { 222 flushToGZip(); 223 } 224 225 buffer[bufferCount++] = (byte) b; 226 227 } 228 229 230 238 public void write(byte b[]) throws IOException { 239 240 write(b, 0, b.length); 241 242 } 243 244 245 255 public void write(byte b[], int off, int len) throws IOException { 256 257 if (debug > 1) { 258 System.out.println("write, bufferCount = " + bufferCount + " len = " + len + " off = " + off); 259 } 260 if (debug > 2) { 261 System.out.print("write("); 262 System.out.write(b, off, len); 263 System.out.println(")"); 264 } 265 266 if (closed) 267 throw new IOException ("Cannot write to a closed output stream"); 268 269 if (len == 0) 270 return; 271 272 if (len <= (buffer.length - bufferCount)) { 274 System.arraycopy(b, off, buffer, bufferCount, len); 275 bufferCount += len; 276 return; 277 } 278 279 flushToGZip(); 281 282 if (len <= (buffer.length - bufferCount)) { 284 System.arraycopy(b, off, buffer, bufferCount, len); 285 bufferCount += len; 286 return; 287 } 288 289 writeToGZip(b, off, len); 291 } 292 293 public void writeToGZip(byte b[], int off, int len) throws IOException { 294 295 if (debug > 1) { 296 System.out.println("writeToGZip, len = " + len); 297 } 298 if (debug > 2) { 299 System.out.print("writeToGZip("); 300 System.out.write(b, off, len); 301 System.out.println(")"); 302 } 303 if (gzipstream == null) { 304 if (debug > 1) { 305 System.out.println("new GZIPOutputStream"); 306 } 307 response.addHeader("Content-Encoding", "gzip"); 308 gzipstream = new GZIPOutputStream (output); 309 } 310 gzipstream.write(b, off, len); 311 312 } 313 314 315 317 318 321 public boolean closed() { 322 323 return (this.closed); 324 325 } 326 327 } 328 | Popular Tags |