Path: blob/main/epkcompiler/src/com/jcraft/jzlib/ZStream.java
8645 views
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */1/*2Copyright (c) 2000-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;3536/**37* ZStream38*39* @deprecated Not for public use in the future.40*/41@Deprecated42public class ZStream{4344static final private int MAX_WBITS=15; // 32K LZ77 window45static final private int DEF_WBITS=MAX_WBITS;4647static final private int Z_NO_FLUSH=0;48static final private int Z_PARTIAL_FLUSH=1;49static final private int Z_SYNC_FLUSH=2;50static final private int Z_FULL_FLUSH=3;51static final private int Z_FINISH=4;5253static final private int MAX_MEM_LEVEL=9;5455static final private int Z_OK=0;56static final private int Z_STREAM_END=1;57static final private int Z_NEED_DICT=2;58static final private int Z_ERRNO=-1;59static final private int Z_STREAM_ERROR=-2;60static final private int Z_DATA_ERROR=-3;61static final private int Z_MEM_ERROR=-4;62static final private int Z_BUF_ERROR=-5;63static final private int Z_VERSION_ERROR=-6;6465public byte[] next_in; // next input byte66public int next_in_index;67public int avail_in; // number of bytes available at next_in68public long total_in; // total nb of input bytes read so far6970public byte[] next_out; // next output byte should be put there71public int next_out_index;72public int avail_out; // remaining free space at next_out73public long total_out; // total nb of bytes output so far7475public String msg;7677Deflate dstate;78Inflate istate;7980int data_type; // best guess about the data type: ascii or binary8182Checksum adler;8384public ZStream(){85this(new Adler32());86}8788public ZStream(Checksum adler){89this.adler=adler;90}9192public int inflateInit(){93return inflateInit(DEF_WBITS);94}95public int inflateInit(boolean nowrap){96return inflateInit(DEF_WBITS, nowrap);97}98public int inflateInit(int w){99return inflateInit(w, false);100}101public int inflateInit(JZlib.WrapperType wrapperType) {102return inflateInit(DEF_WBITS, wrapperType);103}104public int inflateInit(int w, JZlib.WrapperType wrapperType) {105boolean nowrap = false;106if(wrapperType == JZlib.W_NONE){107nowrap = true;108}109else if(wrapperType == JZlib.W_GZIP) {110w += 16;111}112else if(wrapperType == JZlib.W_ANY) {113w |= Inflate.INFLATE_ANY;114}115else if(wrapperType == JZlib.W_ZLIB) {116}117return inflateInit(w, nowrap);118}119public int inflateInit(int w, boolean nowrap){120istate=new Inflate(this);121return istate.inflateInit(nowrap?-w:w);122}123124public int inflate(int f){125if(istate==null) return Z_STREAM_ERROR;126return istate.inflate(f);127}128public int inflateEnd(){129if(istate==null) return Z_STREAM_ERROR;130int ret=istate.inflateEnd();131// istate = null;132return ret;133}134public int inflateSync(){135if(istate == null)136return Z_STREAM_ERROR;137return istate.inflateSync();138}139public int inflateSyncPoint(){140if(istate == null)141return Z_STREAM_ERROR;142return istate.inflateSyncPoint();143}144public int inflateSetDictionary(byte[] dictionary, int dictLength){145if(istate == null)146return Z_STREAM_ERROR;147return istate.inflateSetDictionary(dictionary, dictLength);148}149public boolean inflateFinished(){150return istate.mode==12 /*DONE*/;151}152153public int deflateInit(int level){154return deflateInit(level, MAX_WBITS);155}156public int deflateInit(int level, boolean nowrap){157return deflateInit(level, MAX_WBITS, nowrap);158}159public int deflateInit(int level, int bits){160return deflateInit(level, bits, false);161}162public int deflateInit(int level, int bits, int memlevel, JZlib.WrapperType wrapperType){163if(bits < 9 || bits > 15){164return Z_STREAM_ERROR;165}166if(wrapperType == JZlib.W_NONE) {167bits *= -1;168}169else if(wrapperType == JZlib.W_GZIP) {170bits += 16;171}172else if(wrapperType == JZlib.W_ANY) {173return Z_STREAM_ERROR;174}175else if(wrapperType == JZlib.W_ZLIB) {176}177return this.deflateInit(level, bits, memlevel);178}179public int deflateInit(int level, int bits, int memlevel){180dstate=new Deflate(this);181return dstate.deflateInit(level, bits, memlevel);182}183public int deflateInit(int level, int bits, boolean nowrap){184dstate=new Deflate(this);185return dstate.deflateInit(level, nowrap?-bits:bits);186}187public int deflate(int flush){188if(dstate==null){189return Z_STREAM_ERROR;190}191return dstate.deflate(flush);192}193public int deflateEnd(){194if(dstate==null) return Z_STREAM_ERROR;195int ret=dstate.deflateEnd();196dstate=null;197return ret;198}199public int deflateParams(int level, int strategy){200if(dstate==null) return Z_STREAM_ERROR;201return dstate.deflateParams(level, strategy);202}203public int deflateSetDictionary (byte[] dictionary, int dictLength){204if(dstate == null)205return Z_STREAM_ERROR;206return dstate.deflateSetDictionary(dictionary, dictLength);207}208209// Flush as much pending output as possible. All deflate() output goes210// through this function so some applications may wish to modify it211// to avoid allocating a large strm->next_out buffer and copying into it.212// (See also read_buf()).213void flush_pending(){214int len=dstate.pending;215216if(len>avail_out) len=avail_out;217if(len==0) return;218219if(dstate.pending_buf.length<=dstate.pending_out ||220next_out.length<=next_out_index ||221dstate.pending_buf.length<(dstate.pending_out+len) ||222next_out.length<(next_out_index+len)){223//System.out.println(dstate.pending_buf.length+", "+dstate.pending_out+224// ", "+next_out.length+", "+next_out_index+", "+len);225//System.out.println("avail_out="+avail_out);226}227228System.arraycopy(dstate.pending_buf, dstate.pending_out,229next_out, next_out_index, len);230231next_out_index+=len;232dstate.pending_out+=len;233total_out+=len;234avail_out-=len;235dstate.pending-=len;236if(dstate.pending==0){237dstate.pending_out=0;238}239}240241// Read a new buffer from the current input stream, update the adler32242// and total number of bytes read. All deflate() input goes through243// this function so some applications may wish to modify it to avoid244// allocating a large strm->next_in buffer and copying from it.245// (See also flush_pending()).246int read_buf(byte[] buf, int start, int size) {247int len=avail_in;248249if(len>size) len=size;250if(len==0) return 0;251252avail_in-=len;253254if(dstate.wrap!=0) {255adler.update(next_in, next_in_index, len);256}257System.arraycopy(next_in, next_in_index, buf, start, len);258next_in_index += len;259total_in += len;260return len;261}262263public long getAdler(){264return adler.getValue();265}266267public void free(){268next_in=null;269next_out=null;270msg=null;271}272273public void setOutput(byte[] buf){274setOutput(buf, 0, buf.length);275}276277public void setOutput(byte[] buf, int off, int len){278next_out = buf;279next_out_index = off;280avail_out = len;281}282283public void setInput(byte[] buf){284setInput(buf, 0, buf.length, false);285}286287public void setInput(byte[] buf, boolean append){288setInput(buf, 0, buf.length, append);289}290291public void setInput(byte[] buf, int off, int len, boolean append){292if(len<=0 && append && next_in!=null) return;293294if(avail_in>0 && append){295byte[] tmp = new byte[avail_in+len];296System.arraycopy(next_in, next_in_index, tmp, 0, avail_in);297System.arraycopy(buf, off, tmp, avail_in, len);298next_in=tmp;299next_in_index=0;300avail_in+=len;301}302else{303next_in=buf;304next_in_index=off;305avail_in=len;306}307}308309public byte[] getNextIn(){310return next_in;311}312313public void setNextIn(byte[] next_in){314this.next_in = next_in;315}316317public int getNextInIndex(){318return next_in_index;319}320321public void setNextInIndex(int next_in_index){322this.next_in_index = next_in_index;323}324325public int getAvailIn(){326return avail_in;327}328329public void setAvailIn(int avail_in){330this.avail_in = avail_in;331}332333public byte[] getNextOut(){334return next_out;335}336337public void setNextOut(byte[] next_out){338this.next_out = next_out;339}340341public int getNextOutIndex(){342return next_out_index;343}344345public void setNextOutIndex(int next_out_index){346this.next_out_index = next_out_index;347}348349public int getAvailOut(){350return avail_out;351352}353354public void setAvailOut(int avail_out){355this.avail_out = avail_out;356}357358public long getTotalOut(){359return total_out;360}361362public long getTotalIn(){363return total_in;364}365366public String getMessage(){367return msg;368}369370/**371* Those methods are expected to be override by Inflater and Deflater.372* In the future, they will become abstract methods.373*/374public int end(){ return Z_OK; }375public boolean finished(){ return false; }376}377378379