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 * @since 1.9
031 */
032public class DeflateCompressorOutputStream extends CompressorOutputStream {
033    private final DeflaterOutputStream out;
034   
035    /**
036     * Creates a Deflate compressed output stream with the default parameters.
037     * @param outputStream the stream to wrap
038     * @throws IOException on error
039     */
040    public DeflateCompressorOutputStream(OutputStream outputStream) throws IOException {
041        this(outputStream, new DeflateParameters());
042    }
043
044    /**
045     * Creates a Deflate compressed output stream with the specified parameters.
046     * @param outputStream the stream to wrap
047     * @param parameters the deflate parameters to apply
048     * @throws IOException on error
049     */
050    public DeflateCompressorOutputStream(OutputStream outputStream,
051                                         DeflateParameters parameters) throws IOException {
052        this.out = new DeflaterOutputStream(outputStream, new Deflater(parameters.getCompressionLevel(), !parameters.withZlibHeader()));
053    }
054
055    @Override
056    public void write(int b) throws IOException {
057        out.write(b);
058    }
059
060    @Override
061    public void write(byte[] buf, int off, int len) throws IOException {
062        out.write(buf, off, len);
063    }
064
065    /**
066     * Flushes the encoder and calls <code>outputStream.flush()</code>.
067     * All buffered pending data will then be decompressible from
068     * the output stream. Calling this function very often may increase
069     * the compressed file size a lot.
070     */
071    @Override
072    public void flush() throws IOException {
073        out.flush();
074    }
075
076    /**
077     * Finishes compression without closing the underlying stream.
078     * No more data can be written to this stream after finishing.
079     */
080    public void finish() throws IOException {
081        out.finish();
082    }
083
084    @Override
085    public void close() throws IOException {
086        out.close();
087    }
088}