Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/InflaterInputStream.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 InflaterInputStream extends FilterInputStream {33protected final Inflater inflater;34protected byte[] buf;3536private boolean closed = false;3738private boolean eof = false;3940private boolean close_in = true;4142protected static final int DEFAULT_BUFSIZE = 512;4344public InflaterInputStream(InputStream in) throws IOException {45this(in, false);46}4748public InflaterInputStream(InputStream in, boolean nowrap) throws IOException {49this(in, new Inflater(nowrap));50myinflater = true;51}5253public InflaterInputStream(InputStream in, Inflater inflater) throws IOException {54this(in, inflater, DEFAULT_BUFSIZE);55}5657public InflaterInputStream(InputStream in,58Inflater inflater, int size) throws IOException {59this(in, inflater, size, true);60}6162public InflaterInputStream(InputStream in,63Inflater inflater,64int size, boolean close_in) throws IOException {65super(in);66if (in == null || inflater == null) {67throw new NullPointerException();68}69else if (size <= 0) {70throw new IllegalArgumentException("buffer size must be greater than 0");71}72this.inflater = inflater;73buf = new byte[size];74this.close_in = close_in;75}7677protected boolean myinflater = false;7879private byte[] byte1 = new byte[1];8081public int read() throws IOException {82if (closed) { throw new IOException("Stream closed"); }83return read(byte1, 0, 1) == -1 ? -1 : byte1[0] & 0xff;84}8586public int read(byte[] b, int off, int len) throws IOException {87if (closed) { throw new IOException("Stream closed"); }88if (b == null) {89throw new NullPointerException();90}91else if (off < 0 || len < 0 || len > b.length - off) {92throw new IndexOutOfBoundsException();93}94else if (len == 0) {95return 0;96}97else if (eof) {98return -1;99}100101int n = 0;102inflater.setOutput(b, off, len);103while(!eof) {104if(inflater.avail_in==0)105fill();106int err = inflater.inflate(JZlib.Z_NO_FLUSH);107n += inflater.next_out_index - off;108off = inflater.next_out_index;109switch(err) {110case JZlib.Z_DATA_ERROR:111throw new IOException(inflater.msg);112case JZlib.Z_STREAM_END:113case JZlib.Z_NEED_DICT:114eof = true;115if(err == JZlib.Z_NEED_DICT)116return -1;117break;118default:119}120if(inflater.avail_out==0)121break;122}123return n;124}125126public int available() throws IOException {127if (closed) { throw new IOException("Stream closed"); }128if (eof) {129return 0;130}131else {132return 1;133}134}135136private byte[] b = new byte[512];137138public long skip(long n) throws IOException {139if (n < 0) {140throw new IllegalArgumentException("negative skip length");141}142143if (closed) { throw new IOException("Stream closed"); }144145int max = (int)Math.min(n, Integer.MAX_VALUE);146int total = 0;147while (total < max) {148int len = max - total;149if (len > b.length) {150len = b.length;151}152len = read(b, 0, len);153if (len == -1) {154eof = true;155break;156}157total += len;158}159return total;160}161162public void close() throws IOException {163if (!closed) {164if (myinflater)165inflater.end();166if(close_in)167in.close();168closed = true;169}170}171172protected void fill() throws IOException {173if (closed) { throw new IOException("Stream closed"); }174int len = in.read(buf, 0, buf.length);175if (len == -1) {176if(inflater.istate.wrap == 0 &&177!inflater.finished()){178buf[0]=0;179len=1;180}181else if(inflater.istate.was != -1){ // in reading trailer182throw new IOException("footer is not found");183}184else{185throw new EOFException("Unexpected end of ZLIB input stream");186}187}188inflater.setInput(buf, 0, len, true);189}190191public boolean markSupported() {192return false;193}194195public synchronized void mark(int readlimit) {196}197198public synchronized void reset() throws IOException {199throw new IOException("mark/reset not supported");200}201202public long getTotalIn() {203return inflater.getTotalIn();204}205206public long getTotalOut() {207return inflater.getTotalOut();208}209210public byte[] getAvailIn() {211if(inflater.avail_in<=0)212return null;213byte[] tmp = new byte[inflater.avail_in];214System.arraycopy(inflater.next_in, inflater.next_in_index,215tmp, 0, inflater.avail_in);216return tmp;217}218219public void readHeader() throws IOException {220221byte[] empty = "".getBytes();222inflater.setInput(empty, 0, 0, false);223inflater.setOutput(empty, 0, 0);224225int err = inflater.inflate(JZlib.Z_NO_FLUSH);226if(!inflater.istate.inParsingHeader()){227return;228}229230byte[] b1 = new byte[1];231do{232int i = in.read(b1);233if(i<=0)234throw new IOException("no input");235inflater.setInput(b1);236err = inflater.inflate(JZlib.Z_NO_FLUSH);237if(err!=0/*Z_OK*/)238throw new IOException(inflater.msg);239}240while(inflater.istate.inParsingHeader());241}242243public Inflater getInflater(){244return inflater;245}246}247248