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