1 package com.blandware.atleap.webapp.filter; 2 3 import javax.servlet.ServletOutputStream ; 4 import javax.servlet.http.HttpServletResponse ; 5 import java.io.ByteArrayOutputStream ; 6 import java.io.IOException ; 7 import java.io.OutputStream ; 8 import java.util.zip.GZIPOutputStream ; 9 10 11 17 public class GZIPResponseStream extends ServletOutputStream { 18 protected OutputStream bufferedOutput = null; 20 21 protected boolean closed = false; 23 24 protected HttpServletResponse response = null; 26 27 protected ServletOutputStream output = null; 29 30 protected int bufferSize = 50000; 32 33 public GZIPResponseStream(HttpServletResponse response) 34 throws IOException { 35 super(); 36 closed = false; 37 this.response = response; 38 this.output = response.getOutputStream(); 39 bufferedOutput = new ByteArrayOutputStream (); 40 } 41 42 public void close() throws IOException { 43 if ( closed ) { 45 throw new IOException ("This output stream has already been closed"); 46 } 47 48 if ( bufferedOutput instanceof ByteArrayOutputStream ) { 50 ByteArrayOutputStream baos = (ByteArrayOutputStream ) bufferedOutput; 52 53 ByteArrayOutputStream compressedContent = 55 new ByteArrayOutputStream (); 56 GZIPOutputStream gzipstream = 57 new GZIPOutputStream (compressedContent); 58 byte[] bytes = baos.toByteArray(); 59 gzipstream.write(bytes); 60 gzipstream.finish(); 61 62 byte[] compressedBytes = compressedContent.toByteArray(); 64 65 response.setContentLength(compressedBytes.length); 67 response.addHeader("Content-Encoding", "gzip"); 68 output.write(compressedBytes); 69 output.flush(); 70 output.close(); 71 closed = true; 72 } 73 else if ( bufferedOutput instanceof GZIPOutputStream ) { 75 GZIPOutputStream gzipstream = (GZIPOutputStream ) bufferedOutput; 77 78 gzipstream.finish(); 80 81 output.flush(); 83 output.close(); 84 closed = true; 85 } 86 } 87 88 public void flush() throws IOException { 89 if ( closed ) { 90 throw new IOException ("Cannot flush a closed output stream"); 91 } 92 93 bufferedOutput.flush(); 94 } 95 96 public void write(int b) throws IOException { 97 if ( closed ) { 98 throw new IOException ("Cannot write to a closed output stream"); 99 } 100 101 checkBufferSize(1); 103 104 bufferedOutput.write((byte) b); 106 } 107 108 protected void checkBufferSize(int length) throws IOException { 109 if ( bufferedOutput instanceof ByteArrayOutputStream ) { 111 ByteArrayOutputStream baos = (ByteArrayOutputStream ) bufferedOutput; 112 113 if ( (baos.size() + length) > bufferSize ) { 114 response.addHeader("Content-Encoding", "gzip"); 116 117 byte[] bytes = baos.toByteArray(); 119 120 GZIPOutputStream gzipstream = new GZIPOutputStream (output); 122 gzipstream.write(bytes); 123 124 bufferedOutput = gzipstream; 126 } 127 } 128 } 129 130 public void write(byte[] b) throws IOException { 131 write(b, 0, b.length); 132 } 133 134 public void write(byte[] b, int off, int len) throws IOException { 135 136 if ( closed ) { 137 throw new IOException ("Cannot write to a closed output stream"); 138 } 139 140 checkBufferSize(len); 142 143 bufferedOutput.write(b, off, len); 145 } 146 147 public boolean closed() { 148 return (this.closed); 149 } 150 151 public void reset() { 152 } 154 } 155 | Popular Tags |