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 */ 018 019package org.apache.hadoop.io.compress; 020 021import java.io.IOException; 022import java.io.OutputStream; 023 024import org.apache.hadoop.classification.InterfaceAudience; 025import org.apache.hadoop.classification.InterfaceStability; 026 027/** 028 * A compression output stream. 029 */ 030@InterfaceAudience.Public 031@InterfaceStability.Evolving 032public abstract class CompressionOutputStream extends OutputStream { 033 /** 034 * The output stream to be compressed. 035 */ 036 protected final OutputStream out; 037 038 /** 039 * Create a compression output stream that writes 040 * the compressed bytes to the given stream. 041 * @param out 042 */ 043 protected CompressionOutputStream(OutputStream out) { 044 this.out = out; 045 } 046 047 public void close() throws IOException { 048 finish(); 049 out.close(); 050 } 051 052 public void flush() throws IOException { 053 out.flush(); 054 } 055 056 /** 057 * Write compressed bytes to the stream. 058 * Made abstract to prevent leakage to underlying stream. 059 */ 060 public abstract void write(byte[] b, int off, int len) throws IOException; 061 062 /** 063 * Finishes writing compressed data to the output stream 064 * without closing the underlying stream. 065 */ 066 public abstract void finish() throws IOException; 067 068 /** 069 * Reset the compression to the initial state. 070 * Does not reset the underlying stream. 071 */ 072 public abstract void resetState() throws IOException; 073 074}