jdk/src/java.base/share/classes/java/util/zip/Inflater.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 32037 ab4526f4ac10
child 44534 a076dffbc2c1
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
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: 5173
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: 5173
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: 5173
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5173
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5173
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class provides support for general purpose decompression using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * popular ZLIB compression library. The ZLIB compression library was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * initially developed as part of the PNG graphics standard and is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * protected by patents. It is fully described in the specifications at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the <a href="package-summary.html#package_description">java.util.zip
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * package description</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>The following code fragment demonstrates a trivial compression
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    37
 * and decompression of a string using {@code Deflater} and
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    38
 * {@code Inflater}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *     // Encode a String into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     String inputString = "blahblahblah\u20AC\u20AC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     byte[] input = inputString.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     // Compress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     byte[] output = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     Deflater compresser = new Deflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     compresser.setInput(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     compresser.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     int compressedDataLength = compresser.deflate(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     // Decompress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     Inflater decompresser = new Inflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     decompresser.setInput(output, 0, compressedDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     byte[] result = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     int resultLength = decompresser.inflate(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     decompresser.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     // Decode the bytes into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     String outputString = new String(result, 0, resultLength, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * } catch(java.io.UnsupportedEncodingException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * } catch (java.util.zip.DataFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @see         Deflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
class Inflater {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
    75
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
    76
    private final ZStreamRef zsRef;
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    77
    private byte[] buf = defaultBuf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private int off, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean needDict;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
    81
    private long bytesRead;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
    82
    private long bytesWritten;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    84
    private static final byte[] defaultBuf = new byte[0];
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    85
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Creates a new decompressor. If the parameter 'nowrap' is true then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * the ZLIB header and checksum fields will not be used. This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * compatibility with the compression format used by both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Note: When using the 'nowrap' option it is also necessary to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * an extra "dummy" byte as input. This is required by the ZLIB native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * library in order to support certain optimizations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param nowrap if true then support GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public Inflater(boolean nowrap) {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   103
        zsRef = new ZStreamRef(init(nowrap));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Creates a new decompressor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public Inflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Sets input data for decompression. Should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * needsInput() returns true indicating that more input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param off the start offset of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param len the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   122
    public void setInput(byte[] b, int off, int len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   129
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   130
            this.buf = b;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   131
            this.off = off;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   132
            this.len = len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   133
        }
2
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
     * Sets input data for decompression. Should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * needsInput() returns true indicating that more input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void setInput(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        setInput(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Sets the preset dictionary to the given array of bytes. Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * called when inflate() returns 0 and needsDictionary() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * indicating that a preset dictionary is required. The method getAdler()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * can be used to get the Adler-32 value of the dictionary needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   158
    public void setDictionary(byte[] b, int off, int len) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   159
        if (b == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   165
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   166
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   167
            setDictionary(zsRef.address(), b, off, len);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   168
            needDict = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   169
        }
2
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
     * Sets the preset dictionary to the given array of bytes. Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * called when inflate() returns 0 and needsDictionary() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * indicating that a preset dictionary is required. The method getAdler()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * can be used to get the Adler-32 value of the dictionary needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void setDictionary(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        setDictionary(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns the total number of bytes remaining in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * This can be used to find out what bytes still remain in the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * buffer after decompression has finished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return the total number of bytes remaining in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   191
    public int getRemaining() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   192
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   193
            return len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   194
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns true if no data remains in the input buffer. This can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * be used to determine if #setInput should be called in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * to provide more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return true if no data remains in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   203
    public boolean needsInput() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   204
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   205
            return len <= 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   206
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Returns true if a preset dictionary is needed for decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return true if a preset dictionary is needed for decompression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see Inflater#setDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   214
    public boolean needsDictionary() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   215
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   216
            return needDict;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   217
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Returns true if the end of the compressed data stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return true if the end of the compressed data stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   226
    public boolean finished() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   227
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   228
            return finished;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   229
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Uncompresses bytes into specified buffer. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * of bytes uncompressed. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * needsInput() or needsDictionary() should be called in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * determine if more input data or a preset dictionary is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * In the latter case, getAdler() can be used to get the Adler-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * value of the dictionary required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param b the buffer for the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param len the maximum number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return the actual number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception DataFormatException if the compressed data format is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   247
    public int inflate(byte[] b, int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        throws DataFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   256
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   257
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   258
            int thisLen = this.len;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   259
            int n = inflateBytes(zsRef.address(), b, off, len);
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   260
            bytesWritten += n;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   261
            bytesRead += (thisLen - this.len);
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   262
            return n;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   263
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Uncompresses bytes into specified buffer. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * of bytes uncompressed. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * needsInput() or needsDictionary() should be called in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * determine if more input data or a preset dictionary is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * In the latter case, getAdler() can be used to get the Adler-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * value of the dictionary required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param b the buffer for the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return the actual number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception DataFormatException if the compressed data format is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public int inflate(byte[] b) throws DataFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return inflate(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   287
    public int getAdler() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   288
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   289
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   290
            return getAdler(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   291
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns the total number of compressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return the total number of compressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   308
     * Returns the total number of compressed bytes input so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @return the total (non-negative) number of compressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   313
    public long getBytesRead() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   314
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   315
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   316
            return bytesRead;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   317
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns the total number of uncompressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return the total number of uncompressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   334
     * Returns the total number of uncompressed bytes output so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return the total (non-negative) number of uncompressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   339
    public long getBytesWritten() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   340
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   341
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   342
            return bytesWritten;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   343
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Resets inflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   349
    public void reset() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   350
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   351
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   352
            reset(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   353
            buf = defaultBuf;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   354
            finished = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   355
            needDict = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   356
            off = len = 0;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   357
            bytesRead = bytesWritten = 0;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   358
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Closes the decompressor and discards any unprocessed input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * This method should be called when the decompressor is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * being used, but will also be called automatically by the finalize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * method. Once this method is called, the behavior of the Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * object is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   368
    public void end() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   369
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   370
            long addr = zsRef.address();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   371
            zsRef.clear();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   372
            if (addr != 0) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   373
                end(addr);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   374
                buf = null;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   375
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Closes the decompressor when garbage is collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private void ensureOpen () {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   387
        assert Thread.holdsLock(zsRef);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   388
        if (zsRef.address() == 0)
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   389
            throw new NullPointerException("Inflater has been closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
7815
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   392
    boolean ended() {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   393
        synchronized (zsRef) {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   394
            return zsRef.address() == 0;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   395
        }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   396
    }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   397
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   398
    private static native void initIDs();
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   399
    private static native long init(boolean nowrap);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   400
    private static native void setDictionary(long addr, byte[] b, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                             int len);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   402
    private native int inflateBytes(long addr, byte[] b, int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            throws DataFormatException;
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   404
    private static native int getAdler(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   405
    private static native void reset(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   406
    private static native void end(long addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}