Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/DeflaterOutputStream.java
8650 views
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */1/*2Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:671. Redistributions of source code must retain the above copyright notice,8this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in12the documentation and/or other materials provided with the distribution.13143. The names of the authors may not be used to endorse or promote products15derived from this software without specific prior written permission.1617THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,18INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,20INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,23OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,26EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829package com.jcraft.jzlib;30import java.io.*;3132public class DeflaterOutputStream extends FilterOutputStream {3334protected final Deflater deflater;3536protected byte[] buffer;3738private boolean closed = false;3940private boolean syncFlush = false;4142private final byte[] buf1 = new byte[1];4344protected boolean mydeflater = false;4546private boolean close_out = true;4748protected static final int DEFAULT_BUFSIZE = 512;4950public DeflaterOutputStream(OutputStream out) throws IOException {51this(out,52new Deflater(JZlib.Z_DEFAULT_COMPRESSION),53DEFAULT_BUFSIZE, true);54mydeflater = true;55}5657public DeflaterOutputStream(OutputStream out, Deflater def) throws IOException {58this(out, def, DEFAULT_BUFSIZE, true);59}6061public DeflaterOutputStream(OutputStream out,62Deflater deflater,63int size) throws IOException {64this(out, deflater, size, true);65}66public DeflaterOutputStream(OutputStream out,67Deflater deflater,68int size,69boolean close_out) throws IOException {70super(out);71if (out == null || deflater == null) {72throw new NullPointerException();73}74else if (size <= 0) {75throw new IllegalArgumentException("buffer size must be greater than 0");76}77this.deflater = deflater;78buffer = new byte[size];79this.close_out = close_out;80}8182public void write(int b) throws IOException {83buf1[0] = (byte)(b & 0xff);84write(buf1, 0, 1);85}8687public void write(byte[] b, int off, int len) throws IOException {88if (deflater.finished()) {89throw new IOException("finished");90}91else if (off<0 | len<0 | off+len>b.length) {92throw new IndexOutOfBoundsException();93}94else if (len == 0) {95return;96}97else {98int flush = syncFlush ? JZlib.Z_SYNC_FLUSH : JZlib.Z_NO_FLUSH;99deflater.setInput(b, off, len, true);100while (deflater.avail_in>0) {101int err = deflate(flush);102if (err == JZlib.Z_STREAM_END)103break;104}105}106}107108public void finish() throws IOException {109while (!deflater.finished()) {110deflate(JZlib.Z_FINISH);111}112}113114public void close() throws IOException {115if (!closed) {116finish();117if (mydeflater){118deflater.end();119}120if(close_out)121out.close();122closed = true;123}124}125126protected int deflate(int flush) throws IOException {127deflater.setOutput(buffer, 0, buffer.length);128int err = deflater.deflate(flush);129switch(err) {130case JZlib.Z_OK:131case JZlib.Z_STREAM_END:132break;133case JZlib.Z_BUF_ERROR:134if(deflater.avail_in<=0 && flush!=JZlib.Z_FINISH){135// flush() without any data136break;137}138default:139throw new IOException("failed to deflate: error="+err+" avail_out="+deflater.avail_out);140}141int len = deflater.next_out_index;142if (len > 0) {143out.write(buffer, 0, len);144}145return err;146}147148public void flush() throws IOException {149if (syncFlush && !deflater.finished()) {150while (true) {151int err = deflate(JZlib.Z_SYNC_FLUSH);152if (deflater.next_out_index < buffer.length)153break;154if (err == JZlib.Z_STREAM_END)155break;156}157}158out.flush();159}160161public long getTotalIn() {162return deflater.getTotalIn();163}164165public long getTotalOut() {166return deflater.getTotalOut();167}168169public void setSyncFlush(boolean syncFlush){170this.syncFlush = syncFlush;171}172173public boolean getSyncFlush(){174return this.syncFlush;175}176177public Deflater getDeflater(){178return deflater;179}180}181182183