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 */ 018package org.apache.hadoop.hbase.io.compress.lz4; 019 020import java.io.IOException; 021import java.io.InputStream; 022import java.io.OutputStream; 023import org.apache.hadoop.conf.Configurable; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.CommonConfigurationKeys; 026import org.apache.hadoop.hbase.io.compress.CompressionUtil; 027import org.apache.hadoop.io.compress.BlockCompressorStream; 028import org.apache.hadoop.io.compress.BlockDecompressorStream; 029import org.apache.hadoop.io.compress.CompressionCodec; 030import org.apache.hadoop.io.compress.CompressionInputStream; 031import org.apache.hadoop.io.compress.CompressionOutputStream; 032import org.apache.hadoop.io.compress.Compressor; 033import org.apache.hadoop.io.compress.Decompressor; 034import org.apache.yetus.audience.InterfaceAudience; 035 036/** 037 * Hadoop Lz4 codec implemented with lz4-java. 038 * <p> 039 * This is data format compatible with Hadoop's native lz4 codec. 040 */ 041@InterfaceAudience.Private 042public class Lz4Codec implements Configurable, CompressionCodec { 043 044 public static final String LZ4_BUFFER_SIZE_KEY = "hbase.io.compress.lz4.buffersize"; 045 046 private Configuration conf; 047 private int bufferSize; 048 049 public Lz4Codec() { 050 conf = new Configuration(); 051 this.bufferSize = getBufferSize(conf); 052 } 053 054 @Override 055 public Configuration getConf() { 056 return conf; 057 } 058 059 @Override 060 public void setConf(Configuration conf) { 061 this.conf = conf; 062 this.bufferSize = getBufferSize(conf); 063 } 064 065 @Override 066 public Compressor createCompressor() { 067 return new Lz4Compressor(bufferSize); 068 } 069 070 @Override 071 public Decompressor createDecompressor() { 072 return new Lz4Decompressor(bufferSize); 073 } 074 075 @Override 076 public CompressionInputStream createInputStream(InputStream in) throws IOException { 077 return createInputStream(in, createDecompressor()); 078 } 079 080 @Override 081 public CompressionInputStream createInputStream(InputStream in, Decompressor d) 082 throws IOException { 083 return new BlockDecompressorStream(in, d, bufferSize); 084 } 085 086 @Override 087 public CompressionOutputStream createOutputStream(OutputStream out) throws IOException { 088 return createOutputStream(out, createCompressor()); 089 } 090 091 @Override 092 public CompressionOutputStream createOutputStream(OutputStream out, Compressor c) 093 throws IOException { 094 return new BlockCompressorStream(out, c, bufferSize, 095 CompressionUtil.compressionOverhead(bufferSize)); 096 } 097 098 @Override 099 public Class<? extends Compressor> getCompressorType() { 100 return Lz4Compressor.class; 101 } 102 103 @Override 104 public Class<? extends Decompressor> getDecompressorType() { 105 return Lz4Decompressor.class; 106 } 107 108 @Override 109 public String getDefaultExtension() { 110 return ".lz4"; 111 } 112 113 // Package private 114 115 static int getBufferSize(Configuration conf) { 116 return conf.getInt(LZ4_BUFFER_SIZE_KEY, 117 conf.getInt(CommonConfigurationKeys.IO_COMPRESSION_CODEC_LZ4_BUFFERSIZE_KEY, 118 CommonConfigurationKeys.IO_COMPRESSION_CODEC_LZ4_BUFFERSIZE_DEFAULT)); 119 } 120 121}