1 /* 2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 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. 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 59 private final boolean syncFlush; 60 61 private ZipCryption zipCryption; 62 63 /** 64 * Creates a new output stream with the specified compressor, 65 * buffer size and flush mode. 66 67 * @param out the output stream 68 * @param def the compressor ("deflater") 69 * @param size the output buffer size 70 * @param syncFlush 71 * if {@code true} the {@link #flush()} method of this 72 * instance flushes the compressor with flush mode 73 * {@link Deflater#SYNC_FLUSH} before flushing the output 74 * stream, otherwise only flushes the output stream 75 * 76 * @throws IllegalArgumentException if {@code size <= 0} 77 * 78 * @since 1.7 79 */ 80 public DeflaterOutputStream(OutputStream out, 81 Deflater def, 82 int size, 83 boolean syncFlush) { 84 super(out); 85 if (out == null || def == null) { 86 throw new NullPointerException(); 87 } else if (size <= 0) { 88 throw new IllegalArgumentException("buffer size <= 0"); 89 } 90 this.def = def; 91 this.buf = new byte[size]; 92 this.syncFlush = syncFlush; 93 } 94 95 96 /** 97 * Creates a new output stream with the specified compressor and 98 * buffer size. 99 * 100 * <p>The new output stream instance is created as if by invoking 101 * the 4-argument constructor DeflaterOutputStream(out, def, size, false). 102 * 103 * @param out the output stream 104 * @param def the compressor ("deflater") 105 * @param size the output buffer size 106 * @exception IllegalArgumentException if {@code size <= 0} 107 */ 108 public DeflaterOutputStream(OutputStream out, Deflater def, int size) { 109 this(out, def, size, false); 110 } 111 112 /** 113 * Creates a new output stream with the specified compressor, flush 114 * mode and a default buffer size. 115 * 116 * @param out the output stream 117 * @param def the compressor ("deflater") 118 * @param syncFlush 119 * if {@code true} the {@link #flush()} method of this 120 * instance flushes the compressor with flush mode 121 * {@link Deflater#SYNC_FLUSH} before flushing the output 122 * stream, otherwise only flushes the output stream 123 * 124 * @since 1.7 125 */ 126 public DeflaterOutputStream(OutputStream out, 127 Deflater def, 128 boolean syncFlush) { 129 this(out, def, 512, syncFlush); 130 } 131 132 133 /** 134 * Creates a new output stream with the specified compressor and 135 * a default buffer size. 136 * 137 * <p>The new output stream instance is created as if by invoking 138 * the 3-argument constructor DeflaterOutputStream(out, def, false). 139 * 140 * @param out the output stream 141 * @param def the compressor ("deflater") 142 */ 143 public DeflaterOutputStream(OutputStream out, Deflater def) { 144 this(out, def, 512, false); 145 } 146 147 boolean usesDefaultDeflater = false; 148 149 150 /** 151 * Creates a new output stream with a default compressor, a default 152 * buffer size and the specified flush mode. 153 * 154 * @param out the output stream 155 * @param syncFlush 156 * if {@code true} the {@link #flush()} method of this 157 * instance flushes the compressor with flush mode 158 * {@link Deflater#SYNC_FLUSH} before flushing the output 159 * stream, otherwise only flushes the output stream 160 * 161 * @since 1.7 162 */ 163 public DeflaterOutputStream(OutputStream out, boolean syncFlush) { 164 this(out, new Deflater(), 512, syncFlush); 165 usesDefaultDeflater = true; 166 } 167 168 /** 169 * Creates a new output stream with a default compressor and buffer size. 170 * 171 * <p>The new output stream instance is created as if by invoking 172 * the 2-argument constructor DeflaterOutputStream(out, false). 173 * 174 * @param out the output stream 175 */ 176 public DeflaterOutputStream(OutputStream out) { 177 this(out, false); 178 usesDefaultDeflater = true; 179 } 180 181 /** 182 * Writes a byte to the compressed output stream. This method will 183 * block until the byte can be written. 184 * @param b the byte to be written 185 * @exception IOException if an I/O error has occurred 186 */ 187 public void write(int b) throws IOException { 188 byte[] buf = new byte[1]; 189 buf[0] = (byte)(b & 0xff); 190 write(buf, 0, 1); 191 } 192 193 /** 194 * Writes an array of bytes to the compressed output stream. This 195 * method will block until all the bytes are written. 196 * @param b the data to be written 197 * @param off the start offset of the data 198 * @param len the length of the data 199 * @exception IOException if an I/O error has occurred 200 */ 201 public void write(byte[] b, int off, int len) throws IOException { 202 if (def.finished()) { 203 throw new IOException("write beyond end of stream"); 204 } 205 if ((off | len | (off + len) | (b.length - (off + len))) < 0) { 206 throw new IndexOutOfBoundsException(); 207 } else if (len == 0) { 208 return; 209 } 210 if (!def.finished()) { 211 def.setInput(b, off, len); 212 while (!def.needsInput()) { 213 deflate(); 214 } 215 } 216 } 217 218 /** 219 * Finishes writing compressed data to the output stream without closing 220 * the underlying stream. Use this method when applying multiple filters 221 * in succession to the same output stream. 222 * @exception IOException if an I/O error has occurred 223 */ 224 public void finish() throws IOException { 225 if (!def.finished()) { 226 def.finish(); 227 while (!def.finished()) { 228 deflate(); 229 } 230 } 231 } 232 233 /** 234 * Writes remaining compressed data to the output stream and closes the 235 * underlying stream. 236 * @exception IOException if an I/O error has occurred 237 */ 238 public void close() throws IOException { 239 if (!closed) { 240 finish(); 241 if (usesDefaultDeflater) 242 def.end(); 243 out.close(); 244 closed = true; 245 } 246 } 247 248 /** 249 * Writes next block of compressed data to the output stream. 250 * @throws IOException if an I/O error has occurred 251 */ 252 protected void deflate() throws IOException { 253 int len = def.deflate(buf, 0, buf.length); 254 if (len > 0) { 255 256 if (zipCryption != null) { 257 zipCryption.encryptBytes(buf, 0, len); 258 } 259 260 out.write(buf, 0, len); 261 } 262 } 263 264 /** 265 * Flushes the compressed output stream. 266 * 267 * If {@link #DeflaterOutputStream(OutputStream, Deflater, int, boolean) 268 * syncFlush} is {@code true} when this compressed output stream is 269 * constructed, this method first flushes the underlying {@code compressor} 270 * with the flush mode {@link Deflater#SYNC_FLUSH} to force 271 * all pending data to be flushed out to the output stream and then 272 * flushes the output stream. Otherwise this method only flushes the 273 * output stream without flushing the {@code compressor}. 274 * 275 * @throws IOException if an I/O error has occurred 276 * 277 * @since 1.7 278 */ 279 public void flush() throws IOException { 280 if (syncFlush && !def.finished()) { 281 int len = 0; 282 while ((len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH)) > 0) 283 { 284 285 if (zipCryption != null) { 286 zipCryption.encryptBytes(buf, 0, len); 287 } 288 289 out.write(buf, 0, len); 290 if (len < buf.length) 291 break; 292 } 293 } 294 out.flush(); 295 } 296 297 /** 298 * Set ZIP encryption/decryption engine to this deflater. 299 * @param zipCryption ZIP encrypt/decrypt engine. 300 */ 301 public void setZipCryption(ZipCryption zipCryption) { 302 this.zipCryption = zipCryption; 303 } 304 305 }