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