FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / util / zip / GZIPInputStream.java
blob68fda79dc88b1fde2a1f0b24cb106769c74ce55f
1 /* GZIPInputStream.java - Input filter for reading gzip file
2 Copyright (C) 1999, 2000, 2002 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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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
24 combination.
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. */
38 package java.util.zip;
40 import java.io.InputStream;
41 import java.io.IOException;
43 /**
44 * @author Tom Tromey
45 * @date May 17, 1999
48 /* Written using on-line Java Platform 1.2 API Specification
49 * and JCL book.
50 * Believed complete and correct.
53 public class GZIPInputStream extends InflaterInputStream
55 public static final int GZIP_MAGIC = 0x8b1f;
57 public void close () throws IOException
59 // Nothing to do here.
60 super.close();
63 public GZIPInputStream (InputStream istream) throws IOException
65 this (istream, 512);
68 private final int eof_read () throws IOException
70 int r = in.read();
71 if (r == -1)
72 throw new ZipException ("gzip header corrupted");
73 return r & 0xff;
76 public GZIPInputStream (InputStream istream, int readsize)
77 throws IOException
79 super (istream, new Inflater (true), readsize);
81 // NOTE: header reading code taken from zlib's gzio.c.
83 // Read the magic number.
84 int magic = eof_read () | (eof_read () << 8);
85 if (magic != GZIP_MAGIC)
86 throw new ZipException ("gzip header corrupted");
88 int method = eof_read ();
89 int flags = eof_read ();
90 // Test from zlib.
91 if (method != Z_DEFLATED || (flags & RESERVED) != 0)
92 throw new ZipException ("gzip header corrupted");
94 // Discard time, xflags, OS code.
95 for (int i = 0; i < 6; ++i)
96 eof_read ();
98 // Skip the extra field.
99 if ((flags & EXTRA_FIELD) != 0)
101 int len = eof_read () | (eof_read () << 8);
102 while (len-- != 0)
103 eof_read ();
106 if ((flags & ORIG_NAME) != 0)
108 while (true)
110 int c = eof_read ();
111 if (c == 0)
112 break;
116 if ((flags & COMMENT) != 0)
118 while (true)
120 int c = eof_read ();
121 if (c == 0)
122 break;
126 if ((flags & HEAD_CRC) != 0)
128 // FIXME: consider checking CRC of the header.
129 eof_read ();
130 eof_read ();
133 crc = new CRC32 ();
136 public int read (byte[] buf, int off, int len) throws IOException
138 if (eos)
139 return -1;
140 int r = super.read(buf, off, len);
141 if (r == -1)
143 eos = true;
145 byte[] tmp = new byte[8];
146 // First copy remaining bytes from inflater input buffer.
147 int avail = inf.getRemaining ();
148 System.arraycopy (this.buf, this.len - avail, tmp, 0, avail);
150 // Now read remaining bytes from wrapped input stream.
151 for (int i = avail; i < 8; ++i)
153 tmp[i] = (byte) eof_read ();
156 int header_crc = read4 (tmp, 0);
157 if (crc.getValue() != header_crc)
158 throw new ZipException ("corrupted gzip file - crc mismatch");
159 int isize = read4 (tmp, 4);
160 if (inf.getTotalOut() != isize)
161 throw new ZipException ("corrupted gzip file - size mismatch");
162 return -1;
164 crc.update(buf, off, r);
165 return r;
168 private final int read4 (byte[] buf, int offset) throws IOException
170 return (((buf[offset + 3] & 0xFF) << 24) + ((buf[offset + 2] & 0xFF) << 16)
171 + ((buf[offset + 1] & 0xFF) << 8) + (buf[offset] & 0xFF));
174 // Checksum used by this input stream.
175 protected CRC32 crc;
177 // Indicates whether end-of-stream has been reached.
178 protected boolean eos;
180 // Some constants from zlib.
181 static final int Z_DEFLATED = 8;
182 static final int HEAD_CRC = 0x02;
183 static final int EXTRA_FIELD = 0x04;
184 static final int ORIG_NAME = 0x08;
185 static final int COMMENT = 0x10;
186 static final int RESERVED = 0xe0;