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.ZlibFactory;
032
033@InterfaceAudience.Public
034@InterfaceStability.Evolving
035public class DefaultCodec implements Configurable, CompressionCodec {
036  private static final Log LOG = LogFactory.getLog(DefaultCodec.class);
037  
038  Configuration conf;
039
040  public void setConf(Configuration conf) {
041    this.conf = conf;
042  }
043  
044  public Configuration getConf() {
045    return conf;
046  }
047  
048  public CompressionOutputStream createOutputStream(OutputStream out) 
049  throws IOException {
050    // This may leak memory if called in a loop. The createCompressor() call
051    // may cause allocation of an untracked direct-backed buffer if native
052    // libs are being used (even if you close the stream).  A Compressor
053    // object should be reused between successive calls.
054    LOG.warn("DefaultCodec.createOutputStream() may leak memory. "
055        + "Create a compressor first.");
056    return new CompressorStream(out, createCompressor(), 
057                                conf.getInt("io.file.buffer.size", 4*1024));
058  }
059
060  public CompressionOutputStream createOutputStream(OutputStream out, 
061                                                    Compressor compressor) 
062  throws IOException {
063    return new CompressorStream(out, compressor, 
064                                conf.getInt("io.file.buffer.size", 4*1024));
065  }
066
067  public Class<? extends Compressor> getCompressorType() {
068    return ZlibFactory.getZlibCompressorType(conf);
069  }
070
071  public Compressor createCompressor() {
072    return ZlibFactory.getZlibCompressor(conf);
073  }
074
075  public CompressionInputStream createInputStream(InputStream in) 
076  throws IOException {
077    return new DecompressorStream(in, createDecompressor(),
078                                  conf.getInt("io.file.buffer.size", 4*1024));
079  }
080
081  public CompressionInputStream createInputStream(InputStream in, 
082                                                  Decompressor decompressor) 
083  throws IOException {
084    return new DecompressorStream(in, decompressor, 
085                                  conf.getInt("io.file.buffer.size", 4*1024));
086  }
087
088  public Class<? extends Decompressor> getDecompressorType() {
089    return ZlibFactory.getZlibDecompressorType(conf);
090  }
091
092  public Decompressor createDecompressor() {
093    return ZlibFactory.getZlibDecompressor(conf);
094  }
095  
096  public String getDefaultExtension() {
097    return ".deflate";
098  }
099
100}