GzipParameters.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.gzip;

  20. import java.io.OutputStream;
  21. import java.util.zip.Deflater;

  22. /**
  23.  * Parameters for the GZIP compressor.
  24.  *
  25.  * @see GzipCompressorInputStream
  26.  * @see GzipCompressorOutputStream
  27.  * @since 1.7
  28.  */
  29. public class GzipParameters {

  30.     private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
  31.     private long modificationTime;
  32.     private String fileName;
  33.     private String comment;
  34.     private int operatingSystem = 255; // Unknown OS by default
  35.     private int bufferSize = 512;
  36.     private int deflateStrategy = Deflater.DEFAULT_STRATEGY;

  37.     /**
  38.      * Gets size of the buffer used to retrieve compressed data.
  39.      *
  40.      * @return The size of the buffer used to retrieve compressed data.
  41.      * @since 1.21
  42.      * @see #setBufferSize(int)
  43.      */
  44.     public int getBufferSize() {
  45.         return this.bufferSize;
  46.     }

  47.     public String getComment() {
  48.         return comment;
  49.     }

  50.     public int getCompressionLevel() {
  51.         return compressionLevel;
  52.     }

  53.     /**
  54.      * Gets the deflater strategy.
  55.      *
  56.      * @return the deflater strategy, {@link Deflater#DEFAULT_STRATEGY} by default.
  57.      * @see #setDeflateStrategy(int)
  58.      * @see Deflater#setStrategy(int)
  59.      * @since 1.23
  60.      */
  61.     public int getDeflateStrategy() {
  62.         return deflateStrategy;
  63.     }

  64.     /**
  65.      * Gets the file name.
  66.      *
  67.      * @return the file name.
  68.      * @deprecated Use {@link #getFileName()}.
  69.      */
  70.     @Deprecated
  71.     public String getFilename() {
  72.         return fileName;
  73.     }

  74.     /**
  75.      * Gets the file name.
  76.      *
  77.      * @return the file name.
  78.      * @since 2.25.0
  79.      */
  80.     public String getFileName() {
  81.         return fileName;
  82.     }

  83.     public long getModificationTime() {
  84.         return modificationTime;
  85.     }

  86.     public int getOperatingSystem() {
  87.         return operatingSystem;
  88.     }

  89.     /**
  90.      * Sets size of the buffer used to retrieve compressed data from {@link Deflater} and write to underlying {@link OutputStream}.
  91.      *
  92.      * @param bufferSize the bufferSize to set. Must be a positive value.
  93.      * @since 1.21
  94.      */
  95.     public void setBufferSize(final int bufferSize) {
  96.         if (bufferSize <= 0) {
  97.             throw new IllegalArgumentException("invalid buffer size: " + bufferSize);
  98.         }
  99.         this.bufferSize = bufferSize;
  100.     }

  101.     public void setComment(final String comment) {
  102.         this.comment = comment;
  103.     }

  104.     /**
  105.      * Sets the compression level.
  106.      *
  107.      * @param compressionLevel the compression level (between 0 and 9)
  108.      * @see Deflater#NO_COMPRESSION
  109.      * @see Deflater#BEST_SPEED
  110.      * @see Deflater#DEFAULT_COMPRESSION
  111.      * @see Deflater#BEST_COMPRESSION
  112.      */
  113.     public void setCompressionLevel(final int compressionLevel) {
  114.         if (compressionLevel < -1 || compressionLevel > 9) {
  115.             throw new IllegalArgumentException("Invalid gzip compression level: " + compressionLevel);
  116.         }
  117.         this.compressionLevel = compressionLevel;
  118.     }

  119.     /**
  120.      * Sets the deflater strategy.
  121.      *
  122.      * @param deflateStrategy the new compression strategy
  123.      * @see Deflater#setStrategy(int)
  124.      * @since 1.23
  125.      */
  126.     public void setDeflateStrategy(final int deflateStrategy) {
  127.         this.deflateStrategy = deflateStrategy;
  128.     }

  129.     /**
  130.      * Sets the name of the compressed file.
  131.      *
  132.      * @param fileName the name of the file without the directory path
  133.      * @deprecated Use {@link #setFileName(String)}.
  134.      */
  135.     @Deprecated
  136.     public void setFilename(final String fileName) {
  137.         this.fileName = fileName;
  138.     }

  139.     /**
  140.      * Sets the name of the compressed file.
  141.      *
  142.      * @param fileName the name of the file without the directory path
  143.      */
  144.     public void setFileName(final String fileName) {
  145.         this.fileName = fileName;
  146.     }

  147.     /**
  148.      * Sets the modification time of the compressed file.
  149.      *
  150.      * @param modificationTime the modification time, in milliseconds
  151.      */
  152.     public void setModificationTime(final long modificationTime) {
  153.         this.modificationTime = modificationTime;
  154.     }

  155.     /**
  156.      * Sets the operating system on which the compression took place. The defined values are:
  157.      * <ul>
  158.      * <li>0: FAT file system (MS-DOS, OS/2, NT/Win32)</li>
  159.      * <li>1: Amiga</li>
  160.      * <li>2: VMS (or OpenVMS)</li>
  161.      * <li>3: Unix</li>
  162.      * <li>4: VM/CMS</li>
  163.      * <li>5: Atari TOS</li>
  164.      * <li>6: HPFS file system (OS/2, NT)</li>
  165.      * <li>7: Macintosh</li>
  166.      * <li>8: Z-System</li>
  167.      * <li>9: CP/M</li>
  168.      * <li>10: TOPS-20</li>
  169.      * <li>11: NTFS file system (NT)</li>
  170.      * <li>12: QDOS</li>
  171.      * <li>13: Acorn RISCOS</li>
  172.      * <li>255: Unknown</li>
  173.      * </ul>
  174.      *
  175.      * @param operatingSystem the code of the operating system
  176.      */
  177.     public void setOperatingSystem(final int operatingSystem) {
  178.         this.operatingSystem = operatingSystem;
  179.     }
  180. }