//# 0 errors, 94 messages
//#
/*
    //#gzipresponsestream.java:1:1: class: net.sourceforge.pebble.web.filter.GZIPResponseStream
    //#gzipresponsestream.java:1:1: method: net.sourceforge.pebble.web.filter.GZIPResponseStream.net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init
 * Copyright (c) 2003-2006, Simon Brown
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *
 *   - Neither the name of Pebble nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package net.sourceforge.pebble.web.filter;

import java.io.*;
import java.util.zip.GZIPOutputStream;
import javax.servlet.*;
import javax.servlet.http.*;

public class GZIPResponseStream extends ServletOutputStream {

  protected ByteArrayOutputStream baos = null;
  protected GZIPOutputStream gzipstream = null;
  protected boolean closed = false;
  protected HttpServletResponse response = null;
  protected ServletOutputStream output = null;

  public GZIPResponseStream(HttpServletResponse response) throws IOException {
    //#gzipresponsestream.java:47: method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)
    //#input(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): response
    //#input(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this
    //#output(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): new ByteArrayOutputStream(GZIPResponseStream#1) num objects
    //#output(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): new GZIPOutputStream(GZIPResponseStream#2) num objects
    //#output(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.baos
    //#output(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.closed
    //#output(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.gzipstream
    //#output(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.output
    //#output(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.response
    //#new obj(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): new ByteArrayOutputStream(GZIPResponseStream#1)
    //#new obj(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): new GZIPOutputStream(GZIPResponseStream#2)
    //#pre[1] (void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): response != null
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.baos == &new ByteArrayOutputStream(GZIPResponseStream#1)
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.closed == 0
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.gzipstream == &new GZIPOutputStream(GZIPResponseStream#2)
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): init'ed(this.output)
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.response == response
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): this.response != null
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): new ByteArrayOutputStream(GZIPResponseStream#1) num objects == 1
    //#post(void net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)): new GZIPOutputStream(GZIPResponseStream#2) num objects == 1
    closed = false;
    this.response = response;
    this.output = response.getOutputStream();
    baos = new ByteArrayOutputStream();
    gzipstream = new GZIPOutputStream(baos);
  }
    //#gzipresponsestream.java:53: end of method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.net.sourceforge.pebble.web.filter.GZIPResponseStream(HttpServletResponse)

  public void close() throws IOException {
    if (closed) {
      throw new IOException("This output stream has already been closed");
    }
    gzipstream.finish();
    gzipstream.flush();
    gzipstream.close();

    byte[] bytes = baos.toByteArray();
    response.setContentLength(bytes.length);
    response.addHeader("Content-Encoding", "gzip");
    output.write(bytes);
    output.flush();
    output.close();
    closed = true;
  }

  public void flush() throws IOException {
    if (closed) {
    //#gzipresponsestream.java:73: method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.flush()
    //#input(void flush()): this
    //#input(void flush()): this.closed
    //#input(void flush()): this.gzipstream
    //#pre[2] (void flush()): this.closed == 0
    //#pre[3] (void flush()): this.gzipstream != null
      throw new IOException("Cannot flush a closed output stream");
    }
    gzipstream.flush();
  }
    //#gzipresponsestream.java:77: end of method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.flush()

  public void write(int b) throws IOException {
    if (closed) {
    //#gzipresponsestream.java:80: method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.write(int)
    //#input(void write(int)): b
    //#input(void write(int)): this
    //#input(void write(int)): this.closed
    //#input(void write(int)): this.gzipstream
    //#pre[4] (void write(int)): this.closed == 0
    //#pre[5] (void write(int)): this.gzipstream != null
      throw new IOException("Cannot write to a closed output stream");
    }
    gzipstream.write((byte) b);
  }
    //#gzipresponsestream.java:84: end of method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.write(int)

  public void write(byte b[]) throws IOException {
    write(b, 0, b.length);
    //#gzipresponsestream.java:87: method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.write(byte[])
    //#input(void write(byte[])): __Descendant_Table[net/sourceforge/pebble/web/filter/GZIPResponseStream]
    //#input(void write(byte[])): __Descendant_Table[others]
    //#input(void write(byte[])): __Dispatch_Table.write([BII)V
    //#input(void write(byte[])): b
    //#input(void write(byte[])): b.length
    //#input(void write(byte[])): this
    //#input(void write(byte[])): this.__Tag
    //#input(void write(byte[])): this.closed
    //#input(void write(byte[])): this.gzipstream
    //#pre[1] (void write(byte[])): b != null
    //#pre[2] (void write(byte[])): b.length <= 4_294_967_295
    //#pre[4] (void write(byte[])): this.__Tag == net/sourceforge/pebble/web/filter/GZIPResponseStream
    //#pre[5] (void write(byte[])): this.closed == 0
    //#pre[6] (void write(byte[])): this.gzipstream != null
    //#unanalyzed(void write(byte[])): Effects-of-calling:java.util.zip.GZIPOutputStream:write
  }
    //#gzipresponsestream.java:88: end of method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.write(byte[])

  public void write(byte b[], int off, int len) throws IOException {
    if (closed) {
    //#gzipresponsestream.java:91: method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.write(byte[], int, int)
    //#input(void write(byte[], int, int)): b
    //#input(void write(byte[], int, int)): len
    //#input(void write(byte[], int, int)): off
    //#input(void write(byte[], int, int)): this
    //#input(void write(byte[], int, int)): this.closed
    //#input(void write(byte[], int, int)): this.gzipstream
    //#pre[5] (void write(byte[], int, int)): this.closed == 0
    //#pre[6] (void write(byte[], int, int)): this.gzipstream != null
      throw new IOException("Cannot write to a closed output stream");
    }
    gzipstream.write(b, off, len);
  }
    //#gzipresponsestream.java:95: end of method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.write(byte[], int, int)

  public boolean closed() {
    return (this.closed);
    //#gzipresponsestream.java:98: method: bool net.sourceforge.pebble.web.filter.GZIPResponseStream.closed()
    //#input(bool closed()): this
    //#input(bool closed()): this.closed
    //#output(bool closed()): return_value
    //#pre[2] (bool closed()): init'ed(this.closed)
    //#post(bool closed()): return_value == this.closed
    //#post(bool closed()): init'ed(return_value)
    //#gzipresponsestream.java:98: end of method: bool net.sourceforge.pebble.web.filter.GZIPResponseStream.closed()
  }

  public void reset() {
  }
    //#gzipresponsestream.java:102: method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.reset()
    //#gzipresponsestream.java:102: end of method: void net.sourceforge.pebble.web.filter.GZIPResponseStream.reset()

}    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Descendant_Table[net/sourceforge/pebble/web/filter/GZIPResponseStream]
    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.close()V
    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.closed()Z
    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.flush()V
    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.reset()V
    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.write(I)V
    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.write([B)V
    //#output(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.write([BII)V
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Descendant_Table[net/sourceforge/pebble/web/filter/GZIPResponseStream] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.close()V == &close
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.closed()Z == &closed
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.flush()V == &flush
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.reset()V == &reset
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.write(I)V == &write
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.write([B)V == &write
    //#post(net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init): __Dispatch_Table.write([BII)V == &write
    //#gzipresponsestream.java:: end of method: net.sourceforge.pebble.web.filter.GZIPResponseStream.net.sourceforge.pebble.web.filter.GZIPResponseStream__static_init
    //#gzipresponsestream.java:: end of class: net.sourceforge.pebble.web.filter.GZIPResponseStream
