Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/Deflater.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*/28/*29* This program is based on zlib-1.1.3, so all credit should go authors30* Jean-loup Gailly([email protected]) and Mark Adler([email protected])31* and contributors of zlib.32*/3334package com.jcraft.jzlib;3536final public class Deflater extends ZStream{3738static final private int MAX_WBITS=15; // 32K LZ77 window39static final private int DEF_WBITS=MAX_WBITS;4041static final private int Z_NO_FLUSH=0;42static final private int Z_PARTIAL_FLUSH=1;43static final private int Z_SYNC_FLUSH=2;44static final private int Z_FULL_FLUSH=3;45static final private int Z_FINISH=4;4647static final private int MAX_MEM_LEVEL=9;4849static final private int Z_OK=0;50static final private int Z_STREAM_END=1;51static final private int Z_NEED_DICT=2;52static final private int Z_ERRNO=-1;53static final private int Z_STREAM_ERROR=-2;54static final private int Z_DATA_ERROR=-3;55static final private int Z_MEM_ERROR=-4;56static final private int Z_BUF_ERROR=-5;57static final private int Z_VERSION_ERROR=-6;5859private boolean finished = false;6061public Deflater(){62super();63}6465public Deflater(int level) throws GZIPException {66this(level, MAX_WBITS);67}6869public Deflater(int level, boolean nowrap) throws GZIPException {70this(level, MAX_WBITS, nowrap);71}7273public Deflater(int level, int bits) throws GZIPException {74this(level, bits, false);75}7677public Deflater(int level, int bits, boolean nowrap) throws GZIPException {78super();79int ret = init(level, bits, nowrap);80if(ret!=Z_OK)81throw new GZIPException(ret+": "+msg);82}8384public Deflater(int level, int bits, int memlevel, JZlib.WrapperType wrapperType) throws GZIPException {85super();86int ret = init(level, bits, memlevel, wrapperType);87if(ret!=Z_OK)88throw new GZIPException(ret+": "+msg);89}9091public Deflater(int level, int bits, int memlevel) throws GZIPException {92super();93int ret = init(level, bits, memlevel);94if(ret!=Z_OK)95throw new GZIPException(ret+": "+msg);96}9798public int init(int level){99return init(level, MAX_WBITS);100}101public int init(int level, boolean nowrap){102return init(level, MAX_WBITS, nowrap);103}104public int init(int level, int bits){105return init(level, bits, false);106}107public int init(int level, int bits, int memlevel, JZlib.WrapperType wrapperType){108if(bits < 9 || bits > 15){109return Z_STREAM_ERROR;110}111if(wrapperType == JZlib.W_NONE) {112bits *= -1;113}114else if(wrapperType == JZlib.W_GZIP) {115bits += 16;116}117else if(wrapperType == JZlib.W_ANY) {118return Z_STREAM_ERROR;119}120else if(wrapperType == JZlib.W_ZLIB) {121}122return init(level, bits, memlevel);123}124public int init(int level, int bits, int memlevel){125finished = false;126dstate=new Deflate(this);127return dstate.deflateInit(level, bits, memlevel);128}129public int init(int level, int bits, boolean nowrap){130finished = false;131dstate=new Deflate(this);132return dstate.deflateInit(level, nowrap?-bits:bits);133}134135public int deflate(int flush){136if(dstate==null){137return Z_STREAM_ERROR;138}139int ret = dstate.deflate(flush);140if(ret == Z_STREAM_END)141finished = true;142return ret;143}144public int end(){145finished = true;146if(dstate==null) return Z_STREAM_ERROR;147int ret=dstate.deflateEnd();148dstate=null;149free();150return ret;151}152public int params(int level, int strategy){153if(dstate==null) return Z_STREAM_ERROR;154return dstate.deflateParams(level, strategy);155}156public int setDictionary (byte[] dictionary, int dictLength){157if(dstate == null)158return Z_STREAM_ERROR;159return dstate.deflateSetDictionary(dictionary, dictLength);160}161162public boolean finished(){163return finished;164}165166public int copy(Deflater src){167this.finished = src.finished;168return Deflate.deflateCopy(this, src);169}170}171172173