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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.commons.compress.compressors.deflate;
021
022import java.util.zip.Deflater;
023
024/**
025 * Parameters for the Deflate compressor.
026 * @since 1.9
027 */
028public class DeflateParameters {
029
030    static final int MAX_LEVEL = 9;
031    static final int MIN_LEVEL = 0;
032
033    private boolean zlibHeader = true;
034    private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
035
036    /**
037     * Whether or not the zlib header shall be written (when
038     * compressing) or expected (when decompressing).
039     * @return true if zlib header shall be written
040     */
041    public boolean withZlibHeader() {
042        return zlibHeader;
043    }
044
045    /**
046     * Sets the zlib header presence parameter.
047     *
048     * <p>This affects whether or not the zlib header will be written
049     * (when compressing) or expected (when decompressing).</p>
050     *
051     * @param zlibHeader true if zlib header shall be written
052     */
053    public void setWithZlibHeader(final boolean zlibHeader) {
054        this.zlibHeader = zlibHeader;
055    }
056
057    /**
058     * The compression level.
059     * @see #setCompressionLevel
060     * @return the compression level
061     */
062    public int getCompressionLevel() {
063        return compressionLevel;
064    }
065
066    /**
067     * Sets the compression level.
068     *
069     * @param compressionLevel the compression level (between 0 and 9)
070     * @see Deflater#NO_COMPRESSION
071     * @see Deflater#BEST_SPEED
072     * @see Deflater#DEFAULT_COMPRESSION
073     * @see Deflater#BEST_COMPRESSION
074     */
075    public void setCompressionLevel(final int compressionLevel) {
076        if (compressionLevel < MIN_LEVEL || compressionLevel > MAX_LEVEL) {
077            throw new IllegalArgumentException("Invalid Deflate compression level: " + compressionLevel);
078        }
079        this.compressionLevel = compressionLevel;
080    }
081
082}