comparison app/src/main/java/com/jcraft/jzlib/GZIPInputStream.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/jcraft/jzlib/GZIPInputStream.java@46c2115ae1c8
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
14
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 package com.jcraft.jzlib;
31 import java.io.*;
32
33 public class GZIPInputStream extends InflaterInputStream {
34
35 public GZIPInputStream(InputStream in) throws IOException {
36 this(in, DEFAULT_BUFSIZE, true);
37 }
38
39 public GZIPInputStream(InputStream in,
40 int size,
41 boolean close_in) throws IOException {
42 this(in, new Inflater(15+16), size, close_in);
43 myinflater = true;
44 }
45
46 public GZIPInputStream(InputStream in,
47 Inflater inflater,
48 int size,
49 boolean close_in) throws IOException {
50 super(in, inflater, size, close_in);
51 }
52
53 public long getModifiedtime() {
54 return inflater.istate.getGZIPHeader().getModifiedTime();
55 }
56
57 public int getOS() {
58 return inflater.istate.getGZIPHeader().getOS();
59 }
60
61 public String getName() {
62 return inflater.istate.getGZIPHeader().getName();
63 }
64
65 public String getComment() {
66 return inflater.istate.getGZIPHeader().getComment();
67 }
68
69 public long getCRC() throws GZIPException {
70 if(inflater.istate.mode != 12 /*DONE*/)
71 throw new GZIPException("checksum is not calculated yet.");
72 return inflater.istate.getGZIPHeader().getCRC();
73 }
74
75 public void readHeader() throws IOException {
76
77 byte[] empty = "".getBytes();
78 inflater.setOutput(empty, 0, 0);
79 inflater.setInput(empty, 0, 0, false);
80
81 byte[] b = new byte[10];
82
83 int n = fill(b);
84 if(n!=10){
85 if(n>0){
86 inflater.setInput(b, 0, n, false);
87 //inflater.next_in_index = n;
88 inflater.next_in_index = 0;
89 inflater.avail_in = n;
90 }
91 throw new IOException("no input");
92 }
93
94 inflater.setInput(b, 0, n, false);
95
96 byte[] b1 = new byte[1];
97 do{
98 if(inflater.avail_in<=0){
99 int i = in.read(b1);
100 if(i<=0)
101 throw new IOException("no input");
102 inflater.setInput(b1, 0, 1, true);
103 }
104
105 int err = inflater.inflate(JZlib.Z_NO_FLUSH);
106
107 if(err!=0/*Z_OK*/){
108 int len = 2048-inflater.next_in.length;
109 if(len>0){
110 byte[] tmp = new byte[len];
111 n = fill(tmp);
112 if(n>0){
113 inflater.avail_in += inflater.next_in_index;
114 inflater.next_in_index = 0;
115 inflater.setInput(tmp, 0, n, true);
116 }
117 }
118 //inflater.next_in_index = inflater.next_in.length;
119 inflater.avail_in += inflater.next_in_index;
120 inflater.next_in_index = 0;
121 throw new IOException(inflater.msg);
122 }
123 }
124 while(inflater.istate.inParsingHeader());
125 }
126
127 private int fill(byte[] buf) {
128 int len = buf.length;
129 int n = 0;
130 do{
131 int i = -1;
132 try {
133 i = in.read(buf, n, buf.length - n);
134 }
135 catch(IOException e){
136 }
137 if(i == -1){
138 break;
139 }
140 n+=i;
141 }
142 while(n<len);
143 return n;
144 }
145 }