KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > filter > GZIPResponseStream


1 /*
2  * Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
3  * This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
4  * http://www.jspbook.com. You may freely use the code both commercially
5  * and non-commercially. If you like the code, please pick up a copy of
6  * the book and help support the authors, development of more free code,
7  * and the JSP/Servlet/J2EE community.
8  */

9 package org.snipsnap.net.filter;
10
11 import javax.servlet.ServletOutputStream JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.zip.GZIPOutputStream JavaDoc;
16
17 public class GZIPResponseStream extends ServletOutputStream JavaDoc {
18   protected ByteArrayOutputStream JavaDoc baos = null;
19   protected GZIPOutputStream JavaDoc gzipstream = null;
20   protected boolean closed = false;
21   protected HttpServletResponse JavaDoc response = null;
22   protected ServletOutputStream JavaDoc output = null;
23
24   public GZIPResponseStream(HttpServletResponse JavaDoc response) throws IOException JavaDoc {
25     super();
26     closed = false;
27     this.response = response;
28     this.output = response.getOutputStream();
29     baos = new ByteArrayOutputStream JavaDoc();
30     gzipstream = new GZIPOutputStream JavaDoc(baos);
31   }
32
33   public void close() throws IOException JavaDoc {
34     if (closed) {
35 // throw new IOException("This output stream has already been closed");
36
return;
37     }
38     gzipstream.finish();
39
40     byte[] bytes = baos.toByteArray();
41
42     // only actually tell the remote end we are compressed if there is any data
43
if (bytes.length > 0) {
44       response.setHeader("Content-Length",
45                          Integer.toString(bytes.length));
46       response.setHeader("Content-Encoding", "gzip");
47       output.write(bytes);
48     }
49
50     output.flush();
51     output.close();
52     closed = true;
53   }
54
55   public void flush() throws IOException JavaDoc {
56     if (!closed) {
57       gzipstream.flush();
58     }
59   }
60
61   public void write(int b) throws IOException JavaDoc {
62     if (closed) {
63       throw new IOException JavaDoc("Cannot write to a closed output stream");
64     }
65     gzipstream.write((byte) b);
66   }
67
68   public void write(byte b[]) throws IOException JavaDoc {
69     write(b, 0, b.length);
70   }
71
72   public void write(byte b[], int off, int len) throws IOException JavaDoc {
73 // System.out.println("writing..."+len);
74
if (closed) {
75       throw new IOException JavaDoc("Cannot write to a closed output stream");
76     }
77     gzipstream.write(b, off, len);
78   }
79
80   public boolean closed() {
81     return (this.closed);
82   }
83
84   public void reset() {
85     //noop
86
}
87 }
88
Popular Tags