jdk/src/java.base/share/classes/java/util/zip/InflaterOutputStream.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23010 jdk/src/share/classes/java/util/zip/InflaterOutputStream.java@6dadb192ad81
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
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) 2006, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Implements an output stream filter for uncompressing data stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * "deflate" compression format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since       1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author      David R Tribble (david@tribble.com)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see InflaterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see DeflaterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see DeflaterOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class InflaterOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /** Decompressor for this stream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected final Inflater inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** Output buffer for writing uncompressed data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected final byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /** Temporary write buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final byte[] wbuf = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** Default decompressor is used. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean usesDefaultInflater = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** true iff {@link #close()} has been called. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Checks to make sure that this stream has not been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a new output stream with a default decompressor and buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param out output stream to write the uncompressed data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @throws NullPointerException if {@code out} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public InflaterOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this(out, new Inflater());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        usesDefaultInflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a new output stream with the specified decompressor and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * default buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param out output stream to write the uncompressed data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param infl decompressor ("inflater") for this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @throws NullPointerException if {@code out} or {@code infl} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public InflaterOutputStream(OutputStream out, Inflater infl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this(out, infl, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Creates a new output stream with the specified decompressor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param out output stream to write the uncompressed data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param infl decompressor ("inflater") for this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param bufLen decompression buffer size
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   100
     * @throws IllegalArgumentException if {@code bufLen <= 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws NullPointerException if {@code out} or {@code infl} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public InflaterOutputStream(OutputStream out, Inflater infl, int bufLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Sanity checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (out == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new NullPointerException("Null output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (infl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new NullPointerException("Null inflater");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (bufLen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new IllegalArgumentException("Buffer size < 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        inf = infl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        buf = new byte[bufLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Writes any remaining uncompressed data to the output stream and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * the underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // Complete the uncompressed output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Flushes this output stream, forcing any pending buffered output bytes to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @throws IOException if an I/O error occurs or this stream is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Finish decompressing and writing pending output data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!inf.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                while (!inf.finished()  &&  !inf.needsInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    // Decompress pending output data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    n = inf.inflate(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    if (n < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // Write the uncompressed output data block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    out.write(buf, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                super.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } catch (DataFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                // Improperly formatted compressed (ZIP) data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                String msg = ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    msg = "Invalid ZLIB data format";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new ZipException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Finishes writing uncompressed data to the output stream without closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * the underlying stream.  Use this method when applying multiple filters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * succession to the same output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws IOException if an I/O error occurs or this stream is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void finish() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // Finish decompressing and writing pending output data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (usesDefaultInflater) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            inf.end();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Writes a byte to the uncompressed output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param b a single byte of compressed data to decompress and write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws IOException if an I/O error occurs or this stream is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @throws ZipException if a compression (ZIP) format error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // Write a single byte of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        wbuf[0] = (byte) b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        write(wbuf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Writes an array of bytes to the uncompressed output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param b buffer containing compressed data to decompress and write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param off starting offset of the compressed data within {@code b}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param len number of bytes to decompress from {@code b}
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   214
     * @throws IndexOutOfBoundsException if {@code off < 0}, or if
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   215
     * {@code len < 0}, or if {@code len > b.length - off}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @throws IOException if an I/O error occurs or this stream is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @throws NullPointerException if {@code b} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @throws ZipException if a compression (ZIP) format error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // Sanity checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new NullPointerException("Null buffer for read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // Write uncompressed data to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                // Fill the decompressor buffer with output data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (inf.needsInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    int part;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (len < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    part = (len < 512 ? len : 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    inf.setInput(b, off, part);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    off += part;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    len -= part;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // Decompress and write blocks of output data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    n = inf.inflate(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        out.write(buf, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                // Check the decompressor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (inf.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (inf.needsDictionary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    throw new ZipException("ZLIB dictionary missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } catch (DataFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // Improperly formatted compressed (ZIP) data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            String msg = ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                msg = "Invalid ZLIB data format";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new ZipException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
}