DeflateCompressorOutputStream.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.deflate;

  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.zip.Deflater;
  23. import java.util.zip.DeflaterOutputStream;

  24. import org.apache.commons.compress.compressors.CompressorOutputStream;

  25. /**
  26.  * Deflate compressor.
  27.  *
  28.  * <em>Calling flush()</em>
  29.  * <p>
  30.  * Calling {@link #flush()} flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output
  31.  * stream. Calling this function very often may increase the compressed file size a lot.
  32.  * </p>
  33.  *
  34.  * @since 1.9
  35.  */
  36. public class DeflateCompressorOutputStream extends CompressorOutputStream<DeflaterOutputStream> {
  37.     private final Deflater deflater;

  38.     /**
  39.      * Creates a Deflate compressed output stream with the default parameters.
  40.      *
  41.      * @param outputStream the stream to wrap
  42.      */
  43.     public DeflateCompressorOutputStream(final OutputStream outputStream) {
  44.         this(outputStream, new DeflateParameters());
  45.     }

  46.     /**
  47.      * Creates a Deflate compressed output stream with the specified parameters.
  48.      *
  49.      * @param outputStream the stream to wrap
  50.      * @param parameters   the deflate parameters to apply
  51.      */
  52.     public DeflateCompressorOutputStream(final OutputStream outputStream, final DeflateParameters parameters) {
  53.         super();
  54.         this.deflater = new Deflater(parameters.getCompressionLevel(), !parameters.withZlibHeader());
  55.         this.out = new DeflaterOutputStream(outputStream, deflater);
  56.     }

  57.     @Override
  58.     public void close() throws IOException {
  59.         try {
  60.             out.close();
  61.         } finally {
  62.             deflater.end();
  63.         }
  64.     }

  65.     /**
  66.      * Finishes compression without closing the underlying stream.
  67.      * <p>
  68.      * No more data can be written to this stream after finishing.
  69.      * </p>
  70.      *
  71.      * @throws IOException on error
  72.      */
  73.     @SuppressWarnings("resource") // instance variable access
  74.     public void finish() throws IOException {
  75.         out().finish();
  76.     }

  77.     /**
  78.      * Flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output stream. Calling this
  79.      * function very often may increase the compressed file size a lot.
  80.      */
  81.     @Override
  82.     public void flush() throws IOException {
  83.         out.flush();
  84.     }

  85.     @Override
  86.     public void write(final byte[] buf, final int off, final int len) throws IOException {
  87.         out.write(buf, off, len);
  88.     }

  89. }