src/java.base/share/classes/java/util/zip/Deflater.java
author redestad
Thu, 14 Dec 2017 16:05:08 +0100
changeset 48337 0ee20aad71c4
parent 48238 9f225d4387e2
child 49834 99644c75eaed
permissions -rw-r--r--
8193507: [REDO] Startup regression due to JDK-8185582 Reviewed-by: alanb, rriggs Contributed-by: xueming.shen@oracle.com, claes.redestad@oracle.com
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: 39497
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: 5469
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: 5469
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: 5469
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5469
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5469
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
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
    28
import java.lang.ref.Cleaner.Cleanable;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
    29
import jdk.internal.ref.CleanerFactory;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class provides support for general purpose compression using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * popular ZLIB compression library. The ZLIB compression library was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * initially developed as part of the PNG graphics standard and is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * protected by patents. It is fully described in the specifications at
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 45924
diff changeset
    36
 * the <a href="package-summary.html#package.description">java.util.zip
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * package description</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <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
    40
 * 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
    41
 * {@code Inflater}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *     // Encode a String into bytes
5469
18ea05ea56b9 6937842: Unreadable \uXXXX in javadoc
martin
parents: 5194
diff changeset
    46
 *     String inputString = "blahblahblah";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     byte[] input = inputString.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     // Compress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     byte[] output = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     Deflater compresser = new Deflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *     compresser.setInput(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     compresser.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     int compressedDataLength = compresser.deflate(output);
2940
c6fe11c24d43 6808625: Incomplete code sample in Deflater javadoc
sherman
parents: 2
diff changeset
    55
 *     compresser.end();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     // Decompress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     Inflater decompresser = new Inflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     decompresser.setInput(output, 0, compressedDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     byte[] result = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     int resultLength = decompresser.inflate(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     decompresser.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     // Decode the bytes into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     String outputString = new String(result, 0, resultLength, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * } catch(java.io.UnsupportedEncodingException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * } catch (java.util.zip.DataFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    73
 * @apiNote
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    74
 * To release resources used by this {@code Deflater}, the {@link #end()} method
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    75
 * should be called explicitly. Subclasses are responsible for the cleanup of resources
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    76
 * acquired by the subclass. Subclasses that override {@link #finalize()} in order
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    77
 * to perform cleanup should be modified to use alternative cleanup mechanisms such
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    78
 * as {@link java.lang.ref.Cleaner} and remove the overriding {@code finalize} method.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    79
 *
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    80
 * @implSpec
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    81
 * If this {@code Deflater} has been subclassed and the {@code end} method has been
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    82
 * overridden, the {@code end} method will be called by the finalization when the
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    83
 * deflater is unreachable. But the subclasses should not depend on this specific
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    84
 * implementation; the finalization is not reliable and the {@code finalize} method
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    85
 * is deprecated to be removed.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    86
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see         Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @author      David Connelly
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44534
diff changeset
    89
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    91
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    92
public class Deflater {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
    93
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
    94
    private final DeflaterZStreamRef zsRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private byte[] buf = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private int off, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private int level, strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private boolean setParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private boolean finish, finished;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   100
    private long bytesRead;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   101
    private long bytesWritten;
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
     * Compression method for the deflate algorithm (the only one currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final int DEFLATED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Compression level for no compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static final int NO_COMPRESSION = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Compression level for fastest compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static final int BEST_SPEED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Compression level for best compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static final int BEST_COMPRESSION = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public static final int DEFAULT_COMPRESSION = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Compression strategy best used for data consisting mostly of small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * values with a somewhat random distribution. Forces more Huffman coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * and less string matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static final int FILTERED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Compression strategy for Huffman coding only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static final int HUFFMAN_ONLY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Default compression strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static final int DEFAULT_STRATEGY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   146
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   147
     * Compression flush mode used to achieve best compression result.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   148
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   149
     * @see Deflater#deflate(byte[], int, int, int)
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   150
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   151
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   152
    public static final int NO_FLUSH = 0;
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   153
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   154
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   155
     * Compression flush mode used to flush out all pending output; may
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   156
     * degrade compression for some compression algorithms.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   157
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   158
     * @see Deflater#deflate(byte[], int, int, int)
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   159
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   160
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   161
    public static final int SYNC_FLUSH = 2;
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   163
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   164
     * Compression flush mode used to flush out all pending output and
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   165
     * reset the deflater. Using this mode too often can seriously degrade
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   166
     * compression.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   167
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   168
     * @see Deflater#deflate(byte[], int, int, int)
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   169
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   170
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   171
    public static final int FULL_FLUSH = 3;
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   172
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static {
45924
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
   174
        ZipUtils.loadLibrary();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * If 'nowrap' is true then the ZLIB header and checksum fields will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * not be used in order to support the compression format used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param nowrap if true then use GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public Deflater(int level, boolean nowrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.strategy = DEFAULT_STRATEGY;
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   189
        this.zsRef = DeflaterZStreamRef.get(this,
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   190
                                    init(level, DEFAULT_STRATEGY, nowrap));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public Deflater(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this(level, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Creates a new compressor with the default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public Deflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        this(DEFAULT_COMPRESSION, false);
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
     * Sets input data for compression. This should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * needsInput() returns true indicating that more input data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   218
    public void setInput(byte[] b, int off, int len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (b== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   225
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   226
            this.buf = b;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   227
            this.off = off;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   228
            this.len = len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
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
     * Sets input data for compression. This should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * needsInput() returns true indicating that more input data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void setInput(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        setInput(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   254
    public void setDictionary(byte[] b, int off, int len) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   255
        if (b == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   261
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   262
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   263
            setDictionary(zsRef.address(), b, off, len);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
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
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public void setDictionary(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        setDictionary(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Sets the compression strategy to the specified value.
19865
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   283
     *
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   284
     * <p> If the compression strategy is changed, the next invocation
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   285
     * of {@code deflate} will compress the input available so far with
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   286
     * the old strategy (and may be flushed); the new strategy will take
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   287
     * effect only after that invocation.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   288
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param strategy the new compression strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @exception IllegalArgumentException if the compression strategy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *                                     invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   293
    public void setStrategy(int strategy) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        switch (strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
          case DEFAULT_STRATEGY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
          case FILTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
          case HUFFMAN_ONLY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   302
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   303
            if (this.strategy != strategy) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   304
                this.strategy = strategy;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   305
                setParams = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   306
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
19865
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   311
     * Sets the compression level to the specified value.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   312
     *
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   313
     * <p> If the compression level is changed, the next invocation
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   314
     * of {@code deflate} will compress the input available so far
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   315
     * with the old level (and may be flushed); the new level will
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   316
     * take effect only after that invocation.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   317
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param level the new compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   321
    public void setLevel(int level) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new IllegalArgumentException("invalid compression level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   325
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   326
            if (this.level != level) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   327
                this.level = level;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   328
                setParams = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   329
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * should be called in order to provide more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * should be called in order to provide more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public boolean needsInput() {
32995
a62c89adce3d 8038502: Deflater.needsInput() should use synchronization
coffeys
parents: 32649
diff changeset
   340
        synchronized (zsRef) {
a62c89adce3d 8038502: Deflater.needsInput() should use synchronization
coffeys
parents: 32649
diff changeset
   341
            return len <= 0;
a62c89adce3d 8038502: Deflater.needsInput() should use synchronization
coffeys
parents: 32649
diff changeset
   342
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * When called, indicates that compression should end with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * contents of the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   349
    public void finish() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   350
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   351
            finish = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   352
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   361
    public boolean finished() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   362
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   363
            return finished;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   364
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   368
     * Compresses the input data and fills specified buffer with compressed
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   369
     * data. Returns actual number of bytes of compressed data. A return value
4354
3a70dde80b3b 6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents: 4178
diff changeset
   370
     * of 0 indicates that {@link #needsInput() needsInput} should be called
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   371
     * in order to determine if more input data is required.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   372
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   373
     * <p>This method uses {@link #NO_FLUSH} as its compression flush mode.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   374
     * An invocation of this method of the form {@code deflater.deflate(b, off, len)}
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   375
     * yields the same result as the invocation of
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   376
     * {@code deflater.deflate(b, off, len, Deflater.NO_FLUSH)}.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   377
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @param b the buffer for the compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param len the maximum number of bytes of compressed data
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   381
     * @return the actual number of bytes of compressed data written to the
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   382
     *         output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   383
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   384
    public int deflate(byte[] b, int off, int len) {
4178
61284fdd478f 6894950: test/java/util/zip/Bounds.java fails with OoutOfMemoryError
sherman
parents: 4162
diff changeset
   385
        return deflate(b, off, len, NO_FLUSH);
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   386
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   387
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   388
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   389
     * Compresses the input data and fills specified buffer with compressed
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   390
     * data. Returns actual number of bytes of compressed data. A return value
4354
3a70dde80b3b 6905029: Broken links in Deflater and DeflaterOutputStream javadoc
martin
parents: 4178
diff changeset
   391
     * of 0 indicates that {@link #needsInput() needsInput} should be called
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   392
     * in order to determine if more input data is required.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   393
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   394
     * <p>This method uses {@link #NO_FLUSH} as its compression flush mode.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   395
     * An invocation of this method of the form {@code deflater.deflate(b)}
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   396
     * yields the same result as the invocation of
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   397
     * {@code deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH)}.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   398
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   399
     * @param b the buffer for the compressed data
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   400
     * @return the actual number of bytes of compressed data written to the
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   401
     *         output buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   403
    public int deflate(byte[] b) {
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   404
        return deflate(b, 0, b.length, NO_FLUSH);
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   405
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   406
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   407
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   408
     * Compresses the input data and fills the specified buffer with compressed
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   409
     * data. Returns actual number of bytes of data compressed.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   410
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   411
     * <p>Compression flush mode is one of the following three modes:
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   412
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   413
     * <ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   414
     * <li>{@link #NO_FLUSH}: allows the deflater to decide how much data
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   415
     * to accumulate, before producing output, in order to achieve the best
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   416
     * compression (should be used in normal use scenario). A return value
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   417
     * of 0 in this flush mode indicates that {@link #needsInput()} should
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   418
     * be called in order to determine if more input data is required.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   419
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   420
     * <li>{@link #SYNC_FLUSH}: all pending output in the deflater is flushed,
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   421
     * to the specified output buffer, so that an inflater that works on
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   422
     * compressed data can get all input data available so far (In particular
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   423
     * the {@link #needsInput()} returns {@code true} after this invocation
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   424
     * if enough output space is provided). Flushing with {@link #SYNC_FLUSH}
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   425
     * may degrade compression for some compression algorithms and so it
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   426
     * should be used only when necessary.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   427
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   428
     * <li>{@link #FULL_FLUSH}: all pending output is flushed out as with
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   429
     * {@link #SYNC_FLUSH}. The compression state is reset so that the inflater
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   430
     * that works on the compressed output data can restart from this point
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   431
     * if previous compressed data has been damaged or if random access is
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   432
     * desired. Using {@link #FULL_FLUSH} too often can seriously degrade
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   433
     * compression.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   434
     * </ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   435
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   436
     * <p>In the case of {@link #FULL_FLUSH} or {@link #SYNC_FLUSH}, if
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   437
     * the return value is {@code len}, the space available in output
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   438
     * buffer {@code b}, this method should be invoked again with the same
39497
7701abdf2fb4 8133170: Deflater.deflate with small output buffers fails
sherman
parents: 32995
diff changeset
   439
     * {@code flush} parameter and more output space. Make sure that
7701abdf2fb4 8133170: Deflater.deflate with small output buffers fails
sherman
parents: 32995
diff changeset
   440
     * {@code len} is greater than 6 to avoid flush marker (5 bytes) being
7701abdf2fb4 8133170: Deflater.deflate with small output buffers fails
sherman
parents: 32995
diff changeset
   441
     * repeatedly output to the output buffer every time this method is
7701abdf2fb4 8133170: Deflater.deflate with small output buffers fails
sherman
parents: 32995
diff changeset
   442
     * invoked.
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   443
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   444
     * @param b the buffer for the compressed data
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   445
     * @param off the start offset of the data
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   446
     * @param len the maximum number of bytes of compressed data
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   447
     * @param flush the compression flush mode
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   448
     * @return the actual number of bytes of compressed data written to
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   449
     *         the output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   450
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   451
     * @throws IllegalArgumentException if the flush mode is invalid
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   452
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   453
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   454
    public int deflate(byte[] b, int off, int len, int flush) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   461
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   462
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   463
            if (flush == NO_FLUSH || flush == SYNC_FLUSH ||
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   464
                flush == FULL_FLUSH) {
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   465
                int thisLen = this.len;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   466
                int n = deflateBytes(zsRef.address(), b, off, len, flush);
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   467
                bytesWritten += n;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   468
                bytesRead += (thisLen - this.len);
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   469
                return n;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   470
            }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   471
            throw new IllegalArgumentException();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   472
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   479
    public int getAdler() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   480
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   481
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   482
            return getAdler(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   483
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Returns the total number of uncompressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @return the total number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   500
     * Returns the total number of uncompressed bytes input so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return the total (non-negative) number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   505
    public long getBytesRead() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   506
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   507
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   508
            return bytesRead;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   509
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Returns the total number of compressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return the total number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   526
     * Returns the total number of compressed bytes output so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return the total (non-negative) number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   531
    public long getBytesWritten() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   532
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   533
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   534
            return bytesWritten;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   535
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Resets deflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Keeps current compression level and strategy settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   542
    public void reset() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   543
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   544
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   545
            reset(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   546
            finish = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   547
            finished = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   548
            off = len = 0;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   549
            bytesRead = bytesWritten = 0;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   550
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Closes the compressor and discards any unprocessed input.
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   555
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * This method should be called when the compressor is no longer
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   557
     * being used. Once this method is called, the behavior of the
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   558
     * Deflater object is undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   560
    public void end() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   561
        synchronized (zsRef) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   562
            zsRef.clean();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   563
            buf = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Closes the compressor when garbage is collected.
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 39497
diff changeset
   569
     *
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   570
     * @deprecated The {@code finalize} method has been deprecated and will be
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   571
     *     removed. It is implemented as a no-op. Subclasses that override
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   572
     *     {@code finalize} in order to perform cleanup should be modified to use
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   573
     *     alternative cleanup mechanisms and to remove the overriding {@code finalize}
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   574
     *     method. The recommended cleanup for compressor is to explicitly call
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   575
     *     {@code end} method when it is no longer in use. If the {@code end} is
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   576
     *     not invoked explicitly the resource of the compressor will be released
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   577
     *     when the instance becomes unreachable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   579
    @Deprecated(since="9", forRemoval=true)
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   580
    protected void finalize() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private void ensureOpen() {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   583
        assert Thread.holdsLock(zsRef);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   584
        if (zsRef.address() == 0)
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   585
            throw new NullPointerException("Deflater has been closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private static native void initIDs();
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   589
    private static native long init(int level, int strategy, boolean nowrap);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   590
    private static native void setDictionary(long addr, byte[] b, int off, int len);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   591
    private native int deflateBytes(long addr, byte[] b, int off, int len,
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   592
                                    int flush);
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   593
    private static native int getAdler(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   594
    private static native void reset(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   595
    private static native void end(long addr);
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   596
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   597
    /**
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   598
     * A reference to the native zlib's z_stream structure. It also
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   599
     * serves as the "cleaner" to clean up the native resource when
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   600
     * the Deflater is ended, closed or cleaned.
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   601
     */
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   602
    static class DeflaterZStreamRef implements Runnable {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   603
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   604
        private long address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   605
        private final Cleanable cleanable;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   606
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   607
        private DeflaterZStreamRef(Deflater owner, long addr) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   608
            this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   609
            this.address = addr;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   610
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   611
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   612
        long address() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   613
            return address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   614
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   615
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   616
        void clean() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   617
            cleanable.clean();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   618
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   619
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   620
        public synchronized void run() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   621
            long addr = address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   622
            address = 0;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   623
            if (addr != 0) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   624
                end(addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   625
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   626
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   627
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   628
        /*
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   629
         * If {@code Deflater} has been subclassed and the {@code end} method is
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   630
         * overridden, uses {@code finalizer} mechanism for resource cleanup. So
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   631
         * {@code end} method can be called when the {@code Deflater} is unreachable.
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   632
         * This mechanism will be removed when the {@code finalize} method is
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   633
         * removed from {@code Deflater}.
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   634
         */
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   635
        static DeflaterZStreamRef get(Deflater owner, long addr) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   636
            Class<?> clz = owner.getClass();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   637
            while (clz != Deflater.class) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   638
                try {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   639
                    clz.getDeclaredMethod("end");
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   640
                    return new FinalizableZStreamRef(owner, addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   641
                } catch (NoSuchMethodException nsme) {}
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   642
                clz = clz.getSuperclass();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   643
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   644
            return new DeflaterZStreamRef(owner, addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   645
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   646
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   647
        private static class FinalizableZStreamRef extends DeflaterZStreamRef {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   648
            final Deflater owner;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   649
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   650
            FinalizableZStreamRef (Deflater owner, long addr) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   651
                super(null, addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   652
                this.owner = owner;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   653
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   654
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   655
            @Override
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   656
            void clean() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   657
                run();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   658
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   659
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   660
            @Override
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   661
            @SuppressWarnings("deprecation")
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   662
            protected void finalize() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   663
                owner.end();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   664
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   665
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   666
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
}