jdk/src/java.base/share/classes/java/util/zip/DeflaterOutputStream.java
author avstepan
Thu, 06 Aug 2015 19:07:35 +0300
changeset 32037 ab4526f4ac10
parent 25859 3317bb8137f4
child 45434 4582657c7260
permissions -rw-r--r--
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18156
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4354
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4354
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4354
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4354
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4354
diff changeset
    23
 * questions.
2
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
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param def the compressor ("deflater")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
    74
     * @throws IllegalArgumentException if {@code size <= 0}
4162
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (out == null || def == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new IllegalArgumentException("buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   104
     * @exception IllegalArgumentException if {@code size <= 0}
4162
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Creates a new output stream with the specified compressor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param def the compressor ("deflater")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    boolean usesDefaultDeflater = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        usesDefaultDeflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Writes a byte to the compressed output stream. This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * block until the byte can be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param b the byte to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        byte[] buf = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        buf[0] = (byte)(b & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        write(buf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Writes an array of bytes to the compressed output stream. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * method will block until all the bytes are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new IOException("write beyond end of stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (!def.finished()) {
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 5506
diff changeset
   209
            def.setInput(b, off, len);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 5506
diff changeset
   210
            while (!def.needsInput()) {
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 5506
diff changeset
   211
                deflate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Finishes writing compressed data to the output stream without closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * the underlying stream. Use this method when applying multiple filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * in succession to the same output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void finish() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            def.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            while (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Writes remaining compressed data to the output stream and closes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (usesDefaultDeflater)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                def.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Writes next block of compressed data to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    protected void deflate() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int len = def.deflate(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            out.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   256
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   257
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   258
     * Flushes the compressed output stream.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   259
     *
4354
3a70dde80b3b 6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents: 4162
diff changeset
   260
     * 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
   261
     * 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
   262
     * constructed, this method first flushes the underlying {@code compressor}
3a70dde80b3b 6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents: 4162
diff changeset
   263
     * 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
   264
     * 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
   265
     * 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
   266
     * 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
   267
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   268
     * @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
   269
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   270
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   271
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   272
    public void flush() throws IOException {
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   273
        if (syncFlush && !def.finished()) {
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   274
            int len = 0;
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   275
            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
   276
            {
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   277
                out.write(buf, 0, len);
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   278
                if (len < buf.length)
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   279
                    break;
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   280
            }
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.flush();
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2
diff changeset
   283
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}