001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 04:31:31 -0800 (Thu, 27 Dec 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)
083: throws IOException {
084: super ();
085: closed = false;
086: this .response = response;
087: this .output = response.getOutputStream();
088: baos = new ByteArrayOutputStream();
089: gzipstream = new GZIPOutputStream(baos);
090: }
091:
092: /**
093: * @throws IOException
094: */
095: public void close() throws IOException {
096: if (closed) {
097: throw new IOException(
098: "This output stream has already been closed");
099: }
100: gzipstream.finish();
101:
102: byte[] bytes = baos.toByteArray();
103:
104: response.addHeader("Content-Length", Integer
105: .toString(bytes.length));
106: response.addHeader("Content-Encoding", "gzip");
107: output.write(bytes);
108: output.flush();
109: output.close();
110: closed = true;
111: }
112:
113: /**
114: * @throws IOException
115: */
116: public void flush() throws IOException {
117: if (closed) {
118: throw new IOException("Cannot flush a closed output stream");
119: }
120: gzipstream.flush();
121: }
122:
123: /**
124: * @param b
125: * data to write
126: * @throws IOException
127: */
128: public void write(int b) throws IOException {
129: if (closed) {
130: throw new IOException(
131: "Cannot write to a closed output stream");
132: }
133: gzipstream.write((byte) b);
134: }
135:
136: /**
137: * @param b
138: * data array to write
139: * @throws IOException
140: */
141: public void write(byte b[]) throws IOException {
142: write(b, 0, b.length);
143: }
144:
145: /**
146: * @param b
147: * data array to write
148: * @param off
149: * index of the for byte
150: * @param len
151: * number of bytes to write
152: * @throws IOException
153: */
154: public void write(byte b[], int off, int len) throws IOException {
155: System.out.println("writing...");
156: if (closed) {
157: throw new IOException(
158: "Cannot write to a closed output stream");
159: }
160: gzipstream.write(b, off, len);
161: }
162:
163: /**
164: *
165: * @return true if already has been closed
166: */
167: public boolean closed() {
168: return (this .closed);
169: }
170:
171: /**
172: *
173: *
174: */
175: public void reset() {
176: // noop
177: }
178: }
|