1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/*
Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcraft.jzlib;
import java.io.*;
public class InflaterInputStream extends FilterInputStream {
protected final Inflater inflater;
protected byte[] buf;
private boolean closed = false;
private boolean eof = false;
private boolean close_in = true;
protected static final int DEFAULT_BUFSIZE = 512;
public InflaterInputStream(InputStream in) throws IOException {
this(in, false);
}
public InflaterInputStream(InputStream in, boolean nowrap) throws IOException {
this(in, new Inflater(nowrap));
myinflater = true;
}
public InflaterInputStream(InputStream in, Inflater inflater) throws IOException {
this(in, inflater, DEFAULT_BUFSIZE);
}
public InflaterInputStream(InputStream in,
Inflater inflater, int size) throws IOException {
this(in, inflater, size, true);
}
public InflaterInputStream(InputStream in,
Inflater inflater,
int size, boolean close_in) throws IOException {
super(in);
if (in == null || inflater == null) {
throw new NullPointerException();
}
else if (size <= 0) {
throw new IllegalArgumentException("buffer size must be greater than 0");
}
this.inflater = inflater;
buf = new byte[size];
this.close_in = close_in;
}
protected boolean myinflater = false;
private byte[] byte1 = new byte[1];
public int read() throws IOException {
if (closed) { throw new IOException("Stream closed"); }
return read(byte1, 0, 1) == -1 ? -1 : byte1[0] & 0xff;
}
public int read(byte[] b, int off, int len) throws IOException {
if (closed) { throw new IOException("Stream closed"); }
if (b == null) {
throw new NullPointerException();
}
else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
else if (len == 0) {
return 0;
}
else if (eof) {
return -1;
}
int n = 0;
inflater.setOutput(b, off, len);
while(!eof) {
if(inflater.avail_in==0)
fill();
int err = inflater.inflate(JZlib.Z_NO_FLUSH);
n += inflater.next_out_index - off;
off = inflater.next_out_index;
switch(err) {
case JZlib.Z_DATA_ERROR:
throw new IOException(inflater.msg);
case JZlib.Z_STREAM_END:
case JZlib.Z_NEED_DICT:
eof = true;
if(err == JZlib.Z_NEED_DICT)
return -1;
break;
default:
}
if(inflater.avail_out==0)
break;
}
return n;
}
public int available() throws IOException {
if (closed) { throw new IOException("Stream closed"); }
if (eof) {
return 0;
}
else {
return 1;
}
}
private byte[] b = new byte[512];
public long skip(long n) throws IOException {
if (n < 0) {
throw new IllegalArgumentException("negative skip length");
}
if (closed) { throw new IOException("Stream closed"); }
int max = (int)Math.min(n, Integer.MAX_VALUE);
int total = 0;
while (total < max) {
int len = max - total;
if (len > b.length) {
len = b.length;
}
len = read(b, 0, len);
if (len == -1) {
eof = true;
break;
}
total += len;
}
return total;
}
public void close() throws IOException {
if (!closed) {
if (myinflater)
inflater.end();
if(close_in)
in.close();
closed = true;
}
}
protected void fill() throws IOException {
if (closed) { throw new IOException("Stream closed"); }
int len = in.read(buf, 0, buf.length);
if (len == -1) {
if(inflater.istate.wrap == 0 &&
!inflater.finished()){
buf[0]=0;
len=1;
}
else if(inflater.istate.was != -1){ // in reading trailer
throw new IOException("footer is not found");
}
else{
throw new EOFException("Unexpected end of ZLIB input stream");
}
}
inflater.setInput(buf, 0, len, true);
}
public boolean markSupported() {
return false;
}
public synchronized void mark(int readlimit) {
}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
public long getTotalIn() {
return inflater.getTotalIn();
}
public long getTotalOut() {
return inflater.getTotalOut();
}
public byte[] getAvailIn() {
if(inflater.avail_in<=0)
return null;
byte[] tmp = new byte[inflater.avail_in];
System.arraycopy(inflater.next_in, inflater.next_in_index,
tmp, 0, inflater.avail_in);
return tmp;
}
public void readHeader() throws IOException {
byte[] empty = "".getBytes();
inflater.setInput(empty, 0, 0, false);
inflater.setOutput(empty, 0, 0);
int err = inflater.inflate(JZlib.Z_NO_FLUSH);
if(!inflater.istate.inParsingHeader()){
return;
}
byte[] b1 = new byte[1];
do{
int i = in.read(b1);
if(i<=0)
throw new IOException("no input");
inflater.setInput(b1);
err = inflater.inflate(JZlib.Z_NO_FLUSH);
if(err!=0/*Z_OK*/)
throw new IOException(inflater.msg);
}
while(inflater.istate.inParsingHeader());
}
public Inflater getInflater(){
return inflater;
}
}
|