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    
019    package org.apache.hadoop.io.compress;
020    
021    import java.io.IOException;
022    import java.io.OutputStream;
023    
024    import org.apache.hadoop.classification.InterfaceAudience;
025    import org.apache.hadoop.classification.InterfaceStability;
026    
027    /**
028     * A compression output stream.
029     */
030    @InterfaceAudience.Public
031    @InterfaceStability.Evolving
032    public 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      @Override
048      public void close() throws IOException {
049        finish();
050        out.close();
051      }
052      
053      @Override
054      public void flush() throws IOException {
055        out.flush();
056      }
057      
058      /**
059       * Write compressed bytes to the stream.
060       * Made abstract to prevent leakage to underlying stream.
061       */
062      @Override
063      public abstract void write(byte[] b, int off, int len) throws IOException;
064    
065      /**
066       * Finishes writing compressed data to the output stream 
067       * without closing the underlying stream.
068       */
069      public abstract void finish() throws IOException;
070      
071      /**
072       * Reset the compression to the initial state. 
073       * Does not reset the underlying stream.
074       */
075      public abstract void resetState() throws IOException;
076    
077    }