View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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  }