author | lana |
Mon, 07 Jun 2010 17:08:26 -0700 | |
changeset 5631 | cfb4f43485f3 |
parent 5506 | 202f599c92aa |
child 9023 | 6175922846a4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1996, 2009, 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.FilterOutputStream; |
|
29 |
import java.io.OutputStream; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.IOException; |
|
32 |
||
33 |
/** |
|
34 |
* This class implements an output stream filter for compressing data in |
|
35 |
* the "deflate" compression format. It is also used as the basis for other |
|
36 |
* types of compression filters, such as GZIPOutputStream. |
|
37 |
* |
|
38 |
* @see Deflater |
|
39 |
* @author David Connelly |
|
40 |
*/ |
|
41 |
public |
|
42 |
class DeflaterOutputStream extends FilterOutputStream { |
|
43 |
/** |
|
44 |
* Compressor for this stream. |
|
45 |
*/ |
|
46 |
protected Deflater def; |
|
47 |
||
48 |
/** |
|
49 |
* Output buffer for writing compressed data. |
|
50 |
*/ |
|
51 |
protected byte[] buf; |
|
52 |
||
53 |
/** |
|
54 |
* Indicates that the stream has been closed. |
|
55 |
*/ |
|
56 |
||
57 |
private boolean closed = false; |
|
58 |
||
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
59 |
private final boolean syncFlush; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
60 |
|
2 | 61 |
/** |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
62 |
* Creates a new output stream with the specified compressor, |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
63 |
* buffer size and flush mode. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
64 |
|
2 | 65 |
* @param out the output stream |
66 |
* @param def the compressor ("deflater") |
|
67 |
* @param size the output buffer size |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
68 |
* @param syncFlush |
4354
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4162
diff
changeset
|
69 |
* if {@code true} the {@link #flush()} method of this |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
70 |
* instance flushes the compressor with flush mode |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
71 |
* {@link Deflater#SYNC_FLUSH} before flushing the output |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
72 |
* stream, otherwise only flushes the output stream |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
73 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
74 |
* @throws IllegalArgumentException if size is <= 0 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
75 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
76 |
* @since 1.7 |
2 | 77 |
*/ |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
78 |
public DeflaterOutputStream(OutputStream out, |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
79 |
Deflater def, |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
80 |
int size, |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
81 |
boolean syncFlush) { |
2 | 82 |
super(out); |
83 |
if (out == null || def == null) { |
|
84 |
throw new NullPointerException(); |
|
85 |
} else if (size <= 0) { |
|
86 |
throw new IllegalArgumentException("buffer size <= 0"); |
|
87 |
} |
|
88 |
this.def = def; |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
89 |
this.buf = new byte[size]; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
90 |
this.syncFlush = syncFlush; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
91 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
92 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
93 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
94 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
95 |
* Creates a new output stream with the specified compressor and |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
96 |
* buffer size. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
97 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
98 |
* <p>The new output stream instance is created as if by invoking |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
99 |
* the 4-argument constructor DeflaterOutputStream(out, def, size, false). |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
100 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
101 |
* @param out the output stream |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
102 |
* @param def the compressor ("deflater") |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
103 |
* @param size the output buffer size |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
104 |
* @exception IllegalArgumentException if size is <= 0 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
105 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
106 |
public DeflaterOutputStream(OutputStream out, Deflater def, int size) { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
107 |
this(out, def, size, false); |
2 | 108 |
} |
109 |
||
110 |
/** |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
111 |
* Creates a new output stream with the specified compressor, flush |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
112 |
* mode and a default buffer size. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
113 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
114 |
* @param out the output stream |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
115 |
* @param def the compressor ("deflater") |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
116 |
* @param syncFlush |
4354
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4162
diff
changeset
|
117 |
* if {@code true} the {@link #flush()} method of this |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
118 |
* instance flushes the compressor with flush mode |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
119 |
* {@link Deflater#SYNC_FLUSH} before flushing the output |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
120 |
* stream, otherwise only flushes the output stream |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
121 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
122 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
123 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
124 |
public DeflaterOutputStream(OutputStream out, |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
125 |
Deflater def, |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
126 |
boolean syncFlush) { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
127 |
this(out, def, 512, syncFlush); |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
128 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
129 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
130 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
131 |
/** |
2 | 132 |
* Creates a new output stream with the specified compressor and |
133 |
* a default buffer size. |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
134 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
135 |
* <p>The new output stream instance is created as if by invoking |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
136 |
* the 3-argument constructor DeflaterOutputStream(out, def, false). |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
137 |
* |
2 | 138 |
* @param out the output stream |
139 |
* @param def the compressor ("deflater") |
|
140 |
*/ |
|
141 |
public DeflaterOutputStream(OutputStream out, Deflater def) { |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
142 |
this(out, def, 512, false); |
2 | 143 |
} |
144 |
||
145 |
boolean usesDefaultDeflater = false; |
|
146 |
||
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
147 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
148 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
149 |
* Creates a new output stream with a default compressor, a default |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
150 |
* buffer size and the specified flush mode. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
151 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
152 |
* @param out the output stream |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
153 |
* @param syncFlush |
4354
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4162
diff
changeset
|
154 |
* if {@code true} the {@link #flush()} method of this |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
155 |
* instance flushes the compressor with flush mode |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
156 |
* {@link Deflater#SYNC_FLUSH} before flushing the output |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
157 |
* stream, otherwise only flushes the output stream |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
158 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
159 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
160 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
161 |
public DeflaterOutputStream(OutputStream out, boolean syncFlush) { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
162 |
this(out, new Deflater(), 512, syncFlush); |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
163 |
usesDefaultDeflater = true; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
164 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
165 |
|
2 | 166 |
/** |
167 |
* Creates a new output stream with a default compressor and buffer size. |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
168 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
169 |
* <p>The new output stream instance is created as if by invoking |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
170 |
* the 2-argument constructor DeflaterOutputStream(out, false). |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
171 |
* |
2 | 172 |
* @param out the output stream |
173 |
*/ |
|
174 |
public DeflaterOutputStream(OutputStream out) { |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
175 |
this(out, false); |
2 | 176 |
usesDefaultDeflater = true; |
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* Writes a byte to the compressed output stream. This method will |
|
181 |
* block until the byte can be written. |
|
182 |
* @param b the byte to be written |
|
183 |
* @exception IOException if an I/O error has occurred |
|
184 |
*/ |
|
185 |
public void write(int b) throws IOException { |
|
186 |
byte[] buf = new byte[1]; |
|
187 |
buf[0] = (byte)(b & 0xff); |
|
188 |
write(buf, 0, 1); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Writes an array of bytes to the compressed output stream. This |
|
193 |
* method will block until all the bytes are written. |
|
194 |
* @param b the data to be written |
|
195 |
* @param off the start offset of the data |
|
196 |
* @param len the length of the data |
|
197 |
* @exception IOException if an I/O error has occurred |
|
198 |
*/ |
|
199 |
public void write(byte[] b, int off, int len) throws IOException { |
|
200 |
if (def.finished()) { |
|
201 |
throw new IOException("write beyond end of stream"); |
|
202 |
} |
|
203 |
if ((off | len | (off + len) | (b.length - (off + len))) < 0) { |
|
204 |
throw new IndexOutOfBoundsException(); |
|
205 |
} else if (len == 0) { |
|
206 |
return; |
|
207 |
} |
|
208 |
if (!def.finished()) { |
|
209 |
// Deflate no more than stride bytes at a time. This avoids |
|
210 |
// excess copying in deflateBytes (see Deflater.c) |
|
211 |
int stride = buf.length; |
|
212 |
for (int i = 0; i < len; i+= stride) { |
|
213 |
def.setInput(b, off + i, Math.min(stride, len - i)); |
|
214 |
while (!def.needsInput()) { |
|
215 |
deflate(); |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Finishes writing compressed data to the output stream without closing |
|
223 |
* the underlying stream. Use this method when applying multiple filters |
|
224 |
* in succession to the same output stream. |
|
225 |
* @exception IOException if an I/O error has occurred |
|
226 |
*/ |
|
227 |
public void finish() throws IOException { |
|
228 |
if (!def.finished()) { |
|
229 |
def.finish(); |
|
230 |
while (!def.finished()) { |
|
231 |
deflate(); |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Writes remaining compressed data to the output stream and closes the |
|
238 |
* underlying stream. |
|
239 |
* @exception IOException if an I/O error has occurred |
|
240 |
*/ |
|
241 |
public void close() throws IOException { |
|
242 |
if (!closed) { |
|
243 |
finish(); |
|
244 |
if (usesDefaultDeflater) |
|
245 |
def.end(); |
|
246 |
out.close(); |
|
247 |
closed = true; |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Writes next block of compressed data to the output stream. |
|
253 |
* @throws IOException if an I/O error has occurred |
|
254 |
*/ |
|
255 |
protected void deflate() throws IOException { |
|
256 |
int len = def.deflate(buf, 0, buf.length); |
|
257 |
if (len > 0) { |
|
258 |
out.write(buf, 0, len); |
|
259 |
} |
|
260 |
} |
|
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
261 |
|
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
262 |
/** |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
263 |
* Flushes the compressed output stream. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
264 |
* |
4354
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4162
diff
changeset
|
265 |
* If {@link #DeflaterOutputStream(OutputStream, Deflater, int, boolean) |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
266 |
* syncFlush} is {@code true} when this compressed output stream is |
4354
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4162
diff
changeset
|
267 |
* constructed, this method first flushes the underlying {@code compressor} |
3a70dde80b3b
6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents:
4162
diff
changeset
|
268 |
* with the flush mode {@link Deflater#SYNC_FLUSH} to force |
4162
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
269 |
* all pending data to be flushed out to the output stream and then |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
270 |
* flushes the output stream. Otherwise this method only flushes the |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
271 |
* output stream without flushing the {@code compressor}. |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
272 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
273 |
* @throws IOException if an I/O error has occurred |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
274 |
* |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
275 |
* @since 1.7 |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
276 |
*/ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
277 |
public void flush() throws IOException { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
278 |
if (syncFlush && !def.finished()) { |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
279 |
int len = 0; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
280 |
while ((len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH)) > 0) |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
281 |
{ |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
282 |
out.write(buf, 0, len); |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
283 |
if (len < buf.length) |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
284 |
break; |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
285 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
286 |
} |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
287 |
out.flush(); |
425328b81201
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents:
2
diff
changeset
|
288 |
} |
2 | 289 |
} |