author | darcy |
Thu, 04 Dec 2014 12:59:43 -0800 | |
changeset 27804 | 4659e70271c4 |
parent 25859 | 3317bb8137f4 |
child 32649 | 2ee9017c7597 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
17457
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
27 |
||
28 |
import java.io.SequenceInputStream; |
|
29 |
import java.io.ByteArrayInputStream; |
|
17457
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
30 |
import java.io.FilterInputStream; |
2 | 31 |
import java.io.InputStream; |
32 |
import java.io.IOException; |
|
33 |
import java.io.EOFException; |
|
34 |
||
35 |
/** |
|
36 |
* This class implements a stream filter for reading compressed data in |
|
37 |
* the GZIP file format. |
|
38 |
* |
|
39 |
* @see InflaterInputStream |
|
40 |
* @author David Connelly |
|
41 |
* |
|
42 |
*/ |
|
43 |
public |
|
44 |
class GZIPInputStream extends InflaterInputStream { |
|
45 |
/** |
|
46 |
* CRC-32 for uncompressed data. |
|
47 |
*/ |
|
48 |
protected CRC32 crc = new CRC32(); |
|
49 |
||
50 |
/** |
|
51 |
* Indicates end of input stream. |
|
52 |
*/ |
|
53 |
protected boolean eos; |
|
54 |
||
55 |
private boolean closed = false; |
|
56 |
||
57 |
/** |
|
58 |
* Check to make sure that this stream has not been closed |
|
59 |
*/ |
|
60 |
private void ensureOpen() throws IOException { |
|
61 |
if (closed) { |
|
62 |
throw new IOException("Stream closed"); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Creates a new input stream with the specified buffer size. |
|
68 |
* @param in the input stream |
|
69 |
* @param size the input buffer size |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
70 |
* |
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
71 |
* @exception ZipException if a GZIP format error has occurred or the |
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
72 |
* compression method used is unsupported |
2 | 73 |
* @exception IOException if an I/O error has occurred |
18156 | 74 |
* @exception IllegalArgumentException if {@code size <= 0} |
2 | 75 |
*/ |
76 |
public GZIPInputStream(InputStream in, int size) throws IOException { |
|
77 |
super(in, new Inflater(true), size); |
|
78 |
usesDefaultInflater = true; |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
79 |
readHeader(in); |
2 | 80 |
} |
81 |
||
82 |
/** |
|
83 |
* Creates a new input stream with a default buffer size. |
|
84 |
* @param in the input stream |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
85 |
* |
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
86 |
* @exception ZipException if a GZIP format error has occurred or the |
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
87 |
* compression method used is unsupported |
2 | 88 |
* @exception IOException if an I/O error has occurred |
89 |
*/ |
|
90 |
public GZIPInputStream(InputStream in) throws IOException { |
|
91 |
this(in, 512); |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Reads uncompressed data into an array of bytes. If <code>len</code> is not |
|
96 |
* zero, the method will block until some input can be decompressed; otherwise, |
|
97 |
* no bytes are read and <code>0</code> is returned. |
|
98 |
* @param buf the buffer into which the data is read |
|
99 |
* @param off the start offset in the destination array <code>b</code> |
|
100 |
* @param len the maximum number of bytes read |
|
101 |
* @return the actual number of bytes read, or -1 if the end of the |
|
102 |
* compressed input stream is reached |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
103 |
* |
2 | 104 |
* @exception NullPointerException If <code>buf</code> is <code>null</code>. |
105 |
* @exception IndexOutOfBoundsException If <code>off</code> is negative, |
|
106 |
* <code>len</code> is negative, or <code>len</code> is greater than |
|
107 |
* <code>buf.length - off</code> |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
108 |
* @exception ZipException if the compressed input data is corrupt. |
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
109 |
* @exception IOException if an I/O error has occurred. |
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
110 |
* |
2 | 111 |
*/ |
112 |
public int read(byte[] buf, int off, int len) throws IOException { |
|
113 |
ensureOpen(); |
|
114 |
if (eos) { |
|
115 |
return -1; |
|
116 |
} |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
117 |
int n = super.read(buf, off, len); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
118 |
if (n == -1) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
119 |
if (readTrailer()) |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
120 |
eos = true; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
121 |
else |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
122 |
return this.read(buf, off, len); |
2 | 123 |
} else { |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
124 |
crc.update(buf, off, n); |
2 | 125 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
126 |
return n; |
2 | 127 |
} |
128 |
||
129 |
/** |
|
130 |
* Closes this input stream and releases any system resources associated |
|
131 |
* with the stream. |
|
132 |
* @exception IOException if an I/O error has occurred |
|
133 |
*/ |
|
134 |
public void close() throws IOException { |
|
135 |
if (!closed) { |
|
136 |
super.close(); |
|
137 |
eos = true; |
|
138 |
closed = true; |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* GZIP header magic number. |
|
144 |
*/ |
|
145 |
public final static int GZIP_MAGIC = 0x8b1f; |
|
146 |
||
147 |
/* |
|
148 |
* File header flags. |
|
149 |
*/ |
|
150 |
private final static int FTEXT = 1; // Extra text |
|
151 |
private final static int FHCRC = 2; // Header CRC |
|
152 |
private final static int FEXTRA = 4; // Extra field |
|
153 |
private final static int FNAME = 8; // File name |
|
154 |
private final static int FCOMMENT = 16; // File comment |
|
155 |
||
156 |
/* |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
157 |
* Reads GZIP member header and returns the total byte number |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
158 |
* of this member header. |
2 | 159 |
*/ |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
160 |
private int readHeader(InputStream this_in) throws IOException { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
161 |
CheckedInputStream in = new CheckedInputStream(this_in, crc); |
2 | 162 |
crc.reset(); |
163 |
// Check header magic |
|
164 |
if (readUShort(in) != GZIP_MAGIC) { |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
165 |
throw new ZipException("Not in GZIP format"); |
2 | 166 |
} |
167 |
// Check compression method |
|
168 |
if (readUByte(in) != 8) { |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
169 |
throw new ZipException("Unsupported compression method"); |
2 | 170 |
} |
171 |
// Read flags |
|
172 |
int flg = readUByte(in); |
|
173 |
// Skip MTIME, XFL, and OS fields |
|
174 |
skipBytes(in, 6); |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
175 |
int n = 2 + 2 + 6; |
2 | 176 |
// Skip optional extra field |
177 |
if ((flg & FEXTRA) == FEXTRA) { |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
178 |
int m = readUShort(in); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
179 |
skipBytes(in, m); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
180 |
n += m + 2; |
2 | 181 |
} |
182 |
// Skip optional file name |
|
183 |
if ((flg & FNAME) == FNAME) { |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
184 |
do { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
185 |
n++; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
186 |
} while (readUByte(in) != 0); |
2 | 187 |
} |
188 |
// Skip optional file comment |
|
189 |
if ((flg & FCOMMENT) == FCOMMENT) { |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
190 |
do { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
191 |
n++; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
192 |
} while (readUByte(in) != 0); |
2 | 193 |
} |
194 |
// Check optional header CRC |
|
195 |
if ((flg & FHCRC) == FHCRC) { |
|
196 |
int v = (int)crc.getValue() & 0xffff; |
|
197 |
if (readUShort(in) != v) { |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
198 |
throw new ZipException("Corrupt GZIP header"); |
2 | 199 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
200 |
n += 2; |
2 | 201 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
202 |
crc.reset(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
203 |
return n; |
2 | 204 |
} |
205 |
||
206 |
/* |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
207 |
* Reads GZIP member trailer and returns true if the eos |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
208 |
* reached, false if there are more (concatenated gzip |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
209 |
* data set) |
2 | 210 |
*/ |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
211 |
private boolean readTrailer() throws IOException { |
2 | 212 |
InputStream in = this.in; |
213 |
int n = inf.getRemaining(); |
|
214 |
if (n > 0) { |
|
215 |
in = new SequenceInputStream( |
|
17457
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
216 |
new ByteArrayInputStream(buf, len - n, n), |
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
217 |
new FilterInputStream(in) { |
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
218 |
public void close() throws IOException {} |
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
219 |
}); |
2 | 220 |
} |
221 |
// Uses left-to-right evaluation order |
|
222 |
if ((readUInt(in) != crc.getValue()) || |
|
223 |
// rfc1952; ISIZE is the input size modulo 2^32 |
|
224 |
(readUInt(in) != (inf.getBytesWritten() & 0xffffffffL))) |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
225 |
throw new ZipException("Corrupt GZIP trailer"); |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
226 |
|
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
227 |
// If there are more bytes available in "in" or |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
228 |
// the leftover in the "inf" is > 26 bytes: |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
229 |
// this.trailer(8) + next.header.min(10) + next.trailer(8) |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
230 |
// try concatenated case |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
231 |
if (this.in.available() > 0 || n > 26) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
232 |
int m = 8; // this.trailer |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
233 |
try { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
234 |
m += readHeader(in); // next.header |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
235 |
} catch (IOException ze) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
236 |
return true; // ignore any malformed, do nothing |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
237 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
238 |
inf.reset(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
239 |
if (n > m) |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
240 |
inf.setInput(buf, len - n + m, n - m); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
241 |
return false; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
242 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
243 |
return true; |
2 | 244 |
} |
245 |
||
246 |
/* |
|
247 |
* Reads unsigned integer in Intel byte order. |
|
248 |
*/ |
|
249 |
private long readUInt(InputStream in) throws IOException { |
|
250 |
long s = readUShort(in); |
|
251 |
return ((long)readUShort(in) << 16) | s; |
|
252 |
} |
|
253 |
||
254 |
/* |
|
255 |
* Reads unsigned short in Intel byte order. |
|
256 |
*/ |
|
257 |
private int readUShort(InputStream in) throws IOException { |
|
258 |
int b = readUByte(in); |
|
13158
f628cbfcbfa0
7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents:
5627
diff
changeset
|
259 |
return (readUByte(in) << 8) | b; |
2 | 260 |
} |
261 |
||
262 |
/* |
|
263 |
* Reads unsigned byte. |
|
264 |
*/ |
|
265 |
private int readUByte(InputStream in) throws IOException { |
|
266 |
int b = in.read(); |
|
267 |
if (b == -1) { |
|
268 |
throw new EOFException(); |
|
269 |
} |
|
270 |
if (b < -1 || b > 255) { |
|
271 |
// Report on this.in, not argument in; see read{Header, Trailer}. |
|
272 |
throw new IOException(this.in.getClass().getName() |
|
273 |
+ ".read() returned value out of range -1..255: " + b); |
|
274 |
} |
|
275 |
return b; |
|
276 |
} |
|
277 |
||
278 |
private byte[] tmpbuf = new byte[128]; |
|
279 |
||
280 |
/* |
|
281 |
* Skips bytes of input data blocking until all bytes are skipped. |
|
282 |
* Does not assume that the input stream is capable of seeking. |
|
283 |
*/ |
|
284 |
private void skipBytes(InputStream in, int n) throws IOException { |
|
285 |
while (n > 0) { |
|
286 |
int len = in.read(tmpbuf, 0, n < tmpbuf.length ? n : tmpbuf.length); |
|
287 |
if (len == -1) { |
|
288 |
throw new EOFException(); |
|
289 |
} |
|
290 |
n -= len; |
|
291 |
} |
|
292 |
} |
|
293 |
} |