Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/GZIPInputStream.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 GZIPInputStream extends InflaterInputStream {3334public GZIPInputStream(InputStream in) throws IOException {35this(in, DEFAULT_BUFSIZE, true);36}3738public GZIPInputStream(InputStream in,39int size,40boolean close_in) throws IOException {41this(in, new Inflater(15+16), size, close_in);42myinflater = true;43}4445public GZIPInputStream(InputStream in,46Inflater inflater,47int size,48boolean close_in) throws IOException {49super(in, inflater, size, close_in);50}5152public long getModifiedtime() {53return inflater.istate.getGZIPHeader().getModifiedTime();54}5556public int getOS() {57return inflater.istate.getGZIPHeader().getOS();58}5960public String getName() {61return inflater.istate.getGZIPHeader().getName();62}6364public String getComment() {65return inflater.istate.getGZIPHeader().getComment();66}6768public long getCRC() throws GZIPException {69if(inflater.istate.mode != 12 /*DONE*/)70throw new GZIPException("checksum is not calculated yet.");71return inflater.istate.getGZIPHeader().getCRC();72}7374public void readHeader() throws IOException {7576byte[] empty = "".getBytes();77inflater.setOutput(empty, 0, 0);78inflater.setInput(empty, 0, 0, false);7980byte[] b = new byte[10];8182int n = fill(b);83if(n!=10){84if(n>0){85inflater.setInput(b, 0, n, false);86//inflater.next_in_index = n;87inflater.next_in_index = 0;88inflater.avail_in = n;89}90throw new IOException("no input");91}9293inflater.setInput(b, 0, n, false);9495byte[] b1 = new byte[1];96do{97if(inflater.avail_in<=0){98int i = in.read(b1);99if(i<=0)100throw new IOException("no input");101inflater.setInput(b1, 0, 1, true);102}103104int err = inflater.inflate(JZlib.Z_NO_FLUSH);105106if(err!=0/*Z_OK*/){107int len = 2048-inflater.next_in.length;108if(len>0){109byte[] tmp = new byte[len];110n = fill(tmp);111if(n>0){112inflater.avail_in += inflater.next_in_index;113inflater.next_in_index = 0;114inflater.setInput(tmp, 0, n, true);115}116}117//inflater.next_in_index = inflater.next_in.length;118inflater.avail_in += inflater.next_in_index;119inflater.next_in_index = 0;120throw new IOException(inflater.msg);121}122}123while(inflater.istate.inParsingHeader());124}125126private int fill(byte[] buf) {127int len = buf.length;128int n = 0;129do{130int i = -1;131try {132i = in.read(buf, n, buf.length - n);133}134catch(IOException e){135}136if(i == -1){137break;138}139n+=i;140}141while(n<len);142return n;143}144}145146