jdk/src/java.base/share/classes/java/util/zip/Inflater.java
author alanb
Wed, 19 Jul 2017 19:30:08 +0100
changeset 45924 8bbd04c0791e
parent 45434 4582657c7260
child 47004 b7e72fc752c9
permissions -rw-r--r--
8184917: System.initPhase1 does not need to pre-load libzip Reviewed-by: redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
     2
 * Copyright (c) 1996, 2017, 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
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44534
diff changeset
    71
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
class Inflater {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
    76
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
    77
    private final ZStreamRef zsRef;
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    78
    private byte[] buf = defaultBuf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private int off, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private boolean needDict;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
    82
    private long bytesRead;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
    83
    private long bytesWritten;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    85
    private static final byte[] defaultBuf = new byte[0];
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
45924
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
    88
        ZipUtils.loadLibrary();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Creates a new decompressor. If the parameter 'nowrap' is true then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * the ZLIB header and checksum fields will not be used. This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * compatibility with the compression format used by both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Note: When using the 'nowrap' option it is also necessary to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * an extra "dummy" byte as input. This is required by the ZLIB native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * library in order to support certain optimizations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param nowrap if true then support GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public Inflater(boolean nowrap) {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   104
        zsRef = new ZStreamRef(init(nowrap));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Creates a new decompressor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public Inflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Sets input data for decompression. Should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * needsInput() returns true indicating that more input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param off the start offset of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param len the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   123
    public void setInput(byte[] b, int off, int len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   130
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   131
            this.buf = b;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   132
            this.off = off;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   133
            this.len = len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   134
        }
2
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
     * Sets input data for decompression. Should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * needsInput() returns true indicating that more input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void setInput(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        setInput(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Sets the preset dictionary to the given array of bytes. Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * called when inflate() returns 0 and needsDictionary() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * indicating that a preset dictionary is required. The method getAdler()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * can be used to get the Adler-32 value of the dictionary needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   159
    public void setDictionary(byte[] b, int off, int len) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   160
        if (b == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   166
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   167
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   168
            setDictionary(zsRef.address(), b, off, len);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   169
            needDict = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   170
        }
2
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
     * Sets the preset dictionary to the given array of bytes. Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * called when inflate() returns 0 and needsDictionary() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * indicating that a preset dictionary is required. The method getAdler()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * can be used to get the Adler-32 value of the dictionary needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void setDictionary(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        setDictionary(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the total number of bytes remaining in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This can be used to find out what bytes still remain in the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * buffer after decompression has finished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the total number of bytes remaining in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   192
    public int getRemaining() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   193
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   194
            return len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   195
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Returns true if no data remains in the input buffer. This can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * be used to determine if #setInput should be called in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * to provide more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return true if no data remains in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   204
    public boolean needsInput() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   205
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   206
            return len <= 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   207
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returns true if a preset dictionary is needed for decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return true if a preset dictionary is needed for decompression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @see Inflater#setDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   215
    public boolean needsDictionary() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   216
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   217
            return needDict;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   218
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns true if the end of the compressed data stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return true if the end of the compressed data stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   227
    public boolean finished() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   228
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   229
            return finished;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   230
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Uncompresses bytes into specified buffer. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * of bytes uncompressed. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * needsInput() or needsDictionary() should be called in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * determine if more input data or a preset dictionary is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * In the latter case, getAdler() can be used to get the Adler-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * value of the dictionary required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param b the buffer for the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param len the maximum number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return the actual number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception DataFormatException if the compressed data format is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   248
    public int inflate(byte[] b, int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        throws DataFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   257
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   258
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   259
            int thisLen = this.len;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   260
            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
   261
            bytesWritten += n;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   262
            bytesRead += (thisLen - this.len);
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   263
            return n;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   264
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Uncompresses bytes into specified buffer. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * of bytes uncompressed. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * needsInput() or needsDictionary() should be called in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * determine if more input data or a preset dictionary is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * In the latter case, getAdler() can be used to get the Adler-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * value of the dictionary required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param b the buffer for the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the actual number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception DataFormatException if the compressed data format is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public int inflate(byte[] b) throws DataFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return inflate(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   288
    public int getAdler() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   289
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   290
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   291
            return getAdler(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   292
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns the total number of compressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @return the total number of compressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   309
     * Returns the total number of compressed bytes input so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return the total (non-negative) number of compressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   314
    public long getBytesRead() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   315
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   316
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   317
            return bytesRead;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   318
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns the total number of uncompressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return the total number of uncompressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   335
     * Returns the total number of uncompressed bytes output so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return the total (non-negative) number of uncompressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   340
    public long getBytesWritten() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   341
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   342
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   343
            return bytesWritten;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   344
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Resets inflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   350
    public void reset() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   351
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   352
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   353
            reset(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   354
            buf = defaultBuf;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   355
            finished = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   356
            needDict = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   357
            off = len = 0;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 9035
diff changeset
   358
            bytesRead = bytesWritten = 0;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   359
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Closes the decompressor and discards any unprocessed input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * This method should be called when the decompressor is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * being used, but will also be called automatically by the finalize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * method. Once this method is called, the behavior of the Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * object is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   369
    public void end() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   370
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   371
            long addr = zsRef.address();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   372
            zsRef.clear();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   373
            if (addr != 0) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   374
                end(addr);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   375
                buf = null;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   376
            }
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Closes the decompressor when garbage is collected.
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   382
     *
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   383
     * @deprecated The {@code finalize} method has been deprecated.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   384
     *     Subclasses that override {@code finalize} in order to perform cleanup
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   385
     *     should be modified to use alternative cleanup mechanisms and
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   386
     *     to remove the overriding {@code finalize} method.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   387
     *     When overriding the {@code finalize} method, its implementation must explicitly
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   388
     *     ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   389
     *     See the specification for {@link Object#finalize()} for further
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   390
     *     information about migration options.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32649
diff changeset
   392
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private void ensureOpen () {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   398
        assert Thread.holdsLock(zsRef);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   399
        if (zsRef.address() == 0)
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   400
            throw new NullPointerException("Inflater has been closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
7815
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   403
    boolean ended() {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   404
        synchronized (zsRef) {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   405
            return zsRef.address() == 0;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   406
        }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   407
    }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   408
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   409
    private static native void initIDs();
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   410
    private static native long init(boolean nowrap);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   411
    private static native void setDictionary(long addr, byte[] b, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                             int len);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   413
    private native int inflateBytes(long addr, byte[] b, int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            throws DataFormatException;
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   415
    private static native int getAdler(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   416
    private static native void reset(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   417
    private static native void end(long addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
}