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