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