View Javadoc
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  
20  package org.apache.commons.compress.compressors.deflate;
21  
22  import java.util.zip.Deflater;
23  
24  /**
25   * Parameters for the Deflate compressor.
26   *
27   * @since 1.9
28   */
29  public class DeflateParameters {
30  
31      static final int MAX_LEVEL = 9;
32      static final int MIN_LEVEL = 0;
33  
34      private boolean zlibHeader = true;
35      private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
36  
37      /**
38       * The compression level.
39       *
40       * @see #setCompressionLevel
41       * @return the compression level
42       */
43      public int getCompressionLevel() {
44          return compressionLevel;
45      }
46  
47      /**
48       * Sets the compression level.
49       *
50       * @param compressionLevel the compression level (between 0 and 9)
51       * @see Deflater#NO_COMPRESSION
52       * @see Deflater#BEST_SPEED
53       * @see Deflater#DEFAULT_COMPRESSION
54       * @see Deflater#BEST_COMPRESSION
55       */
56      public void setCompressionLevel(final int compressionLevel) {
57          if (compressionLevel < MIN_LEVEL || compressionLevel > MAX_LEVEL) {
58              throw new IllegalArgumentException("Invalid Deflate compression level: " + compressionLevel);
59          }
60          this.compressionLevel = compressionLevel;
61      }
62  
63      /**
64       * Sets the zlib header presence parameter.
65       *
66       * <p>
67       * This affects whether or not the zlib header will be written (when compressing) or expected (when decompressing).
68       * </p>
69       *
70       * @param zlibHeader true if zlib header shall be written
71       */
72      public void setWithZlibHeader(final boolean zlibHeader) {
73          this.zlibHeader = zlibHeader;
74      }
75  
76      /**
77       * Whether or not the zlib header shall be written (when compressing) or expected (when decompressing).
78       *
79       * @return true if zlib header shall be written
80       */
81      public boolean withZlibHeader() {
82          return zlibHeader;
83      }
84  
85  }