author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 25859 | jdk/src/java.base/share/classes/java/util/zip/DeflaterInputStream.java@3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18156
diff
changeset
|
2 |
* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
27 |
||
28 |
import java.io.FilterInputStream; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.IOException; |
|
31 |
||
32 |
/** |
|
33 |
* Implements an input stream filter for compressing data in the "deflate" |
|
34 |
* compression format. |
|
35 |
* |
|
36 |
* @since 1.6 |
|
37 |
* @author David R Tribble (david@tribble.com) |
|
38 |
* |
|
39 |
* @see DeflaterOutputStream |
|
40 |
* @see InflaterOutputStream |
|
41 |
* @see InflaterInputStream |
|
42 |
*/ |
|
43 |
||
44 |
public class DeflaterInputStream extends FilterInputStream { |
|
45 |
/** Compressor for this stream. */ |
|
46 |
protected final Deflater def; |
|
47 |
||
48 |
/** Input buffer for reading compressed data. */ |
|
49 |
protected final byte[] buf; |
|
50 |
||
51 |
/** Temporary read buffer. */ |
|
52 |
private byte[] rbuf = new byte[1]; |
|
53 |
||
54 |
/** Default compressor is used. */ |
|
55 |
private boolean usesDefaultDeflater = false; |
|
56 |
||
57 |
/** End of the underlying input stream has been reached. */ |
|
58 |
private boolean reachEOF = false; |
|
59 |
||
60 |
/** |
|
61 |
* Check to make sure that this stream has not been closed. |
|
62 |
*/ |
|
63 |
private void ensureOpen() throws IOException { |
|
64 |
if (in == null) { |
|
65 |
throw new IOException("Stream closed"); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Creates a new input stream with a default compressor and buffer |
|
71 |
* size. |
|
72 |
* |
|
73 |
* @param in input stream to read the uncompressed data to |
|
74 |
* @throws NullPointerException if {@code in} is null |
|
75 |
*/ |
|
76 |
public DeflaterInputStream(InputStream in) { |
|
77 |
this(in, new Deflater()); |
|
78 |
usesDefaultDeflater = true; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Creates a new input stream with the specified compressor and a |
|
83 |
* default buffer size. |
|
84 |
* |
|
85 |
* @param in input stream to read the uncompressed data to |
|
86 |
* @param defl compressor ("deflater") for this stream |
|
87 |
* @throws NullPointerException if {@code in} or {@code defl} is null |
|
88 |
*/ |
|
89 |
public DeflaterInputStream(InputStream in, Deflater defl) { |
|
90 |
this(in, defl, 512); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Creates a new input stream with the specified compressor and buffer |
|
95 |
* size. |
|
96 |
* |
|
97 |
* @param in input stream to read the uncompressed data to |
|
98 |
* @param defl compressor ("deflater") for this stream |
|
99 |
* @param bufLen compression buffer size |
|
18156 | 100 |
* @throws IllegalArgumentException if {@code bufLen <= 0} |
2 | 101 |
* @throws NullPointerException if {@code in} or {@code defl} is null |
102 |
*/ |
|
103 |
public DeflaterInputStream(InputStream in, Deflater defl, int bufLen) { |
|
104 |
super(in); |
|
105 |
||
106 |
// Sanity checks |
|
107 |
if (in == null) |
|
108 |
throw new NullPointerException("Null input"); |
|
109 |
if (defl == null) |
|
110 |
throw new NullPointerException("Null deflater"); |
|
111 |
if (bufLen < 1) |
|
112 |
throw new IllegalArgumentException("Buffer size < 1"); |
|
113 |
||
114 |
// Initialize |
|
115 |
def = defl; |
|
116 |
buf = new byte[bufLen]; |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Closes this input stream and its underlying input stream, discarding |
|
121 |
* any pending uncompressed data. |
|
122 |
* |
|
123 |
* @throws IOException if an I/O error occurs |
|
124 |
*/ |
|
125 |
public void close() throws IOException { |
|
126 |
if (in != null) { |
|
127 |
try { |
|
128 |
// Clean up |
|
129 |
if (usesDefaultDeflater) { |
|
130 |
def.end(); |
|
131 |
} |
|
132 |
||
133 |
in.close(); |
|
134 |
} finally { |
|
135 |
in = null; |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Reads a single byte of compressed data from the input stream. |
|
142 |
* This method will block until some input can be read and compressed. |
|
143 |
* |
|
144 |
* @return a single byte of compressed data, or -1 if the end of the |
|
145 |
* uncompressed input stream is reached |
|
146 |
* @throws IOException if an I/O error occurs or if this stream is |
|
147 |
* already closed |
|
148 |
*/ |
|
149 |
public int read() throws IOException { |
|
150 |
// Read a single byte of compressed data |
|
151 |
int len = read(rbuf, 0, 1); |
|
152 |
if (len <= 0) |
|
153 |
return -1; |
|
154 |
return (rbuf[0] & 0xFF); |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Reads compressed data into a byte array. |
|
159 |
* This method will block until some input can be read and compressed. |
|
160 |
* |
|
161 |
* @param b buffer into which the data is read |
|
162 |
* @param off starting offset of the data within {@code b} |
|
163 |
* @param len maximum number of compressed bytes to read into {@code b} |
|
164 |
* @return the actual number of bytes read, or -1 if the end of the |
|
165 |
* uncompressed input stream is reached |
|
18156 | 166 |
* @throws IndexOutOfBoundsException if {@code len > b.length - off} |
2 | 167 |
* @throws IOException if an I/O error occurs or if this input stream is |
168 |
* already closed |
|
169 |
*/ |
|
170 |
public int read(byte[] b, int off, int len) throws IOException { |
|
171 |
// Sanity checks |
|
172 |
ensureOpen(); |
|
173 |
if (b == null) { |
|
174 |
throw new NullPointerException("Null buffer for read"); |
|
175 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
176 |
throw new IndexOutOfBoundsException(); |
|
177 |
} else if (len == 0) { |
|
178 |
return 0; |
|
179 |
} |
|
180 |
||
181 |
// Read and compress (deflate) input data bytes |
|
182 |
int cnt = 0; |
|
183 |
while (len > 0 && !def.finished()) { |
|
184 |
int n; |
|
185 |
||
186 |
// Read data from the input stream |
|
187 |
if (def.needsInput()) { |
|
188 |
n = in.read(buf, 0, buf.length); |
|
189 |
if (n < 0) { |
|
190 |
// End of the input stream reached |
|
191 |
def.finish(); |
|
192 |
} else if (n > 0) { |
|
193 |
def.setInput(buf, 0, n); |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
// Compress the input data, filling the read buffer |
|
198 |
n = def.deflate(b, off, len); |
|
199 |
cnt += n; |
|
200 |
off += n; |
|
201 |
len -= n; |
|
202 |
} |
|
203 |
if (cnt == 0 && def.finished()) { |
|
204 |
reachEOF = true; |
|
205 |
cnt = -1; |
|
206 |
} |
|
207 |
||
208 |
return cnt; |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* Skips over and discards data from the input stream. |
|
213 |
* This method may block until the specified number of bytes are read and |
|
214 |
* skipped. <em>Note:</em> While {@code n} is given as a {@code long}, |
|
215 |
* the maximum number of bytes which can be skipped is |
|
216 |
* {@code Integer.MAX_VALUE}. |
|
217 |
* |
|
218 |
* @param n number of bytes to be skipped |
|
219 |
* @return the actual number of bytes skipped |
|
220 |
* @throws IOException if an I/O error occurs or if this stream is |
|
221 |
* already closed |
|
222 |
*/ |
|
223 |
public long skip(long n) throws IOException { |
|
224 |
if (n < 0) { |
|
225 |
throw new IllegalArgumentException("negative skip length"); |
|
226 |
} |
|
227 |
ensureOpen(); |
|
228 |
||
229 |
// Skip bytes by repeatedly decompressing small blocks |
|
230 |
if (rbuf.length < 512) |
|
231 |
rbuf = new byte[512]; |
|
232 |
||
233 |
int total = (int)Math.min(n, Integer.MAX_VALUE); |
|
234 |
long cnt = 0; |
|
235 |
while (total > 0) { |
|
236 |
// Read a small block of uncompressed bytes |
|
237 |
int len = read(rbuf, 0, (total <= rbuf.length ? total : rbuf.length)); |
|
238 |
||
239 |
if (len < 0) { |
|
240 |
break; |
|
241 |
} |
|
242 |
cnt += len; |
|
243 |
total -= len; |
|
244 |
} |
|
245 |
return cnt; |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Returns 0 after EOF has been reached, otherwise always return 1. |
|
250 |
* <p> |
|
251 |
* Programs should not count on this method to return the actual number |
|
252 |
* of bytes that could be read without blocking |
|
253 |
* @return zero after the end of the underlying input stream has been |
|
254 |
* reached, otherwise always returns 1 |
|
255 |
* @throws IOException if an I/O error occurs or if this stream is |
|
256 |
* already closed |
|
257 |
*/ |
|
258 |
public int available() throws IOException { |
|
259 |
ensureOpen(); |
|
260 |
if (reachEOF) { |
|
261 |
return 0; |
|
262 |
} |
|
263 |
return 1; |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Always returns {@code false} because this input stream does not support |
|
268 |
* the {@link #mark mark()} and {@link #reset reset()} methods. |
|
269 |
* |
|
270 |
* @return false, always |
|
271 |
*/ |
|
272 |
public boolean markSupported() { |
|
273 |
return false; |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* <i>This operation is not supported</i>. |
|
278 |
* |
|
279 |
* @param limit maximum bytes that can be read before invalidating the position marker |
|
280 |
*/ |
|
281 |
public void mark(int limit) { |
|
282 |
// Operation not supported |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* <i>This operation is not supported</i>. |
|
287 |
* |
|
288 |
* @throws IOException always thrown |
|
289 |
*/ |
|
290 |
public void reset() throws IOException { |
|
291 |
throw new IOException("mark/reset not supported"); |
|
292 |
} |
|
293 |
} |