OSDN Git Service

PR libgcj/24461:
[pf3gnuchains/gcc-fork.git] / libjava / java / util / zip / InflaterInputStream.java
1 /* InflaterInputStream.java - Input stream filter for decompressing
2    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3    Free Software Foundation, Inc.
4
5 This file is part of GNU Classpath.
6
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.
11  
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.
16
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21
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.
26
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. */
38
39
40 package java.util.zip;
41
42 import java.io.FilterInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45
46 /**
47  * This filter stream is used to decompress data compressed in the "deflate"
48  * format. The "deflate" format is described in RFC 1951.
49  *
50  * This stream may form the basis for other decompression filters, such
51  * as the <code>GZIPInputStream</code>.
52  *
53  * @author John Leuner
54  * @author Tom Tromey
55  * @since 1.1
56  */
57 public class InflaterInputStream extends FilterInputStream
58 {
59   /**
60    * Decompressor for this filter 
61    */
62   protected Inflater inf;
63
64   /**
65    * Byte array used as a buffer 
66    */
67   protected byte[] buf;
68
69   /**
70    * Size of buffer   
71    */
72   protected int len;
73
74   // We just use this if we are decoding one byte at a time with the
75   // read() call.
76   private byte[] onebytebuffer = new byte[1];
77
78   /**
79    * Create an InflaterInputStream with the default decompresseor
80    * and a default buffer size.
81    *
82    * @param in the InputStream to read bytes from
83    */
84   public InflaterInputStream(InputStream in) 
85   {
86     this(in, new Inflater(), 4096);
87   }
88
89   /**
90    * Create an InflaterInputStream with the specified decompresseor
91    * and a default buffer size.
92    *
93    * @param in the InputStream to read bytes from
94    * @param inf the decompressor used to decompress data read from in
95    */
96   public InflaterInputStream(InputStream in, Inflater inf) 
97   {
98     this(in, inf, 4096);
99   }
100
101   /**
102    * Create an InflaterInputStream with the specified decompresseor
103    * and a specified buffer size.
104    *
105    * @param in the InputStream to read bytes from
106    * @param inf the decompressor used to decompress data read from in
107    * @param size size of the buffer to use
108    */
109   public InflaterInputStream(InputStream in, Inflater inf, int size) 
110   {
111     super(in);
112
113     if (in == null)
114       throw new NullPointerException("in may not be null");
115     if (inf == null)
116       throw new NullPointerException("inf may not be null");
117     if (size < 0)
118       throw new IllegalArgumentException("size may not be negative");
119     
120     this.inf = inf;
121     this.buf = new byte [size];
122   }
123
124   /**
125    * Returns 0 once the end of the stream (EOF) has been reached.
126    * Otherwise returns 1.
127    */
128   public int available() throws IOException
129   {
130     // According to the JDK 1.2 docs, this should only ever return 0
131     // or 1 and should not be relied upon by Java programs.
132     if (inf == null)
133       throw new IOException("stream closed");
134     return inf.finished() ? 0 : 1;
135   }
136
137   /**
138    * Closes the input stream
139    */
140   public synchronized void close() throws IOException
141   {
142     inf = null;
143     super.close();
144   }
145
146   /**
147    * Fills the buffer with more data to decompress.
148    */
149   protected void fill() throws IOException
150   {
151     if (in == null)
152       throw new ZipException ("InflaterInputStream is closed");
153     
154     len = in.read(buf, 0, buf.length);
155
156     if (len < 0)
157       throw new ZipException("Deflated stream ends early.");
158     
159     inf.setInput(buf, 0, len);
160   }
161
162   /**
163    * Reads one byte of decompressed data.
164    *
165    * The byte is in the lower 8 bits of the int.
166    */
167   public int read() throws IOException
168   { 
169     int nread = read(onebytebuffer, 0, 1);
170     if (nread > 0)
171       return onebytebuffer[0] & 0xff;
172     return -1;
173   }
174
175   /**
176    * Decompresses data into the byte array
177    *
178    * @param b the array to read and decompress data into
179    * @param off the offset indicating where the data should be placed
180    * @param len the number of bytes to decompress
181    */
182   public int read(byte[] b, int off, int len) throws IOException
183   {
184     if (inf == null)
185       throw new IOException("stream closed");
186     if (len == 0)
187       return 0;
188     if (inf.finished())
189       return -1;
190
191     int count = 0;
192     while (count == 0)
193       {
194         if (inf.needsInput())
195           fill();
196         
197         try
198           {
199             count = inf.inflate(b, off, len);
200             if (count == 0)
201               {
202                 if (this.len == -1)
203                   {
204                     // Couldn't get any more data to feed to the Inflater
205                     return -1;
206                   }
207                 if (inf.needsDictionary())
208                   throw new ZipException("Inflater needs Dictionary");
209               }
210           } 
211         catch (DataFormatException dfe) 
212           {
213             throw new ZipException(dfe.getMessage());
214           }
215       }
216     return count;
217   }
218
219   /**
220    * Skip specified number of bytes of uncompressed data
221    *
222    * @param n number of bytes to skip
223    */
224   public long skip(long n) throws IOException
225   {
226     if (inf == null)
227       throw new IOException("stream closed");
228     if (n < 0)
229       throw new IllegalArgumentException();
230
231     if (n == 0)
232       return 0;
233
234     int buflen = (int) Math.min(n, 2048);
235     byte[] tmpbuf = new byte[buflen];
236
237     long skipped = 0L;
238     while (n > 0L)
239       {
240         int numread = read(tmpbuf, 0, buflen);
241         if (numread <= 0)
242           break;
243         n -= numread;
244         skipped += numread;
245         buflen = (int) Math.min(n, 2048);
246       }
247
248     return skipped;
249  }
250
251   public boolean markSupported()
252   {
253     return false;
254   }
255
256   public void mark(int readLimit)
257   {
258   }
259
260   public void reset() throws IOException
261   {
262     throw new IOException("reset not supported");
263   }
264 }