1 |
14 |
jlechner |
/* Inflater.java - Decompress a data stream
|
2 |
|
|
Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
package java.util.zip;
|
39 |
|
|
|
40 |
|
|
import gnu.gcj.RawData;
|
41 |
|
|
|
42 |
|
|
/* Written using on-line Java Platform 1.2 API Specification
|
43 |
|
|
* and JCL book.
|
44 |
|
|
* Believed complete and correct.
|
45 |
|
|
*/
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* Inflater is used to decompress data that has been compressed according
|
49 |
|
|
* to the "deflate" standard described in rfc1950.
|
50 |
|
|
*
|
51 |
|
|
* The usage is as following. First you have to set some input with
|
52 |
|
|
* <code>setInput()</code>, then inflate() it. If inflate doesn't
|
53 |
|
|
* inflate any bytes there may be three reasons:
|
54 |
|
|
* <ul>
|
55 |
|
|
* <li>needsInput() returns true because the input buffer is empty.
|
56 |
|
|
* You have to provide more input with <code>setInput()</code>.
|
57 |
|
|
* NOTE: needsInput() also returns true when, the stream is finished.
|
58 |
|
|
* </li>
|
59 |
|
|
* <li>needsDictionary() returns true, you have to provide a preset
|
60 |
|
|
* dictionary with <code>setDictionary()</code>.</li>
|
61 |
|
|
* <li>finished() returns true, the inflater has finished.</li>
|
62 |
|
|
* </ul>
|
63 |
|
|
* Once the first output byte is produced, a dictionary will not be
|
64 |
|
|
* needed at a later stage.
|
65 |
|
|
*
|
66 |
|
|
* @author John Leuner, Jochen Hoenicke
|
67 |
|
|
* @author Tom Tromey
|
68 |
|
|
* @date May 17, 1999
|
69 |
|
|
* @since JDK 1.1
|
70 |
|
|
*/
|
71 |
|
|
public class Inflater
|
72 |
|
|
{
|
73 |
|
|
// The zlib stream.
|
74 |
|
|
private RawData zstream;
|
75 |
|
|
|
76 |
|
|
// True if finished.
|
77 |
|
|
private boolean is_finished;
|
78 |
|
|
|
79 |
|
|
// True if dictionary needed.
|
80 |
|
|
private boolean dict_needed;
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Creates a new inflater.
|
84 |
|
|
*/
|
85 |
|
|
public Inflater ()
|
86 |
|
|
{
|
87 |
|
|
this (false);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
/**
|
91 |
|
|
* Creates a new inflater.
|
92 |
|
|
* @param nowrap true if no header and checksum field appears in the
|
93 |
|
|
* stream. This is used for GZIPed input. For compatibility with
|
94 |
|
|
* Sun JDK you should provide one byte of input more than needed in
|
95 |
|
|
* this case.
|
96 |
|
|
*/
|
97 |
|
|
public Inflater (boolean noHeader)
|
98 |
|
|
{
|
99 |
|
|
init (noHeader);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* Finalizes this object.
|
104 |
|
|
*/
|
105 |
|
|
protected void finalize ()
|
106 |
|
|
{
|
107 |
|
|
end ();
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Frees all objects allocated by the inflater. There's no reason
|
112 |
|
|
* to call this, since you can just rely on garbage collection (even
|
113 |
|
|
* for the Sun implementation). Exists only for compatibility
|
114 |
|
|
* with Sun's JDK, where the compressor allocates native memory.
|
115 |
|
|
* If you call any method (even reset) afterwards the behaviour is
|
116 |
|
|
* <i>undefined</i>.
|
117 |
|
|
* @deprecated Just clear all references to inflater instead.
|
118 |
|
|
*/
|
119 |
|
|
public native void end ();
|
120 |
|
|
|
121 |
|
|
/**
|
122 |
|
|
* Returns true, if the inflater has finished. This means, that no
|
123 |
|
|
* input is needed and no output can be produced.
|
124 |
|
|
*/
|
125 |
|
|
public synchronized boolean finished ()
|
126 |
|
|
{
|
127 |
|
|
return is_finished;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* Gets the adler checksum. This is either the checksum of all
|
132 |
|
|
* uncompressed bytes returned by inflate(), or if needsDictionary()
|
133 |
|
|
* returns true (and thus no output was yet produced) this is the
|
134 |
|
|
* adler checksum of the expected dictionary.
|
135 |
|
|
* @returns the adler checksum.
|
136 |
|
|
*/
|
137 |
|
|
public native int getAdler ();
|
138 |
|
|
|
139 |
|
|
/**
|
140 |
|
|
* Gets the number of unprocessed input. Useful, if the end of the
|
141 |
|
|
* stream is reached and you want to further process the bytes after
|
142 |
|
|
* the deflate stream.
|
143 |
|
|
* @return the number of bytes of the input which were not processed.
|
144 |
|
|
*/
|
145 |
|
|
public native int getRemaining ();
|
146 |
|
|
|
147 |
|
|
/**
|
148 |
|
|
* Gets the total number of processed compressed input bytes.
|
149 |
|
|
* @return the total number of bytes of processed input bytes.
|
150 |
|
|
*/
|
151 |
|
|
public native int getTotalIn ();
|
152 |
|
|
|
153 |
|
|
/**
|
154 |
|
|
* Gets the total number of output bytes returned by inflate().
|
155 |
|
|
* @return the total number of output bytes.
|
156 |
|
|
*/
|
157 |
|
|
public native int getTotalOut ();
|
158 |
|
|
|
159 |
|
|
/**
|
160 |
|
|
* Inflates the compressed stream to the output buffer. If this
|
161 |
|
|
* returns 0, you should check, whether needsDictionary(),
|
162 |
|
|
* needsInput() or finished() returns true, to determine why no
|
163 |
|
|
* further output is produced.
|
164 |
|
|
* @param buffer the output buffer.
|
165 |
|
|
* @return the number of bytes written to the buffer, 0 if no further
|
166 |
|
|
* output can be produced.
|
167 |
|
|
* @exception DataFormatException if deflated stream is invalid.
|
168 |
|
|
* @exception IllegalArgumentException if buf has length 0.
|
169 |
|
|
*/
|
170 |
|
|
public int inflate (byte[] buf) throws DataFormatException
|
171 |
|
|
{
|
172 |
|
|
return inflate (buf, 0, buf.length);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
/**
|
176 |
|
|
* Inflates the compressed stream to the output buffer. If this
|
177 |
|
|
* returns 0, you should check, whether needsDictionary(),
|
178 |
|
|
* needsInput() or finished() returns true, to determine why no
|
179 |
|
|
* further output is produced.
|
180 |
|
|
* @param buffer the output buffer.
|
181 |
|
|
* @param off the offset into buffer where the output should start.
|
182 |
|
|
* @param len the maximum length of the output.
|
183 |
|
|
* @return the number of bytes written to the buffer, 0 if no further
|
184 |
|
|
* output can be produced.
|
185 |
|
|
* @exception DataFormatException if deflated stream is invalid.
|
186 |
|
|
* @exception IndexOutOfBoundsException if the off and/or len are wrong.
|
187 |
|
|
*/
|
188 |
|
|
public native int inflate (byte[] buf, int off, int len)
|
189 |
|
|
throws DataFormatException;
|
190 |
|
|
|
191 |
|
|
private native void init (boolean noHeader);
|
192 |
|
|
|
193 |
|
|
/**
|
194 |
|
|
* Returns true, if a preset dictionary is needed to inflate the input.
|
195 |
|
|
*/
|
196 |
|
|
public synchronized boolean needsDictionary ()
|
197 |
|
|
{
|
198 |
|
|
return dict_needed;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
/**
|
202 |
|
|
* Returns true, if the input buffer is empty.
|
203 |
|
|
* You should then call setInput(). <br>
|
204 |
|
|
*
|
205 |
|
|
* <em>NOTE</em>: This method also returns true when the stream is finished.
|
206 |
|
|
*/
|
207 |
|
|
public synchronized boolean needsInput ()
|
208 |
|
|
{
|
209 |
|
|
return getRemaining () == 0;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
/**
|
213 |
|
|
* Resets the inflater so that a new stream can be decompressed. All
|
214 |
|
|
* pending input and output will be discarded.
|
215 |
|
|
*/
|
216 |
|
|
public native void reset ();
|
217 |
|
|
|
218 |
|
|
/**
|
219 |
|
|
* Sets the preset dictionary. This should only be called, if
|
220 |
|
|
* needsDictionary() returns true and it should set the same
|
221 |
|
|
* dictionary, that was used for deflating. The getAdler()
|
222 |
|
|
* function returns the checksum of the dictionary needed.
|
223 |
|
|
* @param buffer the dictionary.
|
224 |
|
|
* @exception IllegalStateException if no dictionary is needed.
|
225 |
|
|
* @exception IllegalArgumentException if the dictionary checksum is
|
226 |
|
|
* wrong.
|
227 |
|
|
*/
|
228 |
|
|
public void setDictionary (byte[] buf)
|
229 |
|
|
{
|
230 |
|
|
setDictionary (buf, 0, buf.length);
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/**
|
234 |
|
|
* Sets the preset dictionary. This should only be called, if
|
235 |
|
|
* needsDictionary() returns true and it should set the same
|
236 |
|
|
* dictionary, that was used for deflating. The getAdler()
|
237 |
|
|
* function returns the checksum of the dictionary needed.
|
238 |
|
|
* @param buffer the dictionary.
|
239 |
|
|
* @param off the offset into buffer where the dictionary starts.
|
240 |
|
|
* @param len the length of the dictionary.
|
241 |
|
|
* @exception IllegalStateException if no dictionary is needed.
|
242 |
|
|
* @exception IllegalArgumentException if the dictionary checksum is
|
243 |
|
|
* wrong.
|
244 |
|
|
* @exception IndexOutOfBoundsException if the off and/or len are wrong.
|
245 |
|
|
*/
|
246 |
|
|
public native void setDictionary (byte[] buf, int off, int len);
|
247 |
|
|
|
248 |
|
|
/**
|
249 |
|
|
* Sets the input. This should only be called, if needsInput()
|
250 |
|
|
* returns true.
|
251 |
|
|
* @param buffer the input.
|
252 |
|
|
* @exception IllegalStateException if no input is needed.
|
253 |
|
|
*/
|
254 |
|
|
public void setInput (byte[] buf)
|
255 |
|
|
{
|
256 |
|
|
setInput (buf, 0, buf.length);
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
/**
|
260 |
|
|
* Sets the input. This should only be called, if needsInput()
|
261 |
|
|
* returns true.
|
262 |
|
|
* @param buffer the input.
|
263 |
|
|
* @param off the offset into buffer where the input starts.
|
264 |
|
|
* @param len the length of the input.
|
265 |
|
|
* @exception IllegalStateException if no input is needed.
|
266 |
|
|
* @exception IndexOutOfBoundsException if the off and/or len are wrong.
|
267 |
|
|
*/
|
268 |
|
|
public native void setInput (byte[] buf, int off, int len);
|
269 |
|
|
}
|