1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest.filter;
21
22 import java.io.IOException;
23 import java.util.zip.GZIPOutputStream;
24
25 import javax.servlet.ServletOutputStream;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29
30 @InterfaceAudience.Private
31 public class GZIPResponseStream extends ServletOutputStream
32 {
33 private HttpServletResponse response;
34 private GZIPOutputStream out;
35
36 public GZIPResponseStream(HttpServletResponse response) throws IOException {
37 this.response = response;
38 this.out = new GZIPOutputStream(response.getOutputStream());
39 response.addHeader("Content-Encoding", "gzip");
40 }
41
42 public void resetBuffer() {
43 if (out != null && !response.isCommitted()) {
44 response.setHeader("Content-Encoding", null);
45 }
46 out = null;
47 }
48
49 @Override
50 public void write(int b) throws IOException {
51 out.write(b);
52 }
53
54 @Override
55 public void write(byte[] b) throws IOException {
56 out.write(b);
57 }
58
59 @Override
60 public void write(byte[] b, int off, int len) throws IOException {
61 out.write(b, off, len);
62 }
63
64 @Override
65 public void close() throws IOException {
66 finish();
67 out.close();
68 }
69
70 @Override
71 public void flush() throws IOException {
72 out.flush();
73 }
74
75 public void finish() throws IOException {
76 out.finish();
77 }
78 }