1 64 65 package net.myvietnam.mvncore.servlet.filter; 66 67 import java.io.IOException ; 68 import java.util.zip.GZIPOutputStream ; 69 import javax.servlet.ServletOutputStream ; 70 import javax.servlet.http.HttpServletResponse ; 71 72 import org.apache.commons.logging.Log; 73 import org.apache.commons.logging.LogFactory; 74 75 76 84 85 public class CompressionResponseStream extends ServletOutputStream { 86 87 private static Log log = LogFactory.getLog(CompressionResponseStream.class); 89 90 92 93 98 public CompressionResponseStream(HttpServletResponse response) throws IOException { 99 100 super(); 101 closed = false; 102 this.response = response; 103 this.output = response.getOutputStream(); 104 105 } 106 107 108 110 111 115 protected int compressionThreshold = 0; 116 117 120 122 125 protected byte[] buffer = null; 126 127 130 protected int bufferCount = 0; 131 132 135 protected GZIPOutputStream gzipstream = null; 136 137 140 protected boolean closed = false; 141 142 146 protected int length = -1; 147 148 151 protected HttpServletResponse response = null; 152 153 156 protected ServletOutputStream output = null; 157 158 159 161 164 168 169 172 protected void setBuffer(int threshold) { 173 compressionThreshold = threshold; 174 buffer = new byte[compressionThreshold]; 175 } 177 178 182 public void close() throws IOException { 183 184 if (closed) { 186 throw new IOException ("This output stream has already been closed"); 189 } 191 192 if (gzipstream != null) { 193 flushToGZip(); 194 gzipstream.close(); 195 gzipstream = null; 196 } else { 197 if (bufferCount > 0) { 198 205 output.write(buffer, 0, bufferCount); 206 bufferCount = 0; 207 } 208 } 209 210 output.close(); 211 closed = true; 212 213 } 214 215 216 220 public void flush() throws IOException { 221 222 if (closed) { 224 log.warn("Cannot flush a closed output stream"); 226 } 227 228 if (gzipstream != null) { 229 gzipstream.flush(); 230 } 231 232 } 233 234 public void flushToGZip() throws IOException { 235 236 if (bufferCount > 0) { 238 writeToGZip(buffer, 0, bufferCount); 240 bufferCount = 0; 241 } 242 243 } 244 245 252 public void write(int b) throws IOException { 253 254 if (closed) 256 throw new IOException ("Cannot write to a closed output stream"); 257 258 if (bufferCount >= buffer.length) { 259 flushToGZip(); 260 } 261 262 buffer[bufferCount++] = (byte) b; 263 264 } 265 266 267 275 public void write(byte b[]) throws IOException { 276 277 write(b, 0, b.length); 278 279 } 280 281 282 292 public void write(byte b[], int off, int len) throws IOException { 293 294 301 302 if (closed) 303 throw new IOException ("Cannot write to a closed output stream"); 304 305 if (len == 0) 306 return; 307 308 if (len <= (buffer.length - bufferCount)) { 310 System.arraycopy(b, off, buffer, bufferCount, len); 311 bufferCount += len; 312 return; 313 } 314 315 flushToGZip(); 317 318 if (len <= (buffer.length - bufferCount)) { 320 System.arraycopy(b, off, buffer, bufferCount, len); 321 bufferCount += len; 322 return; 323 } 324 325 writeToGZip(b, off, len); 327 } 328 329 public void writeToGZip(byte b[], int off, int len) throws IOException { 330 331 338 if (gzipstream == null) { 339 response.addHeader("Content-Encoding", "gzip"); 341 gzipstream = new GZIPOutputStream (output); 342 } 343 gzipstream.write(b, off, len); 344 345 } 346 347 348 350 351 354 public boolean closed() { 355 356 return (this.closed); 357 358 } 359 360 } 361 | Popular Tags |