jdk/src/share/classes/java/util/zip/Inflater.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7815 12cc7f77e459
child 13409 fe3ab27ef1a6
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7815
diff changeset
     2
 * Copyright (c) 1996, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and decompression of a string using <tt>Deflater</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <tt>Inflater</tt>.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    82
    private static final byte[] defaultBuf = new byte[0];
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
    83
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Creates a new decompressor. If the parameter 'nowrap' is true then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * the ZLIB header and checksum fields will not be used. This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * compatibility with the compression format used by both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Note: When using the 'nowrap' option it is also necessary to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * an extra "dummy" byte as input. This is required by the ZLIB native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * library in order to support certain optimizations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param nowrap if true then support GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public Inflater(boolean nowrap) {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   101
        zsRef = new ZStreamRef(init(nowrap));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Creates a new decompressor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public Inflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Sets input data for decompression. Should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * needsInput() returns true indicating that more input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param off the start offset of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param len the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   120
    public void setInput(byte[] b, int off, int len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   127
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   128
            this.buf = b;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   129
            this.off = off;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   130
            this.len = len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   131
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Sets input data for decompression. Should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * needsInput() returns true indicating that more input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void setInput(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        setInput(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Sets the preset dictionary to the given array of bytes. Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * called when inflate() returns 0 and needsDictionary() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * indicating that a preset dictionary is required. The method getAdler()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * can be used to get the Adler-32 value of the dictionary needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   156
    public void setDictionary(byte[] b, int off, int len) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   157
        if (b == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   163
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   164
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   165
            setDictionary(zsRef.address(), b, off, len);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   166
            needDict = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   167
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Sets the preset dictionary to the given array of bytes. Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * called when inflate() returns 0 and needsDictionary() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * indicating that a preset dictionary is required. The method getAdler()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * can be used to get the Adler-32 value of the dictionary needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void setDictionary(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        setDictionary(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns the total number of bytes remaining in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * This can be used to find out what bytes still remain in the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * buffer after decompression has finished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return the total number of bytes remaining in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   189
    public int getRemaining() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   190
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   191
            return len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   192
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns true if no data remains in the input buffer. This can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * be used to determine if #setInput should be called in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * to provide more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return true if no data remains in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   201
    public boolean needsInput() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   202
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   203
            return len <= 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   204
        }
2
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
     * Returns true if a preset dictionary is needed for decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return true if a preset dictionary is needed for decompression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see Inflater#setDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   212
    public boolean needsDictionary() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   213
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   214
            return needDict;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   215
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Returns true if the end of the compressed data stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return 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
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   224
    public boolean finished() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   225
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   226
            return finished;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   227
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Uncompresses bytes into specified buffer. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * of bytes uncompressed. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * needsInput() or needsDictionary() should be called in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * determine if more input data or a preset dictionary is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * In the latter case, getAdler() can be used to get the Adler-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * value of the dictionary required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param b the buffer for the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param len the maximum number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return the actual number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception DataFormatException if the compressed data format is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   245
    public int inflate(byte[] b, int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        throws DataFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   254
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   255
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   256
            return inflateBytes(zsRef.address(), b, off, len);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   257
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Uncompresses bytes into specified buffer. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * of bytes uncompressed. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * needsInput() or needsDictionary() should be called in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * determine if more input data or a preset dictionary is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * In the latter case, getAdler() can be used to get the Adler-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * value of the dictionary required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param b the buffer for the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return the actual number of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @exception DataFormatException if the compressed data format is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see Inflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see Inflater#needsDictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public int inflate(byte[] b) throws DataFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return inflate(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   281
    public int getAdler() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   282
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   283
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   284
            return getAdler(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   285
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns the total number of compressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return 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
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Returns the total number of compressed bytes input so far.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return the total (non-negative) number of compressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   307
    public long getBytesRead() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   308
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   309
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   310
            return getBytesRead(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   311
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns the total number of uncompressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return 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
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns the total number of uncompressed bytes output so far.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return the total (non-negative) number of uncompressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   333
    public long getBytesWritten() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   334
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   335
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   336
            return getBytesWritten(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   337
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Resets inflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   343
    public void reset() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   344
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   345
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   346
            reset(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   347
            buf = defaultBuf;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   348
            finished = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   349
            needDict = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   350
            off = len = 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   351
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Closes the decompressor and discards any unprocessed input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * This method should be called when the decompressor is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * being used, but will also be called automatically by the finalize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * method. Once this method is called, the behavior of the Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * object is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   361
    public void end() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   362
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   363
            long addr = zsRef.address();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   364
            zsRef.clear();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   365
            if (addr != 0) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   366
                end(addr);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   367
                buf = null;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   368
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Closes the decompressor when garbage is collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private void ensureOpen () {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   380
        assert Thread.holdsLock(zsRef);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   381
        if (zsRef.address() == 0)
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   382
            throw new NullPointerException("Inflater has been closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
7815
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   385
    boolean ended() {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   386
        synchronized (zsRef) {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   387
            return zsRef.address() == 0;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   388
        }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   389
    }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 5506
diff changeset
   390
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private native static void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private native static long init(boolean nowrap);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   393
    private native static void setDictionary(long addr, byte[] b, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                             int len);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   395
    private native int inflateBytes(long addr, byte[] b, int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throws DataFormatException;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   397
    private native static int getAdler(long addr);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   398
    private native static long getBytesRead(long addr);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   399
    private native static long getBytesWritten(long addr);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   400
    private native static void reset(long addr);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 1158
diff changeset
   401
    private native static void end(long addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
}