001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/servlet/GZIPResponseStream.java $
002 /*
003 This file is part of deegree.
004 Copyright (C) 2001-2008 by:
005 EXSE, Department of Geography, University of Bonn
006 http://www.giub.uni-bonn.de/deegree/
007 lat/lon GmbH
008 http://www.lat-lon.de
009
010 This library is free software; you can redistribute it and/or
011 modify it under the terms of the GNU Lesser General Public
012 License as published by the Free Software Foundation; either
013 version 2.1 of the License, or (at your option) any later version.
014
015 This library is distributed in the hope that it will be useful,
016 but WITHOUT ANY WARRANTY; without even the implied warranty of
017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 Lesser General Public License for more details.
019
020 You should have received a copy of the GNU Lesser General Public
021 License along with this library; if not, write to the Free Software
022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023
024 Contact:
025
026 Andreas Poth
027 lat/lon GmbH
028 Aennchenstr. 19
029 53115 Bonn
030 Germany
031 E-Mail: poth@lat-lon.de
032
033 Prof. Dr. Klaus Greve
034 Department of Geography
035 University of Bonn
036 Meckenheimer Allee 166
037 53115 Bonn
038 Germany
039 E-Mail: greve@giub.uni-bonn.de
040
041 Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
042 This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
043 http://www.jspbook.com.
044 */
045 package org.deegree.enterprise.servlet;
046
047 import java.io.ByteArrayOutputStream;
048 import java.io.IOException;
049 import java.util.zip.GZIPOutputStream;
050
051 import javax.servlet.ServletOutputStream;
052 import javax.servlet.http.HttpServletResponse;
053
054 /**
055 *
056 *
057 *
058 * @version $Revision: 9338 $
059 * @author <a href="mailto:jayson@jspinsider.com">Jayson Falkner</a>
060 * @author last edited by: $Author: apoth $
061 *
062 * @version 1.0. $Revision: 9338 $, $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $
063 *
064 * @since 2.0
065 */
066 public class GZIPResponseStream extends ServletOutputStream {
067 protected ByteArrayOutputStream baos = null;
068
069 protected GZIPOutputStream gzipstream = null;
070
071 protected boolean closed = false;
072
073 protected HttpServletResponse response = null;
074
075 protected ServletOutputStream output = null;
076
077 /**
078 *
079 * @param response
080 * @throws IOException
081 */
082 public GZIPResponseStream( HttpServletResponse response ) throws IOException {
083 super();
084 closed = false;
085 this.response = response;
086 this.output = response.getOutputStream();
087 baos = new ByteArrayOutputStream();
088 gzipstream = new GZIPOutputStream( baos );
089 }
090
091 /**
092 * @throws IOException
093 */
094 public void close() throws IOException {
095 if ( closed ) {
096 throw new IOException( "This output stream has already been closed" );
097 }
098 gzipstream.finish();
099
100 byte[] bytes = baos.toByteArray();
101
102 response.addHeader( "Content-Length", Integer.toString( bytes.length ) );
103 response.addHeader( "Content-Encoding", "gzip" );
104 output.write( bytes );
105 output.flush();
106 output.close();
107 closed = true;
108 }
109
110 /**
111 * @throws IOException
112 */
113 public void flush() throws IOException {
114 if ( closed ) {
115 throw new IOException( "Cannot flush a closed output stream" );
116 }
117 gzipstream.flush();
118 }
119
120 /**
121 * @param b
122 * data to write
123 * @throws IOException
124 */
125 public void write( int b ) throws IOException {
126 if ( closed ) {
127 throw new IOException( "Cannot write to a closed output stream" );
128 }
129 gzipstream.write( (byte) b );
130 }
131
132 /**
133 * @param b
134 * data array to write
135 * @throws IOException
136 */
137 public void write( byte b[] ) throws IOException {
138 write( b, 0, b.length );
139 }
140
141 /**
142 * @param b
143 * data array to write
144 * @param off
145 * index of the for byte
146 * @param len
147 * number of bytes to write
148 * @throws IOException
149 */
150 public void write( byte b[], int off, int len ) throws IOException {
151 System.out.println( "writing..." );
152 if ( closed ) {
153 throw new IOException( "Cannot write to a closed output stream" );
154 }
155 gzipstream.write( b, off, len );
156 }
157
158 /**
159 *
160 * @return true if already has been closed
161 */
162 public boolean closed() {
163 return ( this.closed );
164 }
165
166 /**
167 *
168 *
169 */
170 public void reset() {
171 // noop
172 }
173 }