DeflateParameters.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.compress.compressors.deflate;

  20. import java.util.zip.Deflater;

  21. /**
  22.  * Parameters for the Deflate compressor.
  23.  *
  24.  * @since 1.9
  25.  */
  26. public class DeflateParameters {

  27.     static final int MAX_LEVEL = 9;
  28.     static final int MIN_LEVEL = 0;

  29.     private boolean zlibHeader = true;
  30.     private int compressionLevel = Deflater.DEFAULT_COMPRESSION;

  31.     /**
  32.      * The compression level.
  33.      *
  34.      * @see #setCompressionLevel
  35.      * @return the compression level
  36.      */
  37.     public int getCompressionLevel() {
  38.         return compressionLevel;
  39.     }

  40.     /**
  41.      * Sets the compression level.
  42.      *
  43.      * @param compressionLevel the compression level (between 0 and 9)
  44.      * @see Deflater#NO_COMPRESSION
  45.      * @see Deflater#BEST_SPEED
  46.      * @see Deflater#DEFAULT_COMPRESSION
  47.      * @see Deflater#BEST_COMPRESSION
  48.      */
  49.     public void setCompressionLevel(final int compressionLevel) {
  50.         if (compressionLevel < MIN_LEVEL || compressionLevel > MAX_LEVEL) {
  51.             throw new IllegalArgumentException("Invalid Deflate compression level: " + compressionLevel);
  52.         }
  53.         this.compressionLevel = compressionLevel;
  54.     }

  55.     /**
  56.      * Sets the zlib header presence parameter.
  57.      *
  58.      * <p>
  59.      * This affects whether or not the zlib header will be written (when compressing) or expected (when decompressing).
  60.      * </p>
  61.      *
  62.      * @param zlibHeader true if zlib header shall be written
  63.      */
  64.     public void setWithZlibHeader(final boolean zlibHeader) {
  65.         this.zlibHeader = zlibHeader;
  66.     }

  67.     /**
  68.      * Whether or not the zlib header shall be written (when compressing) or expected (when decompressing).
  69.      *
  70.      * @return true if zlib header shall be written
  71.      */
  72.     public boolean withZlibHeader() {
  73.         return zlibHeader;
  74.     }

  75. }