home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / CompressionResponseStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-05-17  |  4.1 KB  |  173 lines

  1. package compressionFilters;
  2.  
  3. import java.io.IOException;
  4. import java.util.zip.GZIPOutputStream;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. public class CompressionResponseStream extends ServletOutputStream {
  9.    protected int compressionThreshold = 0;
  10.    private int debug = 0;
  11.    protected byte[] buffer = null;
  12.    protected int bufferCount = 0;
  13.    protected GZIPOutputStream gzipstream = null;
  14.    protected boolean closed = false;
  15.    protected int length = -1;
  16.    protected HttpServletResponse response = null;
  17.    protected ServletOutputStream output = null;
  18.  
  19.    public CompressionResponseStream(HttpServletResponse response) throws IOException {
  20.       this.closed = false;
  21.       this.response = response;
  22.       this.output = response.getOutputStream();
  23.    }
  24.  
  25.    public void setDebugLevel(int debug) {
  26.       this.debug = debug;
  27.    }
  28.  
  29.    protected void setBuffer(int threshold) {
  30.       this.compressionThreshold = threshold;
  31.       this.buffer = new byte[this.compressionThreshold];
  32.       if (this.debug > 1) {
  33.          System.out.println("buffer is set to " + this.compressionThreshold);
  34.       }
  35.  
  36.    }
  37.  
  38.    public void close() throws IOException {
  39.       if (this.debug > 1) {
  40.          System.out.println("close() @ CompressionResponseStream");
  41.       }
  42.  
  43.       if (this.closed) {
  44.          throw new IOException("This output stream has already been closed");
  45.       } else {
  46.          if (this.gzipstream != null) {
  47.             this.flushToGZip();
  48.             this.gzipstream.close();
  49.             this.gzipstream = null;
  50.          } else if (this.bufferCount > 0) {
  51.             if (this.debug > 2) {
  52.                System.out.print("output.write(");
  53.                System.out.write(this.buffer, 0, this.bufferCount);
  54.                System.out.println(")");
  55.             }
  56.  
  57.             this.output.write(this.buffer, 0, this.bufferCount);
  58.             this.bufferCount = 0;
  59.          }
  60.  
  61.          this.output.close();
  62.          this.closed = true;
  63.       }
  64.    }
  65.  
  66.    public void flush() throws IOException {
  67.       if (this.debug > 1) {
  68.          System.out.println("flush() @ CompressionResponseStream");
  69.       }
  70.  
  71.       if (this.closed) {
  72.          throw new IOException("Cannot flush a closed output stream");
  73.       } else {
  74.          if (this.gzipstream != null) {
  75.             this.gzipstream.flush();
  76.          }
  77.  
  78.       }
  79.    }
  80.  
  81.    public void flushToGZip() throws IOException {
  82.       if (this.debug > 1) {
  83.          System.out.println("flushToGZip() @ CompressionResponseStream");
  84.       }
  85.  
  86.       if (this.bufferCount > 0) {
  87.          if (this.debug > 1) {
  88.             System.out.println("flushing out to GZipStream, bufferCount = " + this.bufferCount);
  89.          }
  90.  
  91.          this.writeToGZip(this.buffer, 0, this.bufferCount);
  92.          this.bufferCount = 0;
  93.       }
  94.  
  95.    }
  96.  
  97.    public void write(int b) throws IOException {
  98.       if (this.debug > 1) {
  99.          System.out.println("write " + b + " in CompressionResponseStream ");
  100.       }
  101.  
  102.       if (this.closed) {
  103.          throw new IOException("Cannot write to a closed output stream");
  104.       } else {
  105.          if (this.bufferCount >= this.buffer.length) {
  106.             this.flushToGZip();
  107.          }
  108.  
  109.          this.buffer[this.bufferCount++] = (byte)b;
  110.       }
  111.    }
  112.  
  113.    public void write(byte[] b) throws IOException {
  114.       this.write(b, 0, b.length);
  115.    }
  116.  
  117.    public void write(byte[] b, int off, int len) throws IOException {
  118.       if (this.debug > 1) {
  119.          System.out.println("write, bufferCount = " + this.bufferCount + " len = " + len + " off = " + off);
  120.       }
  121.  
  122.       if (this.debug > 2) {
  123.          System.out.print("write(");
  124.          System.out.write(b, off, len);
  125.          System.out.println(")");
  126.       }
  127.  
  128.       if (this.closed) {
  129.          throw new IOException("Cannot write to a closed output stream");
  130.       } else if (len != 0) {
  131.          if (len <= this.buffer.length - this.bufferCount) {
  132.             System.arraycopy(b, off, this.buffer, this.bufferCount, len);
  133.             this.bufferCount += len;
  134.          } else {
  135.             this.flushToGZip();
  136.             if (len <= this.buffer.length - this.bufferCount) {
  137.                System.arraycopy(b, off, this.buffer, this.bufferCount, len);
  138.                this.bufferCount += len;
  139.             } else {
  140.                this.writeToGZip(b, off, len);
  141.             }
  142.          }
  143.       }
  144.    }
  145.  
  146.    public void writeToGZip(byte[] b, int off, int len) throws IOException {
  147.       if (this.debug > 1) {
  148.          System.out.println("writeToGZip, len = " + len);
  149.       }
  150.  
  151.       if (this.debug > 2) {
  152.          System.out.print("writeToGZip(");
  153.          System.out.write(b, off, len);
  154.          System.out.println(")");
  155.       }
  156.  
  157.       if (this.gzipstream == null) {
  158.          if (this.debug > 1) {
  159.             System.out.println("new GZIPOutputStream");
  160.          }
  161.  
  162.          this.response.addHeader("Content-Encoding", "gzip");
  163.          this.gzipstream = new GZIPOutputStream(this.output);
  164.       }
  165.  
  166.       this.gzipstream.write(b, off, len);
  167.    }
  168.  
  169.    public boolean closed() {
  170.       return this.closed;
  171.    }
  172. }
  173.