jdk/src/share/classes/java/util/zip/DeflaterOutputStream.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4162 425328b81201
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FilterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class implements an output stream filter for compressing data in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the "deflate" compression format. It is also used as the basis for other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * types of compression filters, such as GZIPOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see         Deflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class DeflaterOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Compressor for this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected Deflater def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Output buffer for writing compressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Indicates that the stream has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Creates a new output stream with the specified compressor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param def the compressor ("deflater")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param size the output buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @exception IllegalArgumentException if size is <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (out == null || def == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new IllegalArgumentException("buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.def = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        buf = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a new output stream with the specified compressor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * a default buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param def the compressor ("deflater")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public DeflaterOutputStream(OutputStream out, Deflater def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this(out, def, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    boolean usesDefaultDeflater = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Creates a new output stream with a default compressor and buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public DeflaterOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this(out, new Deflater());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        usesDefaultDeflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Writes a byte to the compressed output stream. This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * block until the byte can be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param b the byte to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        byte[] buf = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        buf[0] = (byte)(b & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        write(buf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Writes an array of bytes to the compressed output stream. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * method will block until all the bytes are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new IOException("write beyond end of stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // Deflate no more than stride bytes at a time.  This avoids
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // excess copying in deflateBytes (see Deflater.c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            int stride = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            for (int i = 0; i < len; i+= stride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                def.setInput(b, off + i, Math.min(stride, len - i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                while (!def.needsInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Finishes writing compressed data to the output stream without closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the underlying stream. Use this method when applying multiple filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * in succession to the same output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void finish() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            def.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            while (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Writes remaining compressed data to the output stream and closes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (usesDefaultDeflater)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                def.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Writes next block of compressed data to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected void deflate() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int len = def.deflate(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            out.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}