    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/enterprise/servlet/GZIPResponseStream.java $
    002    /*----------------------------------------------------------------------------
    003     This file originated as work from Jayson Falkner.
    004
    005     Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
    006     This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
    007     http://www.jspbook.com. You may freely use the code both commercially
    008     and non-commercially. If you like the code, please pick up a copy of
    009     the book and help support the authors, development of more free code,
    010     and the JSP/Servlet/J2EE community.
    011     ----------------------------------------------------------------------------*/
    012    package org.deegree.enterprise.servlet;
    013
    014    import java.io.ByteArrayOutputStream;
    015    import java.io.IOException;
    016    import java.util.zip.GZIPOutputStream;
    017
    018    import javax.servlet.ServletOutputStream;
    019    import javax.servlet.http.HttpServletResponse;
    020
    021    /**
    022     *
    023     *
    024     * @author <a href="mailto:jayson@jspinsider.com">Jayson Falkner</a>
    025     * @author last edited by: $Author: mschneider $
    026     *
    027     * @version $Revision: 20326 $, $Date: 2009-10-22 18:41:56 +0200 (Do, 22. Okt 2009) $
    028     */
    029    public class GZIPResponseStream extends ServletOutputStream {
    030        /**
    031         * the output as a byte array
    032         */
    033        protected ByteArrayOutputStream baos = null;
    034
    035        /**
    036         * the output as a gzipped stream
    037         */
    038        protected GZIPOutputStream gzipstream = null;
    039
    040        /**
    041         * true if the stream is closed
    042         */
    043        protected boolean closed = false;
    044
    045        /**
    046         * the response wrapper
    047         */
    048        protected HttpServletResponse response = null;
    049
    050        /**
    051         * the output as a stream
    052         */
    053        protected ServletOutputStream output = null;
    054
    055        /**
    056         *
    057         * @param response
    058         * @throws IOException
    059         */
    060        public GZIPResponseStream( HttpServletResponse response ) throws IOException {
    061            super();
    062            closed = false;
    063            this.response = response;
    064            this.output = response.getOutputStream();
    065            baos = new ByteArrayOutputStream();
    066            gzipstream = new GZIPOutputStream( baos );
    067        }
    068
    069        /**
    070         * @throws IOException
    071         */
    072        @Override
    073        public void close()
    074                                throws IOException {
    075            if ( closed ) {
    076                throw new IOException( "This output stream has already been closed" );
    077            }
    078            gzipstream.finish();
    079
    080            byte[] bytes = baos.toByteArray();
    081
    082            response.addHeader( "Content-Length", Integer.toString( bytes.length ) );
    083            response.addHeader( "Content-Encoding", "gzip" );
    084            output.write( bytes );
    085            output.flush();
    086            output.close();
    087            closed = true;
    088        }
    089
    090        /**
    091         * @throws IOException
    092         */
    093        @Override
    094        public void flush()
    095                                throws IOException {
    096            if ( closed ) {
    097                throw new IOException( "Cannot flush a closed output stream" );
    098            }
    099            gzipstream.flush();
    100        }
    101
    102        /**
    103         * @param b
    104         *            data to write
    105         * @throws IOException
    106         */
    107        @Override
    108        public void write( int b )
    109                                throws IOException {
    110            if ( closed ) {
    111                throw new IOException( "Cannot write to a closed output stream" );
    112            }
    113            gzipstream.write( (byte) b );
    114        }
    115
    116        /**
    117         * @param b
    118         *            data array to write
    119         * @throws IOException
    120         */
    121        @Override
    122        public void write( byte b[] )
    123                                throws IOException {
    124            write( b, 0, b.length );
    125        }
    126
    127        /**
    128         * @param b
    129         *            data array to write
    130         * @param off
    131         *            index of the for byte
    132         * @param len
    133         *            number of bytes to write
    134         * @throws IOException
    135         */
    136        @Override
    137        public void write( byte b[], int off, int len )
    138                                throws IOException {
    139            System.out.println( "writing..." );
    140            if ( closed ) {
    141                throw new IOException( "Cannot write to a closed output stream" );
    142            }
    143            gzipstream.write( b, off, len );
    144        }
    145
    146        /**
    147         *
    148         * @return true if already has been closed
    149         */
    150        public boolean closed() {
    151            return ( this.closed );
    152        }
    153
    154        /**
    155         *
    156         *
    157         */
    158        public void reset() {
    159            // noop
    160        }
    161    }