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;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022@InterfaceAudience.Private
023public final class CompressionUtil {
024
025  private CompressionUtil() {
026  }
027
028  /**
029   * Round up to the next power of two, unless the value would become negative (ints are signed), in
030   * which case just return Integer.MAX_VALUE.
031   */
032  public static int roundInt2(int v) {
033    v = Integer.highestOneBit(v) << 1;
034    if (v < 0) {
035      return Integer.MAX_VALUE;
036    }
037    return v;
038  }
039
040  /**
041   * Most compression algorithms can be presented with pathological input that causes an expansion
042   * rather than a compression. Hadoop's compression API requires that we calculate additional
043   * buffer space required for the worst case. There is a formula developed for gzip that applies as
044   * a ballpark to all LZ variants. It should be good enough for now and has been tested as such
045   * with a range of different inputs.
046   */
047  public static int compressionOverhead(int bufferSize) {
048    // Given an input buffer of 'buffersize' bytes we presume a worst case expansion of
049    // 32 bytes (block header) and addition 1/6th of the input size.
050    return (bufferSize / 6) + 32;
051  }
052}