author | jboes |
Fri, 20 Sep 2019 11:07:52 +0100 | |
changeset 58242 | 94bb65cb37d3 |
parent 47216 | 71c04702a3d5 |
child 58288 | 48e480e56aad |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, 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 |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
32649
diff
changeset
|
41 |
* @since 1.1 |
2 | 42 |
* |
43 |
*/ |
|
44 |
public |
|
45 |
class GZIPInputStream extends InflaterInputStream { |
|
46 |
/** |
|
47 |
* CRC-32 for uncompressed data. |
|
48 |
*/ |
|
49 |
protected CRC32 crc = new CRC32(); |
|
50 |
||
51 |
/** |
|
52 |
* Indicates end of input stream. |
|
53 |
*/ |
|
54 |
protected boolean eos; |
|
55 |
||
56 |
private boolean closed = false; |
|
57 |
||
58 |
/** |
|
59 |
* Check to make sure that this stream has not been closed |
|
60 |
*/ |
|
61 |
private void ensureOpen() throws IOException { |
|
62 |
if (closed) { |
|
63 |
throw new IOException("Stream closed"); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* Creates a new input stream with the specified buffer size. |
|
69 |
* @param in the input stream |
|
70 |
* @param size the input buffer size |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
71 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
72 |
* @throws ZipException if a GZIP format error has occurred or the |
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
73 |
* compression method used is unsupported |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
74 |
* @throws IOException if an I/O error has occurred |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
75 |
* @throws IllegalArgumentException if {@code size <= 0} |
2 | 76 |
*/ |
77 |
public GZIPInputStream(InputStream in, int size) throws IOException { |
|
78 |
super(in, new Inflater(true), size); |
|
79 |
usesDefaultInflater = true; |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
80 |
readHeader(in); |
2 | 81 |
} |
82 |
||
83 |
/** |
|
84 |
* Creates a new input stream with a default buffer size. |
|
85 |
* @param in the input stream |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
86 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
87 |
* @throws ZipException if a GZIP format error has occurred or the |
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
88 |
* compression method used is unsupported |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
89 |
* @throws IOException if an I/O error has occurred |
2 | 90 |
*/ |
91 |
public GZIPInputStream(InputStream in) throws IOException { |
|
92 |
this(in, 512); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Reads uncompressed data into an array of bytes. If <code>len</code> is not |
|
97 |
* zero, the method will block until some input can be decompressed; otherwise, |
|
98 |
* no bytes are read and <code>0</code> is returned. |
|
99 |
* @param buf the buffer into which the data is read |
|
100 |
* @param off the start offset in the destination array <code>b</code> |
|
101 |
* @param len the maximum number of bytes read |
|
102 |
* @return the actual number of bytes read, or -1 if the end of the |
|
103 |
* compressed input stream is reached |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
104 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
105 |
* @throws NullPointerException If <code>buf</code> is <code>null</code>. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
106 |
* @throws IndexOutOfBoundsException If <code>off</code> is negative, |
2 | 107 |
* <code>len</code> is negative, or <code>len</code> is greater than |
108 |
* <code>buf.length - off</code> |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
109 |
* @throws ZipException if the compressed input data is corrupt. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
110 |
* @throws IOException if an I/O error has occurred. |
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
111 |
* |
2 | 112 |
*/ |
113 |
public int read(byte[] buf, int off, int len) throws IOException { |
|
114 |
ensureOpen(); |
|
115 |
if (eos) { |
|
116 |
return -1; |
|
117 |
} |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
118 |
int n = super.read(buf, off, len); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
119 |
if (n == -1) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
120 |
if (readTrailer()) |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
121 |
eos = true; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
122 |
else |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
123 |
return this.read(buf, off, len); |
2 | 124 |
} else { |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
125 |
crc.update(buf, off, n); |
2 | 126 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
127 |
return n; |
2 | 128 |
} |
129 |
||
130 |
/** |
|
131 |
* Closes this input stream and releases any system resources associated |
|
132 |
* with the stream. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
133 |
* @throws IOException if an I/O error has occurred |
2 | 134 |
*/ |
135 |
public void close() throws IOException { |
|
136 |
if (!closed) { |
|
137 |
super.close(); |
|
138 |
eos = true; |
|
139 |
closed = true; |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* GZIP header magic number. |
|
145 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
146 |
public static final int GZIP_MAGIC = 0x8b1f; |
2 | 147 |
|
148 |
/* |
|
149 |
* File header flags. |
|
150 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
151 |
private static final int FTEXT = 1; // Extra text |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
152 |
private static final int FHCRC = 2; // Header CRC |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
153 |
private static final int FEXTRA = 4; // Extra field |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
154 |
private static final int FNAME = 8; // File name |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
155 |
private static final int FCOMMENT = 16; // File comment |
2 | 156 |
|
157 |
/* |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
158 |
* Reads GZIP member header and returns the total byte number |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
159 |
* of this member header. |
2 | 160 |
*/ |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
161 |
private int readHeader(InputStream this_in) throws IOException { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
162 |
CheckedInputStream in = new CheckedInputStream(this_in, crc); |
2 | 163 |
crc.reset(); |
164 |
// Check header magic |
|
165 |
if (readUShort(in) != GZIP_MAGIC) { |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
166 |
throw new ZipException("Not in GZIP format"); |
2 | 167 |
} |
168 |
// Check compression method |
|
169 |
if (readUByte(in) != 8) { |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
170 |
throw new ZipException("Unsupported compression method"); |
2 | 171 |
} |
172 |
// Read flags |
|
173 |
int flg = readUByte(in); |
|
174 |
// Skip MTIME, XFL, and OS fields |
|
175 |
skipBytes(in, 6); |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
176 |
int n = 2 + 2 + 6; |
2 | 177 |
// Skip optional extra field |
178 |
if ((flg & FEXTRA) == FEXTRA) { |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
179 |
int m = readUShort(in); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
180 |
skipBytes(in, m); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
181 |
n += m + 2; |
2 | 182 |
} |
183 |
// Skip optional file name |
|
184 |
if ((flg & FNAME) == FNAME) { |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
185 |
do { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
186 |
n++; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
187 |
} while (readUByte(in) != 0); |
2 | 188 |
} |
189 |
// Skip optional file comment |
|
190 |
if ((flg & FCOMMENT) == FCOMMENT) { |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
191 |
do { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
192 |
n++; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
193 |
} while (readUByte(in) != 0); |
2 | 194 |
} |
195 |
// Check optional header CRC |
|
196 |
if ((flg & FHCRC) == FHCRC) { |
|
197 |
int v = (int)crc.getValue() & 0xffff; |
|
198 |
if (readUShort(in) != v) { |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
199 |
throw new ZipException("Corrupt GZIP header"); |
2 | 200 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
201 |
n += 2; |
2 | 202 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
203 |
crc.reset(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
204 |
return n; |
2 | 205 |
} |
206 |
||
207 |
/* |
|
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
208 |
* Reads GZIP member trailer and returns true if the eos |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
209 |
* reached, false if there are more (concatenated gzip |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
210 |
* data set) |
2 | 211 |
*/ |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
212 |
private boolean readTrailer() throws IOException { |
2 | 213 |
InputStream in = this.in; |
214 |
int n = inf.getRemaining(); |
|
215 |
if (n > 0) { |
|
216 |
in = new SequenceInputStream( |
|
17457
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
217 |
new ByteArrayInputStream(buf, len - n, n), |
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
218 |
new FilterInputStream(in) { |
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
219 |
public void close() throws IOException {} |
2046a488d605
7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents:
14342
diff
changeset
|
220 |
}); |
2 | 221 |
} |
222 |
// Uses left-to-right evaluation order |
|
223 |
if ((readUInt(in) != crc.getValue()) || |
|
224 |
// rfc1952; ISIZE is the input size modulo 2^32 |
|
225 |
(readUInt(in) != (inf.getBytesWritten() & 0xffffffffL))) |
|
5605
bba86030a87b
4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents:
2
diff
changeset
|
226 |
throw new ZipException("Corrupt GZIP trailer"); |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
227 |
|
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
228 |
// If there are more bytes available in "in" or |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
229 |
// the leftover in the "inf" is > 26 bytes: |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
230 |
// this.trailer(8) + next.header.min(10) + next.trailer(8) |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
231 |
// try concatenated case |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
232 |
if (this.in.available() > 0 || n > 26) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
233 |
int m = 8; // this.trailer |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
234 |
try { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
235 |
m += readHeader(in); // next.header |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
236 |
} catch (IOException ze) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
237 |
return true; // ignore any malformed, do nothing |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
238 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
239 |
inf.reset(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
240 |
if (n > m) |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
241 |
inf.setInput(buf, len - n + m, n - m); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
242 |
return false; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
243 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
5605
diff
changeset
|
244 |
return true; |
2 | 245 |
} |
246 |
||
247 |
/* |
|
248 |
* Reads unsigned integer in Intel byte order. |
|
249 |
*/ |
|
250 |
private long readUInt(InputStream in) throws IOException { |
|
251 |
long s = readUShort(in); |
|
252 |
return ((long)readUShort(in) << 16) | s; |
|
253 |
} |
|
254 |
||
255 |
/* |
|
256 |
* Reads unsigned short in Intel byte order. |
|
257 |
*/ |
|
258 |
private int readUShort(InputStream in) throws IOException { |
|
259 |
int b = readUByte(in); |
|
13158
f628cbfcbfa0
7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents:
5627
diff
changeset
|
260 |
return (readUByte(in) << 8) | b; |
2 | 261 |
} |
262 |
||
263 |
/* |
|
264 |
* Reads unsigned byte. |
|
265 |
*/ |
|
266 |
private int readUByte(InputStream in) throws IOException { |
|
267 |
int b = in.read(); |
|
268 |
if (b == -1) { |
|
269 |
throw new EOFException(); |
|
270 |
} |
|
271 |
if (b < -1 || b > 255) { |
|
272 |
// Report on this.in, not argument in; see read{Header, Trailer}. |
|
273 |
throw new IOException(this.in.getClass().getName() |
|
274 |
+ ".read() returned value out of range -1..255: " + b); |
|
275 |
} |
|
276 |
return b; |
|
277 |
} |
|
278 |
||
279 |
private byte[] tmpbuf = new byte[128]; |
|
280 |
||
281 |
/* |
|
282 |
* Skips bytes of input data blocking until all bytes are skipped. |
|
283 |
* Does not assume that the input stream is capable of seeking. |
|
284 |
*/ |
|
285 |
private void skipBytes(InputStream in, int n) throws IOException { |
|
286 |
while (n > 0) { |
|
287 |
int len = in.read(tmpbuf, 0, n < tmpbuf.length ? n : tmpbuf.length); |
|
288 |
if (len == -1) { |
|
289 |
throw new EOFException(); |
|
290 |
} |
|
291 |
n -= len; |
|
292 |
} |
|
293 |
} |
|
294 |
} |