-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGZIPResponseStream.java
69 lines (60 loc) · 2.23 KB
/
GZIPResponseStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package testHttp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
public class GZIPResponseStream extends ServletOutputStream {
protected ByteArrayOutputStream baos = null;
protected GZIPOutputStream gzipstream = null;
protected boolean closed = false;
protected HttpServletResponse response = null;
protected ServletOutputStream output = null;
public GZIPResponseStream(HttpServletResponse httpservletresponse) throws IOException {
this.closed = false;
this.response = httpservletresponse;
this.output = httpservletresponse.getOutputStream();
this.baos = new ByteArrayOutputStream();
this.gzipstream = new GZIPOutputStream(this.baos);
}
public void close() throws IOException {
if(this.closed) {
throw new IOException("This output stream has already been closed");
} else {
this.gzipstream.finish();
byte[] abyte = this.baos.toByteArray();
this.response.setContentLength(abyte.length);
this.response.addHeader("Content-Encoding", "gzip");
this.response.setStatus(200);
this.output.write(abyte);
this.output.flush();
this.output.close();
this.closed = true;
}
}
public void flush() throws IOException {
if(!this.closed) {
this.gzipstream.flush();
}
}
public void write(int i) throws IOException {
if(this.closed) {
throw new IOException("Cannot write to a closed output stream");
} else {
this.gzipstream.write((byte)i);
}
}
public void write(byte[] abyte) throws IOException {
this.write(abyte, 0, abyte.length);
}
public void write(byte[] abyte, int i, int j) throws IOException {
if(this.closed) {
throw new IOException("Cannot write to a closed output stream");
} else {
this.gzipstream.write(abyte, i, j);
}
}
public boolean closed() {
return this.closed;
}
}