001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.zip;
027
028 import java.io.FilterOutputStream;
029 import java.io.OutputStream;
030 import java.io.InputStream;
031 import java.io.IOException;
032
033 /**
034 * This class implements an output stream filter for compressing data in
035 * the "deflate" compression format. It is also used as the basis for other
036 * types of compression filters, such as GZIPOutputStream.
037 *
038 * @see Deflater
039 * @version 1.42, 05/05/07
040 * @author David Connelly
041 */
042 public class DeflaterOutputStream extends FilterOutputStream {
043 /**
044 * Compressor for this stream.
045 */
046 protected Deflater def;
047
048 /**
049 * Output buffer for writing compressed data.
050 */
051 protected byte[] buf;
052
053 /**
054 * Indicates that the stream has been closed.
055 */
056
057 private boolean closed = false;
058
059 /**
060 * Creates a new output stream with the specified compressor and
061 * buffer size.
062 * @param out the output stream
063 * @param def the compressor ("deflater")
064 * @param size the output buffer size
065 * @exception IllegalArgumentException if size is <= 0
066 */
067 public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
068 super (out);
069 if (out == null || def == null) {
070 throw new NullPointerException();
071 } else if (size <= 0) {
072 throw new IllegalArgumentException("buffer size <= 0");
073 }
074 this .def = def;
075 buf = new byte[size];
076 }
077
078 /**
079 * Creates a new output stream with the specified compressor and
080 * a default buffer size.
081 * @param out the output stream
082 * @param def the compressor ("deflater")
083 */
084 public DeflaterOutputStream(OutputStream out, Deflater def) {
085 this (out, def, 512);
086 }
087
088 boolean usesDefaultDeflater = false;
089
090 /**
091 * Creates a new output stream with a default compressor and buffer size.
092 * @param out the output stream
093 */
094 public DeflaterOutputStream(OutputStream out) {
095 this (out, new Deflater());
096 usesDefaultDeflater = true;
097 }
098
099 /**
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
116 * @param len the length of the data
117 * @exception IOException if an I/O error has occurred
118 */
119 public void write(byte[] b, int off, int len) throws IOException {
120 if (def.finished()) {
121 throw new IOException("write beyond end of stream");
122 }
123 if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
124 throw new IndexOutOfBoundsException();
125 } else if (len == 0) {
126 return;
127 }
128 if (!def.finished()) {
129 // Deflate no more than stride bytes at a time. This avoids
130 // excess copying in deflateBytes (see Deflater.c)
131 int stride = buf.length;
132 for (int i = 0; i < len; i += stride) {
133 def.setInput(b, off + i, Math.min(stride, len - i));
134 while (!def.needsInput()) {
135 deflate();
136 }
137 }
138 }
139 }
140
141 /**
142 * Finishes writing compressed data to the output stream without closing
143 * the underlying stream. Use this method when applying multiple filters
144 * in succession to the same output stream.
145 * @exception IOException if an I/O error has occurred
146 */
147 public void finish() throws IOException {
148 if (!def.finished()) {
149 def.finish();
150 while (!def.finished()) {
151 deflate();
152 }
153 }
154 }
155
156 /**
157 * Writes remaining compressed data to the output stream and closes the
158 * underlying stream.
159 * @exception IOException if an I/O error has occurred
160 */
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 }
|