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 // This may leak memory if called in a loop. The createCompressor() call 055 // may cause allocation of an untracked direct-backed buffer if native 056 // libs are being used (even if you close the stream). A Compressor 057 // object should be reused between successive calls. 058 LOG.warn("DefaultCodec.createOutputStream() may leak memory. " 059 + "Create a compressor first."); 060 return new CompressorStream(out, createCompressor(), 061 conf.getInt("io.file.buffer.size", 4*1024)); 062 } 063 064 @Override 065 public CompressionOutputStream createOutputStream(OutputStream out, 066 Compressor compressor) 067 throws IOException { 068 return new CompressorStream(out, compressor, 069 conf.getInt("io.file.buffer.size", 4*1024)); 070 } 071 072 @Override 073 public Class<? extends Compressor> getCompressorType() { 074 return ZlibFactory.getZlibCompressorType(conf); 075 } 076 077 @Override 078 public Compressor createCompressor() { 079 return ZlibFactory.getZlibCompressor(conf); 080 } 081 082 @Override 083 public CompressionInputStream createInputStream(InputStream in) 084 throws IOException { 085 return new DecompressorStream(in, createDecompressor(), 086 conf.getInt("io.file.buffer.size", 4*1024)); 087 } 088 089 @Override 090 public CompressionInputStream createInputStream(InputStream in, 091 Decompressor decompressor) 092 throws IOException { 093 return new DecompressorStream(in, decompressor, 094 conf.getInt("io.file.buffer.size", 4*1024)); 095 } 096 097 @Override 098 public Class<? extends Decompressor> getDecompressorType() { 099 return ZlibFactory.getZlibDecompressorType(conf); 100 } 101 102 @Override 103 public Decompressor createDecompressor() { 104 return ZlibFactory.getZlibDecompressor(conf); 105 } 106 107 /** 108 * {@inheritDoc} 109 */ 110 @Override 111 public DirectDecompressor createDirectDecompressor() { 112 return ZlibFactory.getZlibDirectDecompressor(conf); 113 } 114 115 116 @Override 117 public String getDefaultExtension() { 118 return ".deflate"; 119 } 120 121 }