1 /*
2 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
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
59 /**
60 * Creates a new output stream with the specified compressor and
61 * buffer size.
62 * @param out the output stream
63 * @param def the compressor ("deflater")
64 * @param size the output buffer size
65 * @exception IllegalArgumentException if size is <= 0
66 */
67 public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
68 super(out);
69 if (out == null || def == null) {
70 throw new NullPointerException();
71 } else if (size <= 0) {
72 throw new IllegalArgumentException("buffer size <= 0");
73 }
74 this.def = def;
75 buf = new byte[size];
76 }
77
78 /**
79 * Creates a new output stream with the specified compressor and
80 * a default buffer size.
81 * @param out the output stream
82 * @param def the compressor ("deflater")
83 */
84 public DeflaterOutputStream(OutputStream out, Deflater def) {
85 this(out, def, 512);
86 }
87
88 boolean usesDefaultDeflater = false;
89
90 /**
91 * Creates a new output stream with a default compressor and buffer size.
92 * @param out the output stream
93 */
94 public DeflaterOutputStream(OutputStream out) {
95 this(out, new Deflater());
96 usesDefaultDeflater = true;
97 }
98
99 /**
100 * Writes a byte to the compressed output stream. This method will
101 * block until the byte can be written.
102 * @param b the byte to be written
103 * @exception IOException if an I/O error has occurred
104 */
105 public void write(int b) throws IOException {
106 byte[] buf = new byte[1];
107 buf[0] = (byte)(b & 0xff);
108 write(buf, 0, 1);
109 }
110
111 /**
112 * Writes an array of bytes to the compressed output stream. This
113 * method will block until all the bytes are written.
114 * @param b the data to be written
115 * @param off the start offset of the data
161 public void close() throws IOException {
162 if (!closed) {
163 finish();
164 if (usesDefaultDeflater)
165 def.end();
166 out.close();
167 closed = true;
168 }
169 }
170
171 /**
172 * Writes next block of compressed data to the output stream.
173 * @throws IOException if an I/O error has occurred
174 */
175 protected void deflate() throws IOException {
176 int len = def.deflate(buf, 0, buf.length);
177 if (len > 0) {
178 out.write(buf, 0, len);
179 }
180 }
181 }
|
1 /*
2 * Copyright 1996-2009 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
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
59 private final boolean syncFlush;
60
61 /**
62 * Creates a new output stream with the specified compressor,
63 * buffer size and flush mode.
64
65 * @param out the output stream
66 * @param def the compressor ("deflater")
67 * @param size the output buffer size
68 * @param syncFlush
69 * if {@code true} the {@link flush()} method of this
70 * instance flushes the compressor with flush mode
71 * {@link Deflater#SYNC_FLUSH} before flushing the output
72 * stream, otherwise only flushes the output stream
73 *
74 * @throws IllegalArgumentException if size is <= 0
75 *
76 * @since 1.7
77 */
78 public DeflaterOutputStream(OutputStream out,
79 Deflater def,
80 int size,
81 boolean syncFlush) {
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;
89 this.buf = new byte[size];
90 this.syncFlush = syncFlush;
91 }
92
93
94 /**
95 * Creates a new output stream with the specified compressor and
96 * buffer size.
97 *
98 * <p>The new output stream instance is created as if by invoking
99 * the 4-argument constructor DeflaterOutputStream(out, def, size, false).
100 *
101 * @param out the output stream
102 * @param def the compressor ("deflater")
103 * @param size the output buffer size
104 * @exception IllegalArgumentException if size is <= 0
105 */
106 public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
107 this(out, def, size, false);
108 }
109
110 /**
111 * Creates a new output stream with the specified compressor, flush
112 * mode and a default buffer size.
113 *
114 * @param out the output stream
115 * @param def the compressor ("deflater")
116 * @param syncFlush
117 * if {@code true} the {@link flush()} method of this
118 * instance flushes the compressor with flush mode
119 * {@link Deflater#SYNC_FLUSH} before flushing the output
120 * stream, otherwise only flushes the output stream
121 *
122 * @since 1.7
123 */
124 public DeflaterOutputStream(OutputStream out,
125 Deflater def,
126 boolean syncFlush) {
127 this(out, def, 512, syncFlush);
128 }
129
130
131 /**
132 * Creates a new output stream with the specified compressor and
133 * a default buffer size.
134 *
135 * <p>The new output stream instance is created as if by invoking
136 * the 3-argument constructor DeflaterOutputStream(out, def, false).
137 *
138 * @param out the output stream
139 * @param def the compressor ("deflater")
140 */
141 public DeflaterOutputStream(OutputStream out, Deflater def) {
142 this(out, def, 512, false);
143 }
144
145 boolean usesDefaultDeflater = false;
146
147
148 /**
149 * Creates a new output stream with a default compressor, a default
150 * buffer size and the specified flush mode.
151 *
152 * @param out the output stream
153 * @param syncFlush
154 * if {@code true} the {@link flush()} method of this
155 * instance flushes the compressor with flush mode
156 * {@link Deflater#SYNC_FLUSH} before flushing the output
157 * stream, otherwise only flushes the output stream
158 *
159 * @since 1.7
160 */
161 public DeflaterOutputStream(OutputStream out, boolean syncFlush) {
162 this(out, new Deflater(), 512, syncFlush);
163 usesDefaultDeflater = true;
164 }
165
166 /**
167 * Creates a new output stream with a default compressor and buffer size.
168 *
169 * <p>The new output stream instance is created as if by invoking
170 * the 2-argument constructor DeflaterOutputStream(out, false).
171 *
172 * @param out the output stream
173 */
174 public DeflaterOutputStream(OutputStream out) {
175 this(out, false);
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
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 }
261
262 /**
263 * Flushes the compressed output stream.
264 *
265 * If {@link DeflaterOutputStream(OutputStream, Deflater, int, boolean)
266 * syncFlush} is {@code true} when this compressed output stream is
267 * constructed this method flushes the underlying {@code compressor}
268 * first with the flush mode {@link Deflater#SYNC_FLUSH} to force
269 * all pending data to be flushed out to the output stream and then
270 * flushes the output stream. Otherwise this method only flushes the
271 * output stream without flushing the {@code compressor}.
272 *
273 * @throws IOException if an I/O error has occurred
274 *
275 * @since 1.7
276 */
277 public void flush() throws IOException {
278 if (syncFlush && !def.finished()) {
279 int len = 0;
280 while ((len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH)) > 0)
281 {
282 out.write(buf, 0, len);
283 if (len < buf.length)
284 break;
285 }
286 }
287 out.flush();
288 }
289 }
|