49 |
* as the <code>GZIPInputStream</code>. |
* as the <code>GZIPInputStream</code>. |
50 |
* |
* |
51 |
* @author John Leuner |
* @author John Leuner |
52 |
* @since JDK 1.1 |
* @since 1.1 |
53 |
*/ |
*/ |
54 |
|
public class InflaterInputStream extends FilterInputStream |
55 |
public class InflaterInputStream extends FilterInputStream { |
{ |
|
|
|
|
//Variables |
|
|
|
|
56 |
/** |
/** |
57 |
* Decompressor for this filter |
* Decompressor for this filter |
58 |
*/ |
*/ |
|
|
|
59 |
protected Inflater inf; |
protected Inflater inf; |
60 |
|
|
61 |
/** |
/** |
62 |
* Byte array used as a buffer |
* Byte array used as a buffer |
63 |
*/ |
*/ |
|
|
|
64 |
protected byte[] buf; |
protected byte[] buf; |
65 |
|
|
66 |
/** |
/** |
67 |
* Size of buffer |
* Size of buffer |
68 |
*/ |
*/ |
|
|
|
69 |
protected int len; |
protected int len; |
70 |
|
|
71 |
|
|
72 |
//We just use this if we are decoding one byte at a time with the read() call |
/* |
73 |
|
* We just use this if we are decoding one byte at a time with the read() call |
74 |
|
*/ |
75 |
private byte[] onebytebuffer = new byte[1]; |
private byte[] onebytebuffer = new byte[1]; |
76 |
|
|
|
//Constructors |
|
|
|
|
|
|
|
77 |
/** |
/** |
78 |
* Create an InflaterInputStream with the default decompresseor |
* Create an InflaterInputStream with the default decompresseor |
79 |
* and a default buffer size. |
* and a default buffer size. |
80 |
* |
* |
81 |
* @param in the InputStream to read bytes from |
* @param in the InputStream to read bytes from |
82 |
*/ |
*/ |
|
|
|
83 |
public InflaterInputStream(InputStream in) |
public InflaterInputStream(InputStream in) |
84 |
{ |
{ |
85 |
this(in, new Inflater(), 4096); |
this(in, new Inflater(), 4096); |
92 |
* @param in the InputStream to read bytes from |
* @param in the InputStream to read bytes from |
93 |
* @param inf the decompressor used to decompress data read from in |
* @param inf the decompressor used to decompress data read from in |
94 |
*/ |
*/ |
|
|
|
95 |
public InflaterInputStream(InputStream in, Inflater inf) |
public InflaterInputStream(InputStream in, Inflater inf) |
96 |
{ |
{ |
97 |
this(in, inf, 4096); |
this(in, inf, 4096); |
105 |
* @param inf the decompressor used to decompress data read from in |
* @param inf the decompressor used to decompress data read from in |
106 |
* @param size size of the buffer to use |
* @param size size of the buffer to use |
107 |
*/ |
*/ |
|
|
|
108 |
public InflaterInputStream(InputStream in, Inflater inf, int size) |
public InflaterInputStream(InputStream in, Inflater inf, int size) |
109 |
{ |
{ |
110 |
super(in); |
super(in); |
116 |
buf = new byte[size]; //Create the buffer |
buf = new byte[size]; //Create the buffer |
117 |
} |
} |
118 |
|
|
|
//Methods |
|
|
|
|
119 |
/** |
/** |
120 |
* Returns 0 once the end of the stream (EOF) has been reached. |
* Returns 0 once the end of the stream (EOF) has been reached. |
121 |
* Otherwise returns 1. |
* Otherwise returns 1. |
122 |
*/ |
*/ |
|
|
|
123 |
public int available() throws IOException |
public int available() throws IOException |
124 |
{ |
{ |
125 |
return inf.finished() ? 0 : 1; |
return inf.finished() ? 0 : 1; |
131 |
public void close() throws IOException |
public void close() throws IOException |
132 |
{ |
{ |
133 |
in.close(); |
in.close(); |
134 |
|
in = null; |
135 |
} |
} |
136 |
|
|
137 |
/** |
/** |
139 |
*/ |
*/ |
140 |
protected void fill() throws IOException |
protected void fill() throws IOException |
141 |
{ |
{ |
142 |
|
if (in == null) |
143 |
|
throw new ZipException ("InflaterInputStream is closed"); |
144 |
|
|
145 |
len = in.read(buf, 0, buf.length); |
len = in.read(buf, 0, buf.length); |
146 |
|
|
147 |
if (len < 0) |
if (len < 0) |
148 |
throw new ZipException("Deflated stream ends early."); |
throw new ZipException("Deflated stream ends early."); |
149 |
|
|
150 |
inf.setInput(buf, 0, len); |
inf.setInput(buf, 0, len); |
151 |
} |
} |
152 |
|
|
158 |
public int read() throws IOException |
public int read() throws IOException |
159 |
{ |
{ |
160 |
int nread = read(onebytebuffer, 0, 1); //read one byte |
int nread = read(onebytebuffer, 0, 1); //read one byte |
161 |
|
|
162 |
if (nread > 0) |
if (nread > 0) |
163 |
return onebytebuffer[0] & 0xff; |
return onebytebuffer[0] & 0xff; |
164 |
|
|
165 |
return -1; |
return -1; |
166 |
} |
} |
167 |
|
|
168 |
/** |
/** |
169 |
* Decompresses data into the byte array |
* Decompresses data into the byte array |
170 |
* |
* |
|
* |
|
171 |
* @param b the array to read and decompress data into |
* @param b the array to read and decompress data into |
172 |
* @param off the offset indicating where the data should be placed |
* @param off the offset indicating where the data should be placed |
173 |
* @param len the number of bytes to decompress |
* @param len the number of bytes to decompress |
174 |
*/ |
*/ |
175 |
public int read(byte[] b, int off, int len) throws IOException |
public int read(byte[] b, int off, int len) throws IOException |
176 |
{ |
{ |
|
|
|
177 |
if (len == 0) return 0; |
if (len == 0) return 0; |
178 |
|
|
179 |
for (;;) |
for (;;) |
192 |
return count; |
return count; |
193 |
|
|
194 |
if (inf.needsDictionary()) |
if (inf.needsDictionary()) |
195 |
throw new ZipException("Need a dictionary"); |
| inf.finished()) |
|
else if (inf.finished()) |
|
196 |
return -1; |
return -1; |
197 |
else if (inf.needsInput()) |
else if (inf.needsInput()) |
198 |
fill(); |
fill(); |
206 |
* |
* |
207 |
* @param n number of bytes to skip |
* @param n number of bytes to skip |
208 |
*/ |
*/ |
|
|
|
209 |
public long skip(long n) throws IOException |
public long skip(long n) throws IOException |
210 |
{ |
{ |
211 |
if (n < 0) |
if (n < 0) |
228 |
{ |
{ |
229 |
return false; |
return false; |
230 |
} |
} |
|
|
|
|
/** |
|
|
* Throws an exception. |
|
|
* |
|
|
* @see #markSupported |
|
|
* |
|
|
* @param readlimit The parameter passed to <code>in.mark(int)</code> |
|
|
*/ |
|
|
public void mark(int readlimit) |
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
/** |
|
|
* Throws an exception. |
|
|
* |
|
|
* @see #markSupported |
|
|
* |
|
|
* @exception IOException If an error occurs |
|
|
*/ |
|
|
public void reset() throws IOException |
|
|
{ |
|
|
throw new IOException("mark/reset not supported by " + getClass()); |
|
|
} |
|
231 |
} |
} |