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.xz;
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.hbase.io.compress.CompressionUtil;
026import org.apache.hadoop.io.compress.BlockCompressorStream;
027import org.apache.hadoop.io.compress.BlockDecompressorStream;
028import org.apache.hadoop.io.compress.CompressionCodec;
029import org.apache.hadoop.io.compress.CompressionInputStream;
030import org.apache.hadoop.io.compress.CompressionOutputStream;
031import org.apache.hadoop.io.compress.Compressor;
032import org.apache.hadoop.io.compress.Decompressor;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * Hadoop lzma codec implemented with XZ for Java.
037 */
038@InterfaceAudience.Private
039public class LzmaCodec implements Configurable, CompressionCodec {
040
041  public static final String LZMA_LEVEL_KEY = "hbase.io.compress.lzma.level";
042  public static final int LZMA_LEVEL_DEFAULT = 6;
043  public static final String LZMA_BUFFERSIZE_KEY = "hbase.io.compress.lzma.buffersize";
044  public static final int LZMA_BUFFERSIZE_DEFAULT = 256 * 1024;
045
046  private Configuration conf;
047
048  public LzmaCodec() {
049    conf = new Configuration();
050  }
051
052  @Override
053  public Configuration getConf() {
054    return conf;
055  }
056
057  @Override
058  public void setConf(Configuration conf) {
059    this.conf = conf;
060  }
061
062  @Override
063  public Compressor createCompressor() {
064    return new LzmaCompressor(getLevel(conf), getBufferSize(conf));
065  }
066
067  @Override
068  public Decompressor createDecompressor() {
069    return new LzmaDecompressor(getBufferSize(conf));
070  }
071
072  @Override
073  public CompressionInputStream createInputStream(InputStream in) throws IOException {
074    return createInputStream(in, createDecompressor());
075  }
076
077  @Override
078  public CompressionInputStream createInputStream(InputStream in, Decompressor d)
079    throws IOException {
080    return new BlockDecompressorStream(in, d, getBufferSize(conf));
081  }
082
083  @Override
084  public CompressionOutputStream createOutputStream(OutputStream out) throws IOException {
085    return createOutputStream(out, createCompressor());
086  }
087
088  @Override
089  public CompressionOutputStream createOutputStream(OutputStream out, Compressor c)
090    throws IOException {
091    int bufferSize = getBufferSize(conf);
092    return new BlockCompressorStream(out, c, bufferSize,
093      CompressionUtil.compressionOverhead(bufferSize));
094  }
095
096  @Override
097  public Class<? extends Compressor> getCompressorType() {
098    return LzmaCompressor.class;
099  }
100
101  @Override
102  public Class<? extends Decompressor> getDecompressorType() {
103    return LzmaDecompressor.class;
104  }
105
106  @Override
107  public String getDefaultExtension() {
108    return ".lzma";
109  }
110
111  // Package private
112
113  static int getLevel(Configuration conf) {
114    return conf.getInt(LZMA_LEVEL_KEY, LZMA_LEVEL_DEFAULT);
115  }
116
117  static int getBufferSize(Configuration conf) {
118    return conf.getInt(LZMA_BUFFERSIZE_KEY, LZMA_BUFFERSIZE_DEFAULT);
119  }
120
121}