001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest.filter;
019
020import java.io.IOException;
021import java.util.zip.GZIPOutputStream;
022import javax.servlet.ServletOutputStream;
023import javax.servlet.WriteListener;
024import javax.servlet.http.HttpServletResponse;
025import org.apache.yetus.audience.InterfaceAudience;
026
027@InterfaceAudience.Private
028public class GZIPResponseStream extends ServletOutputStream {
029  private HttpServletResponse response;
030  private GZIPOutputStream out;
031
032  public GZIPResponseStream(HttpServletResponse response) throws IOException {
033    this.response = response;
034    this.out = new GZIPOutputStream(response.getOutputStream());
035    response.addHeader("Content-Encoding", "gzip");
036  }
037
038  public void resetBuffer() {
039    if (out != null && !response.isCommitted()) {
040      response.setHeader("Content-Encoding", null);
041    }
042    out = null;
043  }
044
045  @Override
046  public void write(int b) throws IOException {
047    out.write(b);
048  }
049
050  @Override
051  public void write(byte[] b) throws IOException {
052    out.write(b);
053  }
054
055  @Override
056  public void write(byte[] b, int off, int len) throws IOException {
057    out.write(b, off, len);
058  }
059
060  @Override
061  public void close() throws IOException {
062    finish();
063    out.close();
064  }
065
066  @Override
067  public void flush() throws IOException {
068    out.flush();
069  }
070
071  public void finish() throws IOException {
072    out.finish();
073  }
074
075  @Override
076  public boolean isReady() {
077    throw new UnsupportedOperationException("Asynchonous operation is not supported.");
078  }
079
080  @Override
081  public void setWriteListener(WriteListener writeListener) {
082    throw new UnsupportedOperationException("Asynchonous operation is not supported.");
083  }
084}