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