1 9 package com.sslexplorer.core.filters; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.util.zip.GZIPOutputStream ; 14 15 import javax.servlet.ServletOutputStream ; 16 import javax.servlet.http.HttpServletResponse ; 17 18 public class GZIPResponseStream extends ServletOutputStream { 19 protected ByteArrayOutputStream baos = null; 20 protected GZIPOutputStream gzipstream = null; 21 protected boolean closed = false; 22 protected HttpServletResponse response = null; 23 protected ServletOutputStream output = null; 24 25 public GZIPResponseStream(HttpServletResponse response) throws IOException { 26 super(); 27 closed = false; 28 this.response = response; 29 this.output = response.getOutputStream(); 30 baos = new ByteArrayOutputStream (); 31 gzipstream = new GZIPOutputStream (baos); 32 } 33 34 public void close() throws IOException { 35 if (closed) { 36 throw new IOException ("This output stream has already been closed"); 37 } 38 gzipstream.finish(); 39 40 byte[] bytes = baos.toByteArray(); 41 42 43 response.setHeader("Content-Length", 44 Integer.toString(bytes.length)); 45 response.setHeader("Content-Encoding", "gzip"); 46 output.write(bytes); 47 output.flush(); 48 output.close(); 49 closed = true; 50 } 51 52 public void flush() throws IOException { 53 if (closed) { 54 throw new IOException ("Cannot flush a closed output stream"); 55 } 56 gzipstream.flush(); 57 } 58 59 public void write(int b) throws IOException { 60 if (closed) { 61 throw new IOException ("Cannot write to a closed output stream"); 62 } 63 gzipstream.write((byte)b); 64 } 65 66 public void write(byte b[]) throws IOException { 67 write(b, 0, b.length); 68 } 69 70 public void write(byte b[], int off, int len) throws IOException { 71 if (closed) { 72 throw new IOException ("Cannot write to a closed output stream"); 73 } 74 gzipstream.write(b, off, len); 75 } 76 77 public boolean closed() { 78 return (this.closed); 79 } 80 81 public void reset() { 82 } 84 } 85 | Popular Tags |