View Javadoc
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  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.zip.Deflater;
24  import java.util.zip.DeflaterOutputStream;
25  
26  import org.apache.commons.compress.compressors.CompressorOutputStream;
27  
28  /**
29   * Deflate compressor.
30   *
31   * @since 1.9
32   */
33  public class DeflateCompressorOutputStream extends CompressorOutputStream {
34      private final DeflaterOutputStream out;
35      private final Deflater deflater;
36  
37      /**
38       * Creates a Deflate compressed output stream with the default parameters.
39       *
40       * @param outputStream the stream to wrap
41       */
42      public DeflateCompressorOutputStream(final OutputStream outputStream) {
43          this(outputStream, new DeflateParameters());
44      }
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          this.deflater = new Deflater(parameters.getCompressionLevel(), !parameters.withZlibHeader());
54          this.out = new DeflaterOutputStream(outputStream, deflater);
55      }
56  
57      @Override
58      public void close() throws IOException {
59          try {
60              out.close();
61          } finally {
62              deflater.end();
63          }
64      }
65  
66      /**
67       * Finishes compression without closing the underlying stream.
68       * <p>
69       * No more data can be written to this stream after finishing.
70       * </p>
71       *
72       * @throws IOException on error
73       */
74      public void finish() throws IOException {
75          out.finish();
76      }
77  
78      /**
79       * Flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output stream. Calling this
80       * function very often may increase the compressed file size a lot.
81       */
82      @Override
83      public void flush() throws IOException {
84          out.flush();
85      }
86  
87      @Override
88      public void write(final byte[] buf, final int off, final int len) throws IOException {
89          out.write(buf, off, len);
90      }
91  
92      @Override
93      public void write(final int b) throws IOException {
94          out.write(b);
95      }
96  }