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     * The compression level.
038     * @see #setCompressionLevel
039     * @return the compression level
040     */
041    public int getCompressionLevel() {
042        return compressionLevel;
043    }
044
045    /**
046     * Sets the compression level.
047     *
048     * @param compressionLevel the compression level (between 0 and 9)
049     * @see Deflater#NO_COMPRESSION
050     * @see Deflater#BEST_SPEED
051     * @see Deflater#DEFAULT_COMPRESSION
052     * @see Deflater#BEST_COMPRESSION
053     */
054    public void setCompressionLevel(final int compressionLevel) {
055        if (compressionLevel < MIN_LEVEL || compressionLevel > MAX_LEVEL) {
056            throw new IllegalArgumentException("Invalid Deflate compression level: " + compressionLevel);
057        }
058        this.compressionLevel = compressionLevel;
059    }
060
061    /**
062     * Sets the zlib header presence parameter.
063     *
064     * <p>This affects whether or not the zlib header will be written
065     * (when compressing) or expected (when decompressing).</p>
066     *
067     * @param zlibHeader true if zlib header shall be written
068     */
069    public void setWithZlibHeader(final boolean zlibHeader) {
070        this.zlibHeader = zlibHeader;
071    }
072
073    /**
074     * Whether or not the zlib header shall be written (when
075     * compressing) or expected (when decompressing).
076     * @return true if zlib header shall be written
077     */
078    public boolean withZlibHeader() {
079        return zlibHeader;
080    }
081
082}