1 /* GZIPInputStream.java - Input filter for reading gzip file
2 Copyright (C) 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java
.util
.zip
;
41 import java
.io
.EOFException
;
42 import java
.io
.IOException
;
43 import java
.io
.InputStream
;
46 * This filter stream is used to decompress a "GZIP" format stream.
47 * The "GZIP" format is described in RFC 1952.
53 public class GZIPInputStream
54 extends InflaterInputStream
57 * The magic number found at the start of a GZIP stream.
59 public static final int GZIP_MAGIC
= 0x8b1f;
62 * The mask for bit 0 of the flag byte.
64 static final int FTEXT
= 0x1;
67 * The mask for bit 1 of the flag byte.
69 static final int FHCRC
= 0x2;
72 * The mask for bit 2 of the flag byte.
74 static final int FEXTRA
= 0x4;
77 * The mask for bit 3 of the flag byte.
79 static final int FNAME
= 0x8;
82 * The mask for bit 4 of the flag byte.
84 static final int FCOMMENT
= 0x10;
87 * The CRC-32 checksum value for uncompressed data.
92 * Indicates whether or not the end of the stream has been reached.
94 protected boolean eos
;
97 * Indicates whether or not the GZIP header has been read in.
99 private boolean readGZIPHeader
;
102 * Creates a GZIPInputStream with the default buffer size.
104 * @param in The stream to read compressed data from
107 * @throws IOException if an error occurs during an I/O operation.
109 public GZIPInputStream(InputStream in
)
116 * Creates a GZIPInputStream with the specified buffer size.
118 * @param in The stream to read compressed data from
120 * @param size The size of the buffer to use.
122 * @throws IOException if an error occurs during an I/O operation.
123 * @throws IllegalArgumentException if <code>size</code>
124 * is less than or equal to 0.
126 public GZIPInputStream(InputStream in
, int size
)
129 super(in
, new Inflater(true), size
);
135 * Closes the input stream.
137 * @throws IOException if an error occurs during an I/O operation.
142 // Nothing to do here.
147 * Reads in GZIP-compressed data and stores it in uncompressed form
148 * into an array of bytes. The method will block until either
149 * enough input data becomes available or the compressed stream
152 * @param buf the buffer into which the uncompressed data will
154 * @param offset the offset indicating where in <code>buf</code>
155 * the uncompressed data should be placed.
156 * @param len the number of uncompressed bytes to be read.
158 public int read(byte[] buf
, int offset
, int len
) throws IOException
160 // We first have to slurp in the GZIP header, then we feed all the
161 // rest of the data to the superclass.
163 // As we do that we continually update the CRC32. Once the data is
164 // finished, we check the CRC32.
166 // This means we don't need our own buffer, as everything is done
167 // in the superclass.
174 // System.err.println("GZIPIS.read(byte[], off, len ... " + offset + " and len " + len);
176 /* We don't have to read the header,
177 * so we just grab data from the superclass.
179 int numRead
= super.read(buf
, offset
, len
);
181 crc
.update(buf
, offset
, numRead
);
190 * Reads in the GZIP header.
192 private void readHeader() throws IOException
194 /* 1. Check the two magic bytes */
195 CRC32 headCRC
= new CRC32();
196 int magic
= in
.read();
202 int magic2
= in
.read();
203 if ((magic
+ (magic2
<< 8)) != GZIP_MAGIC
)
204 throw new IOException("Error in GZIP header, bad magic code");
205 headCRC
.update(magic
);
206 headCRC
.update(magic2
);
208 /* 2. Check the compression type (must be 8) */
210 if (CM
!= Deflater
.DEFLATED
)
211 throw new IOException("Error in GZIP header, data not in deflate format");
214 /* 3. Check the flags */
215 int flags
= in
.read();
217 throw new EOFException("Early EOF in GZIP header");
218 headCRC
.update(flags
);
220 /* This flag byte is divided into individual bits as follows:
232 /* 3.1 Check the reserved bits are zero */
233 if ((flags
& 0xd0) != 0)
234 throw new IOException("Reserved flag bits in GZIP header != 0");
236 /* 4.-6. Skip the modification time, extra flags, and OS type */
237 for (int i
=0; i
< 6; i
++)
239 int readByte
= in
.read();
241 throw new EOFException("Early EOF in GZIP header");
242 headCRC
.update(readByte
);
245 /* 7. Read extra field */
246 if ((flags
& FEXTRA
) != 0)
248 /* Skip subfield id */
249 for (int i
=0; i
< 2; i
++)
251 int readByte
= in
.read();
253 throw new EOFException("Early EOF in GZIP header");
254 headCRC
.update(readByte
);
256 if (in
.read() < 0 || in
.read() < 0)
257 throw new EOFException("Early EOF in GZIP header");
259 int len1
, len2
, extraLen
;
262 if ((len1
< 0) || (len2
< 0))
263 throw new EOFException("Early EOF in GZIP header");
264 headCRC
.update(len1
);
265 headCRC
.update(len2
);
267 extraLen
= (len1
<< 8) | len2
;
268 for (int i
= 0; i
< extraLen
;i
++)
270 int readByte
= in
.read();
272 throw new EOFException("Early EOF in GZIP header");
273 headCRC
.update(readByte
);
277 /* 8. Read file name */
278 if ((flags
& FNAME
) != 0)
281 while ( (readByte
= in
.read()) > 0)
282 headCRC
.update(readByte
);
284 throw new EOFException("Early EOF in GZIP file name");
285 headCRC
.update(readByte
);
288 /* 9. Read comment */
289 if ((flags
& FCOMMENT
) != 0)
292 while ( (readByte
= in
.read()) > 0)
293 headCRC
.update(readByte
);
296 throw new EOFException("Early EOF in GZIP comment");
297 headCRC
.update(readByte
);
300 /* 10. Read header CRC */
301 if ((flags
& FHCRC
) != 0)
304 int crcval
= in
.read();
306 throw new EOFException("Early EOF in GZIP header");
308 tempByte
= in
.read();
310 throw new EOFException("Early EOF in GZIP header");
312 crcval
= (crcval
<< 8) | tempByte
;
313 if (crcval
!= ((int) headCRC
.getValue() & 0xffff))
314 throw new IOException("Header CRC value mismatch");
317 readGZIPHeader
= true;
318 //System.err.println("Read GZIP header");
321 private void readFooter() throws IOException
323 byte[] footer
= new byte[8];
324 int avail
= inf
.getRemaining();
327 System
.arraycopy(buf
, len
- inf
.getRemaining(), footer
, 0, avail
);
328 int needed
= 8 - avail
;
331 int count
= in
.read(footer
, 8-needed
, needed
);
333 throw new EOFException("Early EOF in GZIP footer");
334 needed
-= count
; //Jewel Jan 16
337 int crcval
= (footer
[0] & 0xff) | ((footer
[1] & 0xff) << 8)
338 | ((footer
[2] & 0xff) << 16) | (footer
[3] << 24);
339 if (crcval
!= (int) crc
.getValue())
340 throw new IOException("GZIP crc sum mismatch, theirs \""
341 + Integer
.toHexString(crcval
)
343 + Integer
.toHexString( (int) crc
.getValue()));
345 int total
= (footer
[4] & 0xff) | ((footer
[5] & 0xff) << 8)
346 | ((footer
[6] & 0xff) << 16) | (footer
[7] << 24);
347 if (total
!= inf
.getTotalOut())
348 throw new IOException("Number of bytes mismatch");
350 /* FIXME" XXX Should we support multiple members.
351 * Difficult, since there may be some bytes still in buf