    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.gzip;
    021
    022import java.io.OutputStream;
    023import java.util.zip.Deflater;
    024
    025/**
    026 * Parameters for the GZIP compressor.
    027 *
    028 * @see GzipCompressorInputStream
    029 * @see GzipCompressorOutputStream
    030 * @since 1.7
    031 */
    032public class GzipParameters {
    033
    034    private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
    035    private long modificationTime;
    036    private String filename;
    037    private String comment;
    038    private int operatingSystem = 255; // Unknown OS by default
    039    private int bufferSize = 512;
    040    private int deflateStrategy = Deflater.DEFAULT_STRATEGY;
    041
    042    /**
    043     * Gets size of the buffer used to retrieve compressed data.
    044     * @return The size of the buffer used to retrieve compressed data.
    045     * @since 1.21
    046     * @see #setBufferSize(int)
    047     */
    048    public int getBufferSize() {
    049        return this.bufferSize;
    050    }
    051
    052    public String getComment() {
    053        return comment;
    054    }
    055
    056    public int getCompressionLevel() {
    057        return compressionLevel;
    058    }
    059
    060    /**
    061     * Gets the deflater strategy.
    062     *
    063     * @return the deflater strategy, {@link Deflater#DEFAULT_STRATEGY} by default.
    064     * @see #setDeflateStrategy(int)
    065     * @see Deflater#setStrategy(int)
    066     * @since 1.23
    067     */
    068    public int getDeflateStrategy() {
    069        return deflateStrategy;
    070    }
    071
    072    public String getFilename() {
    073        return filename;
    074    }
    075
    076    public long getModificationTime() {
    077        return modificationTime;
    078    }
    079
    080    public int getOperatingSystem() {
    081        return operatingSystem;
    082    }
    083
    084    /**
    085     * Sets size of the buffer used to retrieve compressed data from
    086     * {@link Deflater} and write to underlying {@link OutputStream}.
    087     *
    088     * @param bufferSize the bufferSize to set. Must be a positive value.
    089     * @since 1.21
    090     */
    091    public void setBufferSize(final int bufferSize) {
    092        if (bufferSize <= 0) {
    093            throw new IllegalArgumentException("invalid buffer size: " + bufferSize);
    094        }
    095        this.bufferSize = bufferSize;
    096    }
    097
    098    public void setComment(final String comment) {
    099        this.comment = comment;
    100    }
    101
    102    /**
    103     * Sets the compression level.
    104     *
    105     * @param compressionLevel the compression level (between 0 and 9)
    106     * @see Deflater#NO_COMPRESSION
    107     * @see Deflater#BEST_SPEED
    108     * @see Deflater#DEFAULT_COMPRESSION
    109     * @see Deflater#BEST_COMPRESSION
    110     */
    111    public void setCompressionLevel(final int compressionLevel) {
    112        if (compressionLevel < -1 || compressionLevel > 9) {
    113            throw new IllegalArgumentException("Invalid gzip compression level: " + compressionLevel);
    114        }
    115        this.compressionLevel = compressionLevel;
    116    }
    117
    118    /**
    119     * Sets the deflater strategy.
    120     *
    121     * @param deflateStrategy the new compression strategy
    122     * @see Deflater#setStrategy(int)
    123     * @since 1.23
    124     */
    125    public void setDeflateStrategy(int deflateStrategy) {
    126        this.deflateStrategy = deflateStrategy;
    127    }
    128
    129    /**
    130     * Sets the name of the compressed file.
    131     *
    132     * @param fileName the name of the file without the directory path
    133     */
    134    public void setFilename(final String fileName) {
    135        this.filename = fileName;
    136    }
    137
    138    /**
    139     * Sets the modification time of the compressed file.
    140     *
    141     * @param modificationTime the modification time, in milliseconds
    142     */
    143    public void setModificationTime(final long modificationTime) {
    144        this.modificationTime = modificationTime;
    145    }
    146
    147    /**
    148     * Sets the operating system on which the compression took place.
    149     * The defined values are:
    150     * <ul>
    151     *   <li>0: FAT file system (MS-DOS, OS/2, NT/Win32)</li>
    152     *   <li>1: Amiga</li>
    153     *   <li>2: VMS (or OpenVMS)</li>
    154     *   <li>3: Unix</li>
    155     *   <li>4: VM/CMS</li>
    156     *   <li>5: Atari TOS</li>
    157     *   <li>6: HPFS file system (OS/2, NT)</li>
    158     *   <li>7: Macintosh</li>
    159     *   <li>8: Z-System</li>
    160     *   <li>9: CP/M</li>
    161     *   <li>10: TOPS-20</li>
    162     *   <li>11: NTFS file system (NT)</li>
    163     *   <li>12: QDOS</li>
    164     *   <li>13: Acorn RISCOS</li>
    165     *   <li>255: Unknown</li>
    166     * </ul>
    167     *
    168     * @param operatingSystem the code of the operating system
    169     */
    170    public void setOperatingSystem(final int operatingSystem) {
    171        this.operatingSystem = operatingSystem;
    172    }
    173}