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 */
019package org.apache.commons.compress.compressors.deflate;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.util.zip.Deflater;
024import java.util.zip.DeflaterOutputStream;
025
026import org.apache.commons.compress.compressors.CompressorOutputStream;
027
028/**
029 * Deflate compressor.
030 *
031 * <em>Calling flush()</em>
032 * <p>
033 * Calling {@link #flush()} flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output
034 * stream. Calling this function very often may increase the compressed file size a lot.
035 * </p>
036 *
037 * @since 1.9
038 */
039public class DeflateCompressorOutputStream extends CompressorOutputStream<DeflaterOutputStream> {
040    private final Deflater deflater;
041
042    /**
043     * Creates a Deflate compressed output stream with the default parameters.
044     *
045     * @param outputStream the stream to wrap
046     */
047    public DeflateCompressorOutputStream(final OutputStream outputStream) {
048        this(outputStream, new DeflateParameters());
049    }
050
051    /**
052     * Creates a Deflate compressed output stream with the specified parameters.
053     *
054     * @param outputStream the stream to wrap
055     * @param parameters   the deflate parameters to apply
056     */
057    public DeflateCompressorOutputStream(final OutputStream outputStream, final DeflateParameters parameters) {
058        super();
059        this.deflater = new Deflater(parameters.getCompressionLevel(), !parameters.withZlibHeader());
060        this.out = new DeflaterOutputStream(outputStream, deflater);
061    }
062
063    @Override
064    public void close() throws IOException {
065        try {
066            out.close();
067        } finally {
068            deflater.end();
069        }
070    }
071
072    /**
073     * Finishes compression without closing the underlying stream.
074     * <p>
075     * No more data can be written to this stream after finishing.
076     * </p>
077     *
078     * @throws IOException on error
079     */
080    @SuppressWarnings("resource") // instance variable access
081    public void finish() throws IOException {
082        out().finish();
083    }
084
085    /**
086     * Flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output stream. Calling this
087     * function very often may increase the compressed file size a lot.
088     */
089    @Override
090    public void flush() throws IOException {
091        out.flush();
092    }
093
094    @Override
095    public void write(final byte[] buf, final int off, final int len) throws IOException {
096        out.write(buf, off, len);
097    }
098
099}