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