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.InputStream;
023    import java.io.OutputStream;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.hadoop.classification.InterfaceAudience;
028    import org.apache.hadoop.classification.InterfaceStability;
029    import org.apache.hadoop.conf.Configurable;
030    import org.apache.hadoop.conf.Configuration;
031    import org.apache.hadoop.io.compress.zlib.ZlibDecompressor;
032    import org.apache.hadoop.io.compress.zlib.ZlibFactory;
033    
034    @InterfaceAudience.Public
035    @InterfaceStability.Evolving
036    public class DefaultCodec implements Configurable, CompressionCodec, DirectDecompressionCodec {
037      private static final Log LOG = LogFactory.getLog(DefaultCodec.class);
038      
039      Configuration conf;
040    
041      @Override
042      public void setConf(Configuration conf) {
043        this.conf = conf;
044      }
045      
046      @Override
047      public Configuration getConf() {
048        return conf;
049      }
050      
051      @Override
052      public CompressionOutputStream createOutputStream(OutputStream out) 
053      throws IOException {
054        return CompressionCodec.Util.
055            createOutputStreamWithCodecPool(this, conf, out);
056      }
057    
058      @Override
059      public CompressionOutputStream createOutputStream(OutputStream out, 
060                                                        Compressor compressor) 
061      throws IOException {
062        return new CompressorStream(out, compressor, 
063                                    conf.getInt("io.file.buffer.size", 4*1024));
064      }
065    
066      @Override
067      public Class<? extends Compressor> getCompressorType() {
068        return ZlibFactory.getZlibCompressorType(conf);
069      }
070    
071      @Override
072      public Compressor createCompressor() {
073        return ZlibFactory.getZlibCompressor(conf);
074      }
075    
076      @Override
077      public CompressionInputStream createInputStream(InputStream in) 
078      throws IOException {
079        return CompressionCodec.Util.
080            createInputStreamWithCodecPool(this, conf, in);
081      }
082    
083      @Override
084      public CompressionInputStream createInputStream(InputStream in, 
085                                                      Decompressor decompressor) 
086      throws IOException {
087        return new DecompressorStream(in, decompressor, 
088                                      conf.getInt("io.file.buffer.size", 4*1024));
089      }
090    
091      @Override
092      public Class<? extends Decompressor> getDecompressorType() {
093        return ZlibFactory.getZlibDecompressorType(conf);
094      }
095    
096      @Override
097      public Decompressor createDecompressor() {
098        return ZlibFactory.getZlibDecompressor(conf);
099      }
100      
101      /**
102       * {@inheritDoc}
103       */
104      @Override
105      public DirectDecompressor createDirectDecompressor() {
106        return ZlibFactory.getZlibDirectDecompressor(conf);
107      }
108      
109      
110      @Override
111      public String getDefaultExtension() {
112        return ".deflate";
113      }
114    
115    }