src/java.base/share/classes/java/util/zip/Deflater.java
author martin
Wed, 23 May 2018 10:34:18 -0700
changeset 50238 a9307f400f5a
parent 49834 99644c75eaed
child 52305 52a97e06a5e3
permissions -rw-r--r--
8203328: Rename EFS in java.util.zip internals to something meaningful Reviewed-by: sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
     2
 * Copyright (c) 1996, 2018, 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;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    29
import java.lang.ref.Reference;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    30
import java.nio.ByteBuffer;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    31
import java.nio.ReadOnlyBufferException;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    32
import java.util.Objects;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    33
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
    34
import jdk.internal.ref.CleanerFactory;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    35
import sun.nio.ch.DirectBuffer;
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class provides support for general purpose compression using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * popular ZLIB compression library. The ZLIB compression library was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * initially developed as part of the PNG graphics standard and is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * 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
    42
 * the <a href="package-summary.html#package.description">java.util.zip
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * package description</a>.
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    44
 * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    45
 * This class deflates sequences of bytes into ZLIB compressed data format.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    46
 * The input byte sequence is provided in either byte array or byte buffer,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    47
 * via one of the {@code setInput()} methods. The output byte sequence is
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    48
 * written to the output byte array or byte buffer passed to the
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    49
 * {@code deflate()} methods.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    50
 * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
    51
 * 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
    52
 * 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
    53
 * {@code Inflater}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     // Encode a String into bytes
5469
18ea05ea56b9 6937842: Unreadable \uXXXX in javadoc
martin
parents: 5194
diff changeset
    58
 *     String inputString = "blahblahblah";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     byte[] input = inputString.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     // Compress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     byte[] output = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     Deflater compresser = new Deflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     compresser.setInput(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     compresser.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *     int compressedDataLength = compresser.deflate(output);
2940
c6fe11c24d43 6808625: Incomplete code sample in Deflater javadoc
sherman
parents: 2
diff changeset
    67
 *     compresser.end();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     // Decompress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     Inflater decompresser = new Inflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     decompresser.setInput(output, 0, compressedDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     byte[] result = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     int resultLength = decompresser.inflate(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *     decompresser.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     // Decode the bytes into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     String outputString = new String(result, 0, resultLength, "UTF-8");
50238
a9307f400f5a 8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents: 49834
diff changeset
    78
 * } catch (java.io.UnsupportedEncodingException ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * } catch (java.util.zip.DataFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    85
 * @apiNote
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    86
 * 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
    87
 * 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
    88
 * 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
    89
 * 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
    90
 * 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
    91
 *
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    92
 * @implSpec
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    93
 * 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
    94
 * 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
    95
 * 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
    96
 * 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
    97
 * is deprecated to be removed.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    98
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @see         Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @author      David Connelly
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44534
diff changeset
   101
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 */
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   103
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   104
public class Deflater {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   105
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   106
    private final DeflaterZStreamRef zsRef;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   107
    private ByteBuffer input = ZipUtils.defaultBuf;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   108
    private byte[] inputArray;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   109
    private int inputPos, inputLim;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private int level, strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private boolean setParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private boolean finish, finished;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   113
    private long bytesRead;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   114
    private long bytesWritten;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Compression method for the deflate algorithm (the only one currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static final int DEFLATED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Compression level for no compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static final int NO_COMPRESSION = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Compression level for fastest compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static final int BEST_SPEED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Compression level for best compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static final int BEST_COMPRESSION = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static final int DEFAULT_COMPRESSION = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Compression strategy best used for data consisting mostly of small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * values with a somewhat random distribution. Forces more Huffman coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * and less string matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static final int FILTERED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Compression strategy for Huffman coding only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static final int HUFFMAN_ONLY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Default compression strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static final int DEFAULT_STRATEGY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   159
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   160
     * 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
   161
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   162
     * @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
   163
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   164
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   165
    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
   166
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
     * 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
   169
     * 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
   170
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   171
     * @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
   172
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   173
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   174
    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
   175
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   176
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   177
     * 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
   178
     * 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
   179
     * compression.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   180
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   181
     * @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
   182
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   183
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   184
    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
   185
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   186
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   187
     * Flush mode to use at the end of output.  Can only be provided by the
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   188
     * user by way of {@link #finish()}.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   189
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   190
    private static final int FINISH = 4;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   191
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static {
45924
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
   193
        ZipUtils.loadLibrary();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * If 'nowrap' is true then the ZLIB header and checksum fields will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * not be used in order to support the compression format used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param nowrap if true then use GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public Deflater(int level, boolean nowrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        this.strategy = DEFAULT_STRATEGY;
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   207
        this.zsRef = DeflaterZStreamRef.get(this,
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   208
                                    init(level, DEFAULT_STRATEGY, nowrap));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public Deflater(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this(level, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Creates a new compressor with the default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public Deflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        this(DEFAULT_COMPRESSION, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   229
     * Sets input data for compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   230
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   231
     * One of the {@code setInput()} methods should be called whenever
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   232
     * {@code needsInput()} returns true indicating that more input data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   233
     * is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   234
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   235
     * @param input the input data bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   240
    public void setInput(byte[] input, int off, int len) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   241
        if (off < 0 || len < 0 || off > input.length - len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   244
        synchronized (zsRef) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   245
            this.input = null;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   246
            this.inputArray = input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   247
            this.inputPos = off;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   248
            this.inputLim = off + len;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   249
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   253
     * Sets input data for compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   254
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   255
     * One of the {@code setInput()} methods should be called whenever
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   256
     * {@code needsInput()} returns true indicating that more input data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   257
     * is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   258
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   259
     * @param input the input data bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   262
    public void setInput(byte[] input) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   263
        setInput(input, 0, input.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   267
     * Sets input data for compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   268
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   269
     * One of the {@code setInput()} methods should be called whenever
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   270
     * {@code needsInput()} returns true indicating that more input data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   271
     * is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   272
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   273
     * The given buffer's position will be advanced as deflate
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   274
     * operations are performed, up to the buffer's limit.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   275
     * The input buffer may be modified (refilled) between deflate
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   276
     * operations; doing so is equivalent to creating a new buffer
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   277
     * and setting it with this method.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   278
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   279
     * Modifying the input buffer's contents, position, or limit
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   280
     * concurrently with an deflate operation will result in
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   281
     * undefined behavior, which may include incorrect operation
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   282
     * results or operation failure.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   283
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   284
     * @param input the input data bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   285
     * @see Deflater#needsInput
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   286
     * @since 11
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   288
    public void setInput(ByteBuffer input) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   289
        Objects.requireNonNull(input);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   290
        synchronized (zsRef) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   291
            this.input = input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   292
            this.inputArray = null;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   293
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * decompression.
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   302
     * @param dictionary the dictionary data bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   303
     * @param off the start offset of the data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   304
     * @param len the length of the data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   305
     * @see Inflater#inflate
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   306
     * @see Inflater#getAdler
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   307
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   308
    public void setDictionary(byte[] dictionary, int off, int len) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   309
        if (off < 0 || len < 0 || off > dictionary.length - len) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   310
            throw new ArrayIndexOutOfBoundsException();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   311
        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   312
        synchronized (zsRef) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   313
            ensureOpen();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   314
            setDictionary(zsRef.address(), dictionary, off, len);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   315
        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   316
    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   317
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   318
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   319
     * Sets preset dictionary for compression. A preset dictionary is used
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   320
     * when the history buffer can be predetermined. When the data is later
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   321
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   322
     * in order to get the Adler-32 value of the dictionary required for
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   323
     * decompression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   324
     * @param dictionary the dictionary data bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   328
    public void setDictionary(byte[] dictionary) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   329
        setDictionary(dictionary, 0, dictionary.length);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   330
    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   331
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   332
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   333
     * Sets preset dictionary for compression. A preset dictionary is used
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   334
     * when the history buffer can be predetermined. When the data is later
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   335
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   336
     * in order to get the Adler-32 value of the dictionary required for
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   337
     * decompression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   338
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   339
     * The bytes in given byte buffer will be fully consumed by this method.  On
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   340
     * return, its position will equal its limit.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   341
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   342
     * @param dictionary the dictionary data bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   343
     * @see Inflater#inflate
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   344
     * @see Inflater#getAdler
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   345
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   346
    public void setDictionary(ByteBuffer dictionary) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   347
        synchronized (zsRef) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   348
            int position = dictionary.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   349
            int remaining = Math.max(dictionary.limit() - position, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   350
            ensureOpen();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   351
            if (dictionary.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   352
                long address = ((DirectBuffer) dictionary).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   353
                try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   354
                    setDictionaryBuffer(zsRef.address(), address + position, remaining);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   355
                } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   356
                    Reference.reachabilityFence(dictionary);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   357
                }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   358
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   359
                byte[] array = ZipUtils.getBufferArray(dictionary);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   360
                int offset = ZipUtils.getBufferOffset(dictionary);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   361
                setDictionary(zsRef.address(), array, offset + position, remaining);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   362
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   363
            dictionary.position(position + remaining);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Sets the compression strategy to the specified value.
19865
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   369
     *
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   370
     * <p> If the compression strategy is changed, the next invocation
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   371
     * 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
   372
     * 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
   373
     * effect only after that invocation.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   374
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param strategy the new compression strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @exception IllegalArgumentException if the compression strategy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *                                     invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   379
    public void setStrategy(int strategy) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        switch (strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
          case DEFAULT_STRATEGY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
          case FILTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
          case HUFFMAN_ONLY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   388
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   389
            if (this.strategy != strategy) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   390
                this.strategy = strategy;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   391
                setParams = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   392
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
19865
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   397
     * Sets the compression level to the specified value.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   398
     *
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   399
     * <p> If the compression level is changed, the next invocation
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   400
     * of {@code deflate} will compress the input available so far
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   401
     * 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
   402
     * take effect only after that invocation.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   403
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param level the new compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   407
    public void setLevel(int level) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new IllegalArgumentException("invalid compression level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   411
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   412
            if (this.level != level) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   413
                this.level = level;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   414
                setParams = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   415
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   420
     * Returns true if no data remains in the input buffer. This can
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   421
     * be used to determine if one of the {@code setInput()} methods should be
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   422
     * called in order to provide more input.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   423
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @return true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * should be called in order to provide more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public boolean needsInput() {
32995
a62c89adce3d 8038502: Deflater.needsInput() should use synchronization
coffeys
parents: 32649
diff changeset
   428
        synchronized (zsRef) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   429
            ByteBuffer input = this.input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   430
            return input == null ? inputLim == inputPos : ! input.hasRemaining();
32995
a62c89adce3d 8038502: Deflater.needsInput() should use synchronization
coffeys
parents: 32649
diff changeset
   431
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * When called, indicates that compression should end with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * contents of the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   438
    public void finish() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   439
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   440
            finish = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   441
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Returns true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   450
    public boolean finished() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   451
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   452
            return finished;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   453
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   457
     * 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
   458
     * 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
   459
     * 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
   460
     * 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
   461
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   462
     * <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
   463
     * 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
   464
     * 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
   465
     * {@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
   466
     *
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   467
     * @param output the buffer for the compressed data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @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
   470
     * @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
   471
     *         output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   472
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   473
    public int deflate(byte[] output, int off, int len) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   474
        return deflate(output, off, len, NO_FLUSH);
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   475
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   476
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   477
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   478
     * 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
   479
     * 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
   480
     * 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
   481
     * 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
   482
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   483
     * <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
   484
     * 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
   485
     * 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
   486
     * {@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
   487
     *
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   488
     * @param output the buffer for the compressed data
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   489
     * @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
   490
     *         output buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   492
    public int deflate(byte[] output) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   493
        return deflate(output, 0, output.length, NO_FLUSH);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   494
    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   495
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   496
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   497
     * Compresses the input data and fills specified buffer with compressed
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   498
     * data. Returns actual number of bytes of compressed data. A return value
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   499
     * of 0 indicates that {@link #needsInput() needsInput} should be called
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   500
     * in order to determine if more input data is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   501
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   502
     * <p>This method uses {@link #NO_FLUSH} as its compression flush mode.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   503
     * An invocation of this method of the form {@code deflater.deflate(output)}
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   504
     * yields the same result as the invocation of
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   505
     * {@code deflater.deflate(output, Deflater.NO_FLUSH)}.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   506
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   507
     * @param output the buffer for the compressed data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   508
     * @return the actual number of bytes of compressed data written to the
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   509
     *         output buffer
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   510
     * @since 11
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   511
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   512
    public int deflate(ByteBuffer output) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   513
        return deflate(output, NO_FLUSH);
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   514
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   515
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   516
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   517
     * 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
   518
     * 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
   519
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   520
     * <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
   521
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   522
     * <ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   523
     * <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
   524
     * 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
   525
     * 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
   526
     * 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
   527
     * 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
   528
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   529
     * <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
   530
     * 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
   531
     * 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
   532
     * 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
   533
     * 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
   534
     * 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
   535
     * 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
   536
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   537
     * <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
   538
     * {@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
   539
     * 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
   540
     * 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
   541
     * 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
   542
     * compression.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   543
     * </ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   544
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   545
     * <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
   546
     * 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
   547
     * 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
   548
     * {@code flush} parameter and more output space. Make sure that
7701abdf2fb4 8133170: Deflater.deflate with small output buffers fails
sherman
parents: 32995
diff changeset
   549
     * {@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
   550
     * 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
   551
     * invoked.
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   552
     *
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   553
     * <p>If the {@link #setInput(ByteBuffer)} method was called to provide a buffer
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   554
     * for input, the input buffer's position will be advanced by the number of bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   555
     * consumed by this operation.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   556
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   557
     * @param output the buffer for the compressed data
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   558
     * @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
   559
     * @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
   560
     * @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
   561
     * @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
   562
     *         the output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   563
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   564
     * @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
   565
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   566
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   567
    public int deflate(byte[] output, int off, int len, int flush) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   568
        if (off < 0 || len < 0 || off > output.length - len) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   569
            throw new ArrayIndexOutOfBoundsException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   571
        if (flush != NO_FLUSH && flush != SYNC_FLUSH && flush != FULL_FLUSH) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   572
            throw new IllegalArgumentException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   574
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   575
            ensureOpen();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   576
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   577
            ByteBuffer input = this.input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   578
            if (finish) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   579
                // disregard given flush mode in this case
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   580
                flush = FINISH;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   581
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   582
            int params;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   583
            if (setParams) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   584
                // bit 0: true to set params
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   585
                // bit 1-2: strategy (0, 1, or 2)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   586
                // bit 3-31: level (0..9 or -1)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   587
                params = 1 | strategy << 1 | level << 3;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   588
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   589
                params = 0;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   590
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   591
            int inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   592
            long result;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   593
            if (input == null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   594
                inputPos = this.inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   595
                result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   596
                    inputArray, inputPos, inputLim - inputPos,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   597
                    output, off, len,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   598
                    flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   599
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   600
                inputPos = input.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   601
                int inputRem = Math.max(input.limit() - inputPos, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   602
                if (input.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   603
                    try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   604
                        long inputAddress = ((DirectBuffer) input).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   605
                        result = deflateBufferBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   606
                            inputAddress + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   607
                            output, off, len,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   608
                            flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   609
                    } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   610
                        Reference.reachabilityFence(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   611
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   612
                } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   613
                    byte[] inputArray = ZipUtils.getBufferArray(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   614
                    int inputOffset = ZipUtils.getBufferOffset(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   615
                    result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   616
                        inputArray, inputOffset + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   617
                        output, off, len,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   618
                        flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   619
                }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   620
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   621
            int read = (int) (result & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   622
            int written = (int) (result >>> 31 & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   623
            if ((result >>> 62 & 1) != 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   624
                finished = true;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   625
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   626
            if (params != 0 && (result >>> 63 & 1) == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   627
                setParams = false;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   628
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   629
            if (input != null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   630
                input.position(inputPos + read);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   631
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   632
                this.inputPos = inputPos + read;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   633
            }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   634
            bytesWritten += written;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   635
            bytesRead += read;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   636
            return written;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   637
        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   638
    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   639
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   640
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   641
     * Compresses the input data and fills the specified buffer with compressed
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   642
     * data. Returns actual number of bytes of data compressed.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   643
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   644
     * <p>Compression flush mode is one of the following three modes:
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   645
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   646
     * <ul>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   647
     * <li>{@link #NO_FLUSH}: allows the deflater to decide how much data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   648
     * to accumulate, before producing output, in order to achieve the best
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   649
     * compression (should be used in normal use scenario). A return value
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   650
     * of 0 in this flush mode indicates that {@link #needsInput()} should
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   651
     * be called in order to determine if more input data is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   652
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   653
     * <li>{@link #SYNC_FLUSH}: all pending output in the deflater is flushed,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   654
     * to the specified output buffer, so that an inflater that works on
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   655
     * compressed data can get all input data available so far (In particular
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   656
     * the {@link #needsInput()} returns {@code true} after this invocation
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   657
     * if enough output space is provided). Flushing with {@link #SYNC_FLUSH}
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   658
     * may degrade compression for some compression algorithms and so it
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   659
     * should be used only when necessary.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   660
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   661
     * <li>{@link #FULL_FLUSH}: all pending output is flushed out as with
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   662
     * {@link #SYNC_FLUSH}. The compression state is reset so that the inflater
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   663
     * that works on the compressed output data can restart from this point
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   664
     * if previous compressed data has been damaged or if random access is
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   665
     * desired. Using {@link #FULL_FLUSH} too often can seriously degrade
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   666
     * compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   667
     * </ul>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   668
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   669
     * <p>In the case of {@link #FULL_FLUSH} or {@link #SYNC_FLUSH}, if
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   670
     * the return value is equal to the {@linkplain ByteBuffer#remaining() remaining space}
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   671
     * of the buffer, this method should be invoked again with the same
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   672
     * {@code flush} parameter and more output space. Make sure that
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   673
     * the buffer has at least 6 bytes of remaining space to avoid the
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   674
     * flush marker (5 bytes) being repeatedly output to the output buffer
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   675
     * every time this method is invoked.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   676
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   677
     * <p>On success, the position of the given {@code output} byte buffer will be
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   678
     * advanced by as many bytes as were produced by the operation, which is equal
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   679
     * to the number returned by this method.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   680
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   681
     * <p>If the {@link #setInput(ByteBuffer)} method was called to provide a buffer
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   682
     * for input, the input buffer's position will be advanced by the number of bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   683
     * consumed by this operation.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   684
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   685
     * @param output the buffer for the compressed data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   686
     * @param flush the compression flush mode
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   687
     * @return the actual number of bytes of compressed data written to
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   688
     *         the output buffer
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   689
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   690
     * @throws IllegalArgumentException if the flush mode is invalid
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   691
     * @since 11
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   692
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   693
    public int deflate(ByteBuffer output, int flush) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   694
        if (output.isReadOnly()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   695
            throw new ReadOnlyBufferException();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   696
        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   697
        if (flush != NO_FLUSH && flush != SYNC_FLUSH && flush != FULL_FLUSH) {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   698
            throw new IllegalArgumentException();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   699
        }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   700
        synchronized (zsRef) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   701
            ensureOpen();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   702
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   703
            ByteBuffer input = this.input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   704
            if (finish) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   705
                // disregard given flush mode in this case
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   706
                flush = FINISH;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   707
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   708
            int params;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   709
            if (setParams) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   710
                // bit 0: true to set params
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   711
                // bit 1-2: strategy (0, 1, or 2)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   712
                // bit 3-31: level (0..9 or -1)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   713
                params = 1 | strategy << 1 | level << 3;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   714
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   715
                params = 0;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   716
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   717
            int outputPos = output.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   718
            int outputRem = Math.max(output.limit() - outputPos, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   719
            int inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   720
            long result;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   721
            if (input == null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   722
                inputPos = this.inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   723
                if (output.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   724
                    long outputAddress = ((DirectBuffer) output).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   725
                    try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   726
                        result = deflateBytesBuffer(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   727
                            inputArray, inputPos, inputLim - inputPos,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   728
                            outputAddress + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   729
                            flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   730
                    } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   731
                        Reference.reachabilityFence(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   732
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   733
                } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   734
                    byte[] outputArray = ZipUtils.getBufferArray(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   735
                    int outputOffset = ZipUtils.getBufferOffset(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   736
                    result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   737
                        inputArray, inputPos, inputLim - inputPos,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   738
                        outputArray, outputOffset + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   739
                        flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   740
                }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   741
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   742
                inputPos = input.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   743
                int inputRem = Math.max(input.limit() - inputPos, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   744
                if (input.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   745
                    long inputAddress = ((DirectBuffer) input).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   746
                    try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   747
                        if (output.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   748
                            long outputAddress = outputPos + ((DirectBuffer) output).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   749
                            try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   750
                                result = deflateBufferBuffer(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   751
                                    inputAddress + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   752
                                    outputAddress, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   753
                                    flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   754
                            } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   755
                                Reference.reachabilityFence(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   756
                            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   757
                        } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   758
                            byte[] outputArray = ZipUtils.getBufferArray(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   759
                            int outputOffset = ZipUtils.getBufferOffset(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   760
                            result = deflateBufferBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   761
                                inputAddress + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   762
                                outputArray, outputOffset + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   763
                                flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   764
                        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   765
                    } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   766
                        Reference.reachabilityFence(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   767
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   768
                } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   769
                    byte[] inputArray = ZipUtils.getBufferArray(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   770
                    int inputOffset = ZipUtils.getBufferOffset(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   771
                    if (output.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   772
                        long outputAddress = ((DirectBuffer) output).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   773
                        try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   774
                            result = deflateBytesBuffer(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   775
                                inputArray, inputOffset + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   776
                                outputAddress + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   777
                                flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   778
                        } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   779
                            Reference.reachabilityFence(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   780
                        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   781
                    } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   782
                        byte[] outputArray = ZipUtils.getBufferArray(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   783
                        int outputOffset = ZipUtils.getBufferOffset(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   784
                        result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   785
                            inputArray, inputOffset + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   786
                            outputArray, outputOffset + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   787
                            flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   788
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   789
                }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   790
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   791
            int read = (int) (result & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   792
            int written = (int) (result >>> 31 & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   793
            if ((result >>> 62 & 1) != 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   794
                finished = true;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   795
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   796
            if (params != 0 && (result >>> 63 & 1) == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   797
                setParams = false;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   798
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   799
            if (input != null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   800
                input.position(inputPos + read);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   801
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   802
                this.inputPos = inputPos + read;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   803
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   804
            output.position(outputPos + written);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   805
            bytesWritten += written;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   806
            bytesRead += read;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   807
            return written;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   808
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   815
    public int getAdler() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   816
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   817
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   818
            return getAdler(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   819
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * Returns the total number of uncompressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @return the total number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   836
     * Returns the total number of uncompressed bytes input so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @return the total (non-negative) number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   841
    public long getBytesRead() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   842
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   843
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   844
            return bytesRead;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   845
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Returns the total number of compressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @return the total number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   862
     * Returns the total number of compressed bytes output so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @return the total (non-negative) number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   867
    public long getBytesWritten() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   868
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   869
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   870
            return bytesWritten;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   871
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Resets deflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * Keeps current compression level and strategy settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   878
    public void reset() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   879
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   880
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   881
            reset(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   882
            finish = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   883
            finished = false;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   884
            input = ZipUtils.defaultBuf;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   885
            inputArray = null;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   886
            bytesRead = bytesWritten = 0;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   887
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Closes the compressor and discards any unprocessed input.
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   892
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * 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
   894
     * 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
   895
     * Deflater object is undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   897
    public void end() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   898
        synchronized (zsRef) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   899
            zsRef.clean();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   900
            input = ZipUtils.defaultBuf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Closes the compressor when garbage is collected.
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 39497
diff changeset
   906
     *
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   907
     * @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
   908
     *     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
   909
     *     {@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
   910
     *     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
   911
     *     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
   912
     *     {@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
   913
     *     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
   914
     *     when the instance becomes unreachable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   916
    @Deprecated(since="9", forRemoval=true)
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   917
    protected void finalize() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    private void ensureOpen() {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   920
        assert Thread.holdsLock(zsRef);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   921
        if (zsRef.address() == 0)
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   922
            throw new NullPointerException("Deflater has been closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   925
    private static native long init(int level, int strategy, boolean nowrap);
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   926
    private static native void setDictionary(long addr, byte[] b, int off,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   927
                                             int len);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   928
    private static native void setDictionaryBuffer(long addr, long bufAddress, int len);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   929
    private native long deflateBytesBytes(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   930
        byte[] inputArray, int inputOff, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   931
        byte[] outputArray, int outputOff, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   932
        int flush, int params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   933
    private native long deflateBytesBuffer(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   934
        byte[] inputArray, int inputOff, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   935
        long outputAddress, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   936
        int flush, int params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   937
    private native long deflateBufferBytes(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   938
        long inputAddress, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   939
        byte[] outputArray, int outputOff, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   940
        int flush, int params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   941
    private native long deflateBufferBuffer(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   942
        long inputAddress, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   943
        long outputAddress, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   944
        int flush, int params);
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   945
    private static native int getAdler(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   946
    private static native void reset(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   947
    private static native void end(long addr);
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   948
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   949
    /**
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   950
     * 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
   951
     * 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
   952
     * the Deflater is ended, closed or cleaned.
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   953
     */
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   954
    static class DeflaterZStreamRef implements Runnable {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   955
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   956
        private long address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   957
        private final Cleanable cleanable;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   958
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   959
        private DeflaterZStreamRef(Deflater owner, long addr) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   960
            this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   961
            this.address = addr;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   962
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   963
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   964
        long address() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   965
            return address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   966
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   967
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   968
        void clean() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   969
            cleanable.clean();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   970
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   971
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   972
        public synchronized void run() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   973
            long addr = address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   974
            address = 0;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   975
            if (addr != 0) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   976
                end(addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   977
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   978
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   979
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   980
        /*
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   981
         * 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
   982
         * overridden, uses {@code finalizer} mechanism for resource cleanup. So
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   983
         * {@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
   984
         * 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
   985
         * removed from {@code Deflater}.
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   986
         */
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   987
        static DeflaterZStreamRef get(Deflater owner, long addr) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   988
            Class<?> clz = owner.getClass();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   989
            while (clz != Deflater.class) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   990
                try {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   991
                    clz.getDeclaredMethod("end");
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   992
                    return new FinalizableZStreamRef(owner, addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   993
                } catch (NoSuchMethodException nsme) {}
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   994
                clz = clz.getSuperclass();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   995
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   996
            return new DeflaterZStreamRef(owner, addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   997
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   998
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   999
        private static class FinalizableZStreamRef extends DeflaterZStreamRef {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1000
            final Deflater owner;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1001
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1002
            FinalizableZStreamRef (Deflater owner, long addr) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1003
                super(null, addr);
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1004
                this.owner = owner;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1005
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1006
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1007
            @Override
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1008
            void clean() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1009
                run();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1010
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1011
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1012
            @Override
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1013
            @SuppressWarnings("deprecation")
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1014
            protected void finalize() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1015
                owner.end();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1016
            }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1017
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
  1018
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
}