/* * Copyright 2003 Jayson Falkner (jayson@jspinsider.com) * This code is from "Servlets and JavaServer pages; the J2EE Web Tier", * http://www.jspbook.com. You may freely use the code both commercially * and non-commercially. If you like the code, please pick up a copy of * the book and help support the authors, development of more free code, * and the JSP/Servlet/J2EE community. * * Modified by David Winslow <dwinslow@openplans.org> */ package org.geoserver.filters; import java.io.IOException; import java.io.ByteArrayOutputStream; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; public class GZIPResponseStream extends ServletOutputStream { protected GZIPOutputStream gzipstream = null; protected boolean closed = false; protected HttpServletResponse response = null; protected ServletOutputStream output = null; public GZIPResponseStream(HttpServletResponse response) throws IOException { super(); closed = false; this.response = response; response.addHeader("Content-Encoding", "gzip"); this.output = response.getOutputStream(); // create a 4kb buffered gzip output stream gzipstream = new GZIPOutputStream(output, 4 * 1024); } public void close() throws IOException { if (closed) { throw new IOException("This output stream has already been closed"); } gzipstream.finish(); output.flush(); output.close(); closed = true; } public void flush() throws IOException { if (closed) { throw new IOException("Cannot flush a closed output stream"); } gzipstream.flush(); } public void write(int b) throws IOException { if (closed) { throw new IOException("Cannot write to a closed output stream"); } gzipstream.write((byte)b); } public void write(byte b[]) throws IOException { if (closed) { throw new IOException("Cannot write to a closed output stream"); } write(b, 0, b.length); } public void write(byte b[], int off, int len) throws IOException { if (closed) { throw new IOException("Cannot write to a closed output stream"); } gzipstream.write(b, off, len); } public boolean closed() { return (this.closed); } public void reset() { //noop } }