1 24 package org.jresearch.gossip.filters.gzip; 25 26 import java.io.IOException ; 27 import java.io.OutputStreamWriter ; 28 import java.io.PrintWriter ; 29 30 import javax.servlet.ServletOutputStream ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpServletResponseWrapper ; 33 34 public class GZIPResponseWrapper extends HttpServletResponseWrapper { 35 36 protected HttpServletResponse origResponse = null; 37 38 protected ServletOutputStream stream = null; 39 40 protected PrintWriter writer = null; 41 42 public GZIPResponseWrapper(HttpServletResponse response) { 43 super(response); 44 origResponse = response; 45 } 46 47 public ServletOutputStream createOutputStream() throws IOException { 48 return (new GZIPResponseStream(origResponse)); 49 } 50 51 public void finishResponse() { 52 try { 53 if (writer != null) { 54 writer.close(); 55 } else { 56 if (stream != null) { 57 stream.close(); 58 } 59 } 60 } catch (IOException e) { 61 } 62 } 63 64 public void flushBuffer() throws IOException { 65 if (stream != null) { 66 stream.flush(); 67 } 68 } 69 70 public ServletOutputStream getOutputStream() throws IOException { 71 if (writer != null) { 72 throw new IllegalStateException ( 73 "getWriter() has already been called!"); 74 } 75 76 if (stream == null) 77 stream = createOutputStream(); 78 return (stream); 79 } 80 81 public PrintWriter getWriter() throws IOException { 82 if (writer != null) { 83 return (writer); 84 } 85 86 if (stream != null) { 87 throw new IllegalStateException ( 88 "getOutputStream() has already been called!"); 89 } 90 91 stream = createOutputStream(); 92 writer = new PrintWriter (new OutputStreamWriter (stream, origResponse 94 .getCharacterEncoding())); 95 return (writer); 96 } 97 98 public void setContentLength(int length) { 99 } 100 } | Popular Tags |