Path: blob/main/epkcompiler/src/com/jcraft/jzlib/ZInputStream.java
8645 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.*;3132/**33* ZInputStream34*35* @deprecated use DeflaterOutputStream or InflaterInputStream36*/37@Deprecated38public class ZInputStream extends FilterInputStream {3940protected int flush=JZlib.Z_NO_FLUSH;41protected boolean compress;42protected InputStream in=null;4344protected Deflater deflater;45protected InflaterInputStream iis;4647public ZInputStream(InputStream in) throws IOException {48this(in, false);49}50public ZInputStream(InputStream in, boolean nowrap) throws IOException {51super(in);52iis = new InflaterInputStream(in, nowrap);53compress=false;54}5556public ZInputStream(InputStream in, int level) throws IOException {57super(in);58this.in=in;59deflater = new Deflater();60deflater.init(level);61compress=true;62}6364private byte[] buf1 = new byte[1];65public int read() throws IOException {66if(read(buf1, 0, 1)==-1) return -1;67return(buf1[0]&0xFF);68}6970private byte[] buf = new byte[512];7172public int read(byte[] b, int off, int len) throws IOException {73if(compress){74deflater.setOutput(b, off, len);75while(true){76int datalen = in.read(buf, 0, buf.length);77if(datalen == -1) return -1;78deflater.setInput(buf, 0, datalen, true);79int err = deflater.deflate(flush);80if(deflater.next_out_index>0)81return deflater.next_out_index;82if(err == JZlib.Z_STREAM_END)83return 0;84if(err == JZlib.Z_STREAM_ERROR ||85err == JZlib.Z_DATA_ERROR){86throw new ZStreamException("deflating: "+deflater.msg);87}88}89}90else{91return iis.read(b, off, len);92}93}9495public long skip(long n) throws IOException {96int len=512;97if(n<len)98len=(int)n;99byte[] tmp=new byte[len];100return((long)read(tmp));101}102103public int getFlushMode() {104return flush;105}106107public void setFlushMode(int flush) {108this.flush=flush;109}110111public long getTotalIn() {112if(compress) return deflater.total_in;113else return iis.getTotalIn();114}115116public long getTotalOut() {117if(compress) return deflater.total_out;118else return iis.getTotalOut();119}120121public void close() throws IOException{122if(compress) deflater.end();123else iis.close();124}125}126127128