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 * <em>Calling flush()</em>
32 * <p>
33 * Calling {@link #flush()} flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output
34 * stream. Calling this function very often may increase the compressed file size a lot.
35 * </p>
36 *
37 * @since 1.9
38 */
39 public class DeflateCompressorOutputStream extends CompressorOutputStream<DeflaterOutputStream> {
40 private final Deflater deflater;
41
42 /**
43 * Creates a Deflate compressed output stream with the default parameters.
44 *
45 * @param outputStream the stream to wrap
46 */
47 public DeflateCompressorOutputStream(final OutputStream outputStream) {
48 this(outputStream, new DeflateParameters());
49 }
50
51 /**
52 * Creates a Deflate compressed output stream with the specified parameters.
53 *
54 * @param outputStream the stream to wrap
55 * @param parameters the deflate parameters to apply
56 */
57 public DeflateCompressorOutputStream(final OutputStream outputStream, final DeflateParameters parameters) {
58 super();
59 this.deflater = new Deflater(parameters.getCompressionLevel(), !parameters.withZlibHeader());
60 this.out = new DeflaterOutputStream(outputStream, deflater);
61 }
62
63 @Override
64 public void close() throws IOException {
65 try {
66 out.close();
67 } finally {
68 deflater.end();
69 }
70 }
71
72 /**
73 * Finishes compression without closing the underlying stream.
74 * <p>
75 * No more data can be written to this stream after finishing.
76 * </p>
77 *
78 * @throws IOException on error
79 */
80 @SuppressWarnings("resource") // instance variable access
81 public void finish() throws IOException {
82 out().finish();
83 }
84
85 /**
86 * Flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output stream. Calling this
87 * function very often may increase the compressed file size a lot.
88 */
89 @Override
90 public void flush() throws IOException {
91 out.flush();
92 }
93
94 @Override
95 public void write(final byte[] buf, final int off, final int len) throws IOException {
96 out.write(buf, off, len);
97 }
98
99 }