1 24 package org.jresearch.gossip.filters.gzip; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.util.zip.GZIPOutputStream ; 30 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 public class GZIPResponseStream extends ServletOutputStream { 35 36 protected OutputStream bufferedOutput = null; 38 39 protected boolean closed = false; 41 42 protected HttpServletResponse response = null; 44 45 protected ServletOutputStream output = null; 47 48 private int bufferSize = 50000; 50 51 public GZIPResponseStream(HttpServletResponse response) throws IOException { 52 super(); 53 closed = false; 54 this.response = response; 55 this.output = response.getOutputStream(); 56 bufferedOutput = new ByteArrayOutputStream (); 57 } 58 59 public void close() throws IOException { 60 66 if (bufferedOutput instanceof ByteArrayOutputStream ) { 68 ByteArrayOutputStream baos = (ByteArrayOutputStream ) bufferedOutput; 70 ByteArrayOutputStream compressedContent = new ByteArrayOutputStream (); 72 GZIPOutputStream gzipstream = new GZIPOutputStream ( 73 compressedContent); 74 byte[] bytes = baos.toByteArray(); 75 gzipstream.write(bytes); 76 gzipstream.finish(); 77 byte[] compressedBytes = compressedContent.toByteArray(); 79 response.setContentLength(compressedBytes.length); 81 response.addHeader("Content-Encoding", "gzip"); 82 output.write(compressedBytes); 83 output.flush(); 84 output.close(); 85 closed = true; 86 } 87 else if (bufferedOutput instanceof GZIPOutputStream ) { 90 GZIPOutputStream gzipstream = (GZIPOutputStream ) bufferedOutput; 92 gzipstream.finish(); 94 output.flush(); 96 output.close(); 97 closed = true; 98 } 99 } 100 101 public void flush() throws IOException { 102 if (!closed) { 103 bufferedOutput.flush(); 104 } 105 } 106 107 public void write(int b) throws IOException { 108 if (closed) { 109 throw new IOException ("Cannot write to a closed output stream"); 110 } 111 checkBufferSize(1); 113 bufferedOutput.write((byte) b); 115 } 116 117 private void checkBufferSize(int length) throws IOException { 118 if (bufferedOutput instanceof ByteArrayOutputStream ) { 120 ByteArrayOutputStream baos = (ByteArrayOutputStream ) bufferedOutput; 121 if (baos.size() + length > bufferSize) { 122 response.addHeader("Content-Encoding", "gzip"); 125 byte[] bytes = baos.toByteArray(); 127 GZIPOutputStream gzipstream = new GZIPOutputStream (output); 129 gzipstream.write(bytes); 130 bufferedOutput = gzipstream; 132 } 133 } 134 } 135 136 public void write(byte b[]) throws IOException { 137 write(b, 0, b.length); 138 } 139 140 public void write(byte b[], int off, int len) throws IOException { 141 if (closed) { 142 throw new IOException ("Cannot write to a closed output stream"); 143 } 144 checkBufferSize(len); 146 bufferedOutput.write(b, off, len); 148 } 149 150 public boolean closed() { 151 return (this.closed); 152 } 153 154 public void reset() { 155 } 157 } | Popular Tags |