1 package com.openedit.servlet.gzip; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 import java.io.OutputStream ; 6 import java.util.zip.GZIPOutputStream ; 7 8 import javax.servlet.ServletOutputStream ; 9 import javax.servlet.http.HttpServletResponse ; 10 11 public class GzipResponseStream extends ServletOutputStream { 12 protected OutputStream bufferedOutput = null; 14 protected boolean closed = false; 16 protected HttpServletResponse response = null; 18 protected ServletOutputStream output = null; 20 private int bufferSize = 50000; 22 23 public GzipResponseStream(HttpServletResponse response) throws IOException { 24 super(); 25 closed = false; 26 this.response = response; 27 this.output = response.getOutputStream(); 28 bufferedOutput = new ByteArrayOutputStream (); 29 } 30 31 public void close() throws IOException { 32 40 if (bufferedOutput instanceof ByteArrayOutputStream ) { 42 ByteArrayOutputStream baos = (ByteArrayOutputStream )bufferedOutput; 44 ByteArrayOutputStream compressedContent = new ByteArrayOutputStream (); 46 GZIPOutputStream gzipstream = new GZIPOutputStream (compressedContent); 47 byte[] bytes = baos.toByteArray(); 48 gzipstream.write(bytes); 49 gzipstream.finish(); 50 byte[] compressedBytes = compressedContent.toByteArray(); 52 response.setContentLength(compressedBytes.length); 54 response.addHeader("Content-Encoding", "gzip"); 55 output.write(compressedBytes); 56 output.flush(); 57 output.close(); 58 closed = true; 59 } 60 else if (bufferedOutput instanceof GZIPOutputStream ) { 62 GZIPOutputStream gzipstream = (GZIPOutputStream )bufferedOutput; 64 gzipstream.finish(); 66 output.flush(); 68 output.close(); 69 closed = true; 70 } 71 } 72 73 public void flush() throws IOException { 74 if (closed) { 75 throw new IOException ("Cannot flush a closed output stream"); 76 } 77 bufferedOutput.flush(); 78 } 79 80 public void write(int b) throws IOException { 81 if (closed) { 82 throw new IOException ("Cannot write to a closed output stream"); 83 } 84 checkBufferSize(1); 86 bufferedOutput.write((byte)b); 88 } 89 90 private void checkBufferSize(int length) throws IOException { 91 if (bufferedOutput instanceof ByteArrayOutputStream ) { 93 ByteArrayOutputStream baos = (ByteArrayOutputStream )bufferedOutput; 94 if (baos.size()+length > bufferSize) { 95 response.addHeader("Content-Encoding", "gzip"); 97 byte[] bytes = baos.toByteArray(); 99 GZIPOutputStream gzipstream = new GZIPOutputStream (output); 101 gzipstream.write(bytes); 102 bufferedOutput = gzipstream; 104 } 105 } 106 } 107 108 public void write(byte b[]) throws IOException { 109 write(b, 0, b.length); 110 } 111 112 public void write(byte b[], int off, int len) throws IOException { 113 if (closed) { 115 throw new IOException ("Cannot write to a closed output stream"); 116 } 117 checkBufferSize(len); 119 bufferedOutput.write(b, off, len); 121 } 122 123 public boolean closed() { 124 return (this.closed); 125 } 126 127 public void reset() { 128 } 130 } 131 | Popular Tags |