DeflaterOutputStream.java, [...]: Reformatted.
[official-gcc.git] / libjava / java / util / zip / InflaterInputStream.java
blob60442e474afabf3e19be5b8f0f5f1ecf61d4e938
1 /* InflaterInputStream.java - Input stream filter for decompressing
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
39 package java.util.zip;
41 import java.io.FilterInputStream;
42 import java.io.InputStream;
43 import java.io.IOException;
45 /**
46 * This filter stream is used to decompress data compressed in the "deflate"
47 * format. The "deflate" format is described in RFC 1951.
49 * This stream may form the basis for other decompression filters, such
50 * as the <code>GZIPInputStream</code>.
52 * @author John Leuner
53 * @author Tom Tromey
54 * @since 1.1
56 public class InflaterInputStream extends FilterInputStream
58 /**
59 * Decompressor for this filter
61 protected Inflater inf;
63 /**
64 * Byte array used as a buffer
66 protected byte[] buf;
68 /**
69 * Size of buffer
71 protected int len;
74 /**
75 * Create an InflaterInputStream with the default decompresseor
76 * and a default buffer size.
78 * @param in the InputStream to read bytes from
80 public InflaterInputStream(InputStream in)
82 this(in, new Inflater(), 512);
85 /**
86 * Create an InflaterInputStream with the specified decompresseor
87 * and a default buffer size.
89 * @param in the InputStream to read bytes from
90 * @param inf the decompressor used to decompress data read from in
92 public InflaterInputStream(InputStream in, Inflater inf)
94 this(in, inf, 512);
97 /**
98 * Create an InflaterInputStream with the specified decompresseor
99 * and a specified buffer size.
101 * @param in the InputStream to read bytes from
102 * @param inf the decompressor used to decompress data read from in
103 * @param size size of the buffer to use
105 public InflaterInputStream(InputStream in, Inflater inf, int size)
107 super(in);
109 if (in == null)
110 throw new NullPointerException("in may not be null");
111 if (inf == null)
112 throw new NullPointerException("inf may not be null");
113 if (size < 0)
114 throw new IllegalArgumentException("size may not be negative");
116 this.inf = inf;
117 this.buf = new byte [size];
121 * Returns 0 once the end of the stream (EOF) has been reached.
122 * Otherwise returns 1.
124 public int available() throws IOException
126 // According to the JDK 1.2 docs, this should only ever return 0
127 // or 1 and should not be relied upon by Java programs.
128 if (inf == null)
129 throw new IOException("stream closed");
130 return inf.finished() ? 0 : 1;
134 * Closes the input stream
136 public synchronized void close() throws IOException
138 inf = null;
139 super.close();
143 * Fills the buffer with more data to decompress.
145 protected void fill() throws IOException
147 if (in == null)
148 throw new ZipException ("InflaterInputStream is closed");
150 len = in.read(buf, 0, buf.length);
152 if (len != -1)
153 inf.setInput(buf, 0, len);
157 * Decompresses data into the byte array
159 * @param b the array to read and decompress data into
160 * @param off the offset indicating where the data should be placed
161 * @param len the number of bytes to decompress
163 public int read(byte[] b, int off, int len) throws IOException
165 if (inf == null)
166 throw new IOException("stream closed");
167 if (len == 0)
168 return 0;
169 if (inf.finished())
170 return -1;
172 int count = 0;
173 while (count == 0)
175 if (inf.needsInput())
176 fill();
180 count = inf.inflate(b, off, len);
181 if (count == 0)
183 if (this.len == -1)
185 // Couldn't get any more data to feed to the Inflater
186 return -1;
188 if (inf.needsDictionary())
189 throw new ZipException("Inflater needs Dictionary");
192 catch (DataFormatException dfe)
194 throw new ZipException(dfe.getMessage());
197 return count;
201 * Skip specified number of bytes of uncompressed data
203 * @param n number of bytes to skip
205 public long skip(long n) throws IOException
207 if (inf == null)
208 throw new IOException("stream closed");
209 if (n < 0)
210 throw new IllegalArgumentException();
212 if (n == 0)
213 return 0;
215 int buflen = (int) Math.min(n, 1024);
216 byte[] tmpbuf = new byte[buflen];
218 long skipped = 0L;
219 while (n > 0L)
221 int numread = read(tmpbuf, 0, buflen);
222 if (numread == -1)
223 break;
224 n -= numread;
225 skipped += numread;
226 buflen = (int) Math.min(n, 1024);
229 return skipped;