1 package com.genimen.djeneric.web.util.compression; 2 3 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 33 34 public class CompressionResponseStream extends ServletOutputStream 35 { 36 37 42 public CompressionResponseStream(HttpServletResponse response) throws IOException 43 { 44 45 super(); 46 closed = false; 47 this.response = response; 48 this.output = response.getOutputStream(); 49 50 } 51 52 56 protected int compressionThreshold = 0; 57 58 61 private int debug = 0; 62 63 66 protected byte[] buffer = null; 67 68 71 protected int bufferCount = 0; 72 73 76 protected GZIPOutputStream gzipstream = null; 77 78 81 protected boolean closed = false; 82 83 87 protected int length = -1; 88 89 92 protected HttpServletResponse response = null; 93 94 97 protected ServletOutputStream output = null; 98 99 102 public void setDebugLevel(int debug) 103 { 104 this.debug = debug; 105 } 106 107 110 protected void setBuffer(int threshold) 111 { 112 compressionThreshold = threshold; 113 buffer = new byte[compressionThreshold]; 114 if (debug > 1) 115 { 116 System.out.println("buffer is set to " + compressionThreshold); 117 } 118 } 119 120 124 public void close() throws IOException 125 { 126 127 if (debug > 1) 128 { 129 System.out.println("close() @ CompressionResponseStream"); 130 } 131 if (closed) throw new IOException ("This output stream has already been closed"); 132 133 if (gzipstream != null) 134 { 135 flushToGZip(); 136 gzipstream.close(); 137 gzipstream = null; 138 } 139 else 140 { 141 if (bufferCount > 0) 142 { 143 if (debug > 2) 144 { 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 163 public void flush() throws IOException 164 { 165 166 if (debug > 1) 167 { 168 System.out.println("flush() @ CompressionResponseStream"); 169 } 170 if (closed) 171 { 172 throw new IOException ("Cannot flush a closed output stream"); 173 } 174 175 if (gzipstream != null) 176 { 177 gzipstream.flush(); 178 } 179 180 } 181 182 public void flushToGZip() throws IOException 183 { 184 185 if (debug > 1) 186 { 187 System.out.println("flushToGZip() @ CompressionResponseStream"); 188 } 189 if (bufferCount > 0) 190 { 191 if (debug > 1) 192 { 193 System.out.println("flushing out to GZipStream, bufferCount = " + bufferCount); 194 } 195 writeToGZip(buffer, 0, bufferCount); 196 bufferCount = 0; 197 } 198 199 } 200 201 208 public void write(int b) throws IOException 209 { 210 211 if (debug > 1) 212 { 213 System.out.println("write " + b + " in CompressionResponseStream "); 214 } 215 if (closed) throw new IOException ("Cannot write to a closed output stream"); 216 217 if (bufferCount >= buffer.length) 218 { 219 flushToGZip(); 220 } 221 222 buffer[bufferCount++] = (byte) b; 223 224 } 225 226 234 public void write(byte b[]) throws IOException 235 { 236 237 write(b, 0, b.length); 238 239 } 240 241 251 public void write(byte b[], int off, int len) throws IOException 252 { 253 254 if (debug > 1) 255 { 256 System.out.println("write, bufferCount = " + bufferCount + " len = " + len + " off = " + off); 257 } 258 if (debug > 2) 259 { 260 System.out.print("write("); 261 System.out.write(b, off, len); 262 System.out.println(")"); 263 } 264 265 if (closed) throw new IOException ("Cannot write to a closed output stream"); 266 267 if (len == 0) return; 268 269 if (len <= (buffer.length - bufferCount)) 271 { 272 System.arraycopy(b, off, buffer, bufferCount, len); 273 bufferCount += len; 274 return; 275 } 276 277 flushToGZip(); 279 280 if (len <= (buffer.length - bufferCount)) 282 { 283 System.arraycopy(b, off, buffer, bufferCount, len); 284 bufferCount += len; 285 return; 286 } 287 288 writeToGZip(b, off, len); 290 } 291 292 public void writeToGZip(byte b[], int off, int len) throws IOException 293 { 294 295 if (debug > 1) 296 { 297 System.out.println("writeToGZip, len = " + len); 298 } 299 if (debug > 2) 300 { 301 System.out.print("writeToGZip("); 302 System.out.write(b, off, len); 303 System.out.println(")"); 304 } 305 if (gzipstream == null) 306 { 307 if (debug > 1) 308 { 309 System.out.println("new GZIPOutputStream"); 310 } 311 response.addHeader("Content-Encoding", "gzip"); 312 gzipstream = new GZIPOutputStream (output); 313 } 314 gzipstream.write(b, off, len); 315 316 } 317 318 322 public boolean closed() 323 { 324 return (this.closed); 325 } 326 } | Popular Tags |