src/org/apache/commons/compress/compressors/deflate/DeflateCompressorOutputStream.java
raw symbols SCIP| 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 | * https://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 | this.deflater = new Deflater(parameters.getCompressionLevel(), !parameters.withZlibHeader()); |
| 59 | this.out = new DeflaterOutputStream(outputStream, deflater); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void close() throws IOException { |
| 64 | try { |
| 65 | super.close(); |
| 66 | } finally { |
| 67 | deflater.end(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Finishes compression without closing the underlying stream. |
| 73 | * <p> |
| 74 | * No more data can be written to this stream after finishing. |
| 75 | * </p> |
| 76 | * |
| 77 | * @throws IOException on error |
| 78 | */ |
| 79 | @Override |
| 80 | @SuppressWarnings("resource") // instance variable access |
| 81 | public void finish() throws IOException { |
| 82 | out().finish(); |
| 83 | super.finish(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output stream. Calling this |
| 88 | * function very often may increase the compressed file size a lot. |
| 89 | */ |
| 90 | @Override |
| 91 | public void flush() throws IOException { |
| 92 | out.flush(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void write(final byte[] buf, final int off, final int len) throws IOException { |
| 97 | out.write(buf, off, len); |
| 98 | } |
| 99 | |
| 100 | } |