1 package com.atlassian.gzipfilter;
2
3 import java.io.IOException;
4 import java.util.zip.GZIPOutputStream;
5
6 import javax.servlet.ServletOutputStream;
7 import javax.servlet.http.HttpServletResponse;
8
9 public final class GzipResponseStream extends ServletOutputStream
10 {
11 protected final GZIPOutputStream gzipstream;
12 protected final HttpServletResponse response;
13
14 private final static int DEFAULT_BUFFER_SIZE_BYTES = 1024;
15
16 protected boolean hasWrittenData = false;
17
18 public GzipResponseStream(HttpServletResponse response) throws IOException
19 {
20 super();
21 this.response = response;
22 gzipstream = new GZIPOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE_BYTES);
23 }
24
25 public void close() throws IOException
26 {
27 gzipstream.close();
28 }
29
30 public void flush() throws IOException
31 {
32 gzipstream.flush();
33 }
34
35 public void write(int b) throws IOException
36 {
37 markUsed();
38 gzipstream.write(b);
39 }
40
41 public void write(byte b[]) throws IOException
42 {
43 this.write(b, 0, b.length);
44 }
45
46 public void write(byte b[], int off, int len) throws IOException
47 {
48 markUsed();
49 gzipstream.write(b, off, len);
50 }
51
52 private void markUsed()
53 {
54 hasWrittenData = true;
55 }
56 }