src/java.base/share/classes/java/util/zip/Deflater.java
author jjg
Mon, 03 Dec 2018 16:14:15 -0800
changeset 52807 bbf85239e37c
parent 52305 52a97e06a5e3
child 58242 94bb65cb37d3
permissions -rw-r--r--
8214744: Unnecessary <p> tags in java.util.zip.Deflater Reviewed-by: mchung, lancea
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
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @see         Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @author      David Connelly
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44534
diff changeset
    94
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    96
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
    97
public class Deflater {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
    98
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
    99
    private final DeflaterZStreamRef zsRef;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   100
    private ByteBuffer input = ZipUtils.defaultBuf;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   101
    private byte[] inputArray;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   102
    private int inputPos, inputLim;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private int level, strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private boolean setParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private boolean finish, finished;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   106
    private long bytesRead;
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   107
    private long bytesWritten;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Compression method for the deflate algorithm (the only one currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static final int DEFLATED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Compression level for no compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static final int NO_COMPRESSION = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Compression level for fastest compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static final int BEST_SPEED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Compression level for best compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static final int BEST_COMPRESSION = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static final int DEFAULT_COMPRESSION = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Compression strategy best used for data consisting mostly of small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * values with a somewhat random distribution. Forces more Huffman coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * and less string matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static final int FILTERED = 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 for Huffman coding only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static final int HUFFMAN_ONLY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Default compression strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static final int DEFAULT_STRATEGY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   152
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   153
     * 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
   154
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   155
     * @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
   156
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   157
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   158
    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
   159
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   160
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   161
     * 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
   162
     * 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
   163
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   164
     * @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
   165
     * @since 1.7
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
    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
   168
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   169
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   170
     * 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
   171
     * 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
   172
     * compression.
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
     * @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
   175
     * @since 1.7
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
    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
   178
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   179
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   180
     * 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
   181
     * user by way of {@link #finish()}.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   182
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   183
    private static final int FINISH = 4;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   184
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static {
45924
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
   186
        ZipUtils.loadLibrary();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * If 'nowrap' is true then the ZLIB header and checksum fields will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * not be used in order to support the compression format used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param nowrap if true then use GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public Deflater(int level, boolean nowrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.strategy = DEFAULT_STRATEGY;
52305
52a97e06a5e3 8212129: Remove finalize methods from java.util.zip.ZipFIle/Inflator/Deflator
lancea
parents: 50238
diff changeset
   200
        this.zsRef = new DeflaterZStreamRef(this,
52a97e06a5e3 8212129: Remove finalize methods from java.util.zip.ZipFIle/Inflator/Deflator
lancea
parents: 50238
diff changeset
   201
                init(level, DEFAULT_STRATEGY, nowrap));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public Deflater(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        this(level, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Creates a new compressor with the default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public Deflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        this(DEFAULT_COMPRESSION, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   222
     * Sets input data for compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   223
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   224
     * 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
   225
     * {@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
   226
     * is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   227
     * @param input the input data bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   232
    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
   233
        if (off < 0 || len < 0 || off > input.length - len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   236
        synchronized (zsRef) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   237
            this.input = null;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   238
            this.inputArray = input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   239
            this.inputPos = off;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   240
            this.inputLim = off + len;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   241
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   245
     * Sets input data for compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   246
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   247
     * 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
   248
     * {@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
   249
     * is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   250
     * @param input the input data bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see Deflater#needsInput
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
    public void setInput(byte[] input) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   254
        setInput(input, 0, input.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   258
     * Sets input data for compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   259
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   260
     * 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
   261
     * {@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
   262
     * is required.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   263
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   264
     * 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
   265
     * 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
   266
     * 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
   267
     * 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
   268
     * and setting it with this method.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   269
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   270
     * 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
   271
     * concurrently with an deflate operation will result in
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   272
     * undefined behavior, which may include incorrect operation
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   273
     * results or operation failure.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   274
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   275
     * @param input the input data bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   276
     * @see Deflater#needsInput
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   277
     * @since 11
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   279
    public void setInput(ByteBuffer input) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   280
        Objects.requireNonNull(input);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   281
        synchronized (zsRef) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   282
            this.input = input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   283
            this.inputArray = null;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   284
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * decompression.
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   293
     * @param dictionary the dictionary data bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   294
     * @param off the start offset of the data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   295
     * @param len the length of the data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   296
     * @see Inflater#inflate
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   297
     * @see Inflater#getAdler
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   298
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   299
    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
   300
        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
   301
            throw new ArrayIndexOutOfBoundsException();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   302
        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   303
        synchronized (zsRef) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   304
            ensureOpen();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   305
            setDictionary(zsRef.address(), dictionary, off, len);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   306
        }
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
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   309
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   310
     * 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
   311
     * 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
   312
     * 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
   313
     * 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
   314
     * decompression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   315
     * @param dictionary the dictionary data bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   319
    public void setDictionary(byte[] dictionary) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   320
        setDictionary(dictionary, 0, dictionary.length);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   321
    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   322
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   323
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   324
     * 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
   325
     * 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
   326
     * 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
   327
     * 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
   328
     * decompression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   329
     * <p>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   330
     * 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
   331
     * return, its position will equal its limit.
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
     * @param dictionary the dictionary data bytes
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   334
     * @see Inflater#inflate
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   335
     * @see Inflater#getAdler
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   336
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   337
    public void setDictionary(ByteBuffer dictionary) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   338
        synchronized (zsRef) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   339
            int position = dictionary.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   340
            int remaining = Math.max(dictionary.limit() - position, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   341
            ensureOpen();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   342
            if (dictionary.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   343
                long address = ((DirectBuffer) dictionary).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   344
                try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   345
                    setDictionaryBuffer(zsRef.address(), address + position, remaining);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   346
                } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   347
                    Reference.reachabilityFence(dictionary);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   348
                }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   349
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   350
                byte[] array = ZipUtils.getBufferArray(dictionary);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   351
                int offset = ZipUtils.getBufferOffset(dictionary);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   352
                setDictionary(zsRef.address(), array, offset + position, remaining);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   353
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   354
            dictionary.position(position + remaining);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   355
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Sets the compression strategy to the specified value.
19865
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   360
     *
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   361
     * <p> If the compression strategy is changed, the next invocation
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   362
     * 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
   363
     * 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
   364
     * effect only after that invocation.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   365
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param strategy the new compression strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @exception IllegalArgumentException if the compression strategy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *                                     invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   370
    public void setStrategy(int strategy) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        switch (strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
          case DEFAULT_STRATEGY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
          case FILTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
          case HUFFMAN_ONLY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   379
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   380
            if (this.strategy != strategy) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   381
                this.strategy = strategy;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   382
                setParams = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   383
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
19865
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   388
     * Sets the compression level to the specified value.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   389
     *
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   390
     * <p> If the compression level is changed, the next invocation
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   391
     * of {@code deflate} will compress the input available so far
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   392
     * 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
   393
     * take effect only after that invocation.
b68185846a71 8020687: Deflater.setLevel does not work as expected
sherman
parents: 18560
diff changeset
   394
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param level the new compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   398
    public void setLevel(int level) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new IllegalArgumentException("invalid compression level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   402
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   403
            if (this.level != level) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   404
                this.level = level;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   405
                setParams = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   406
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   411
     * 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
   412
     * 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
   413
     * called in order to provide more input.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   414
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * should be called in order to provide more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public boolean needsInput() {
32995
a62c89adce3d 8038502: Deflater.needsInput() should use synchronization
coffeys
parents: 32649
diff changeset
   419
        synchronized (zsRef) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   420
            ByteBuffer input = this.input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   421
            return input == null ? inputLim == inputPos : ! input.hasRemaining();
32995
a62c89adce3d 8038502: Deflater.needsInput() should use synchronization
coffeys
parents: 32649
diff changeset
   422
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * When called, indicates that compression should end with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * contents of the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   429
    public void finish() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   430
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   431
            finish = true;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   432
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Returns true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @return true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   441
    public boolean finished() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   442
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   443
            return finished;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   444
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   448
     * 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
   449
     * 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
   450
     * 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
   451
     * 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
   452
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   453
     * <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
   454
     * 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
   455
     * 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
   456
     * {@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
   457
     *
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   458
     * @param output the buffer for the compressed data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @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
   461
     * @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
   462
     *         output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   463
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   464
    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
   465
        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
   466
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   467
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   468
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   469
     * 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
   470
     * 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
   471
     * 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
   472
     * 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
   473
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   474
     * <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
   475
     * 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
   476
     * 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
   477
     * {@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
   478
     *
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   479
     * @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
   480
     * @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
   481
     *         output buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   483
    public int deflate(byte[] output) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   484
        return deflate(output, 0, output.length, NO_FLUSH);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   485
    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   486
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   487
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   488
     * 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
   489
     * 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
   490
     * 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
   491
     * 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
   492
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   493
     * <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
   494
     * 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
   495
     * yields the same result as the invocation of
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   496
     * {@code deflater.deflate(output, Deflater.NO_FLUSH)}.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   497
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   498
     * @param output the buffer for the compressed data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   499
     * @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
   500
     *         output buffer
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   501
     * @since 11
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   502
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   503
    public int deflate(ByteBuffer output) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   504
        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
   505
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   506
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   507
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   508
     * 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
   509
     * 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
   510
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   511
     * <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
   512
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   513
     * <ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   514
     * <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
   515
     * 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
   516
     * 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
   517
     * 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
   518
     * 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
   519
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   520
     * <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
   521
     * 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
   522
     * 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
   523
     * 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
   524
     * 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
   525
     * 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
   526
     * 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
   527
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   528
     * <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
   529
     * {@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
   530
     * 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
   531
     * 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
   532
     * 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
   533
     * compression.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   534
     * </ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   535
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   536
     * <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
   537
     * 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
   538
     * 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
   539
     * {@code flush} parameter and more output space. Make sure that
7701abdf2fb4 8133170: Deflater.deflate with small output buffers fails
sherman
parents: 32995
diff changeset
   540
     * {@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
   541
     * 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
   542
     * invoked.
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   543
     *
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   544
     * <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
   545
     * 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
   546
     * consumed by this operation.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   547
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   548
     * @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
   549
     * @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
   550
     * @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
   551
     * @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
   552
     * @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
   553
     *         the output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   554
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   555
     * @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
   556
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   557
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   558
    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
   559
        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
   560
            throw new ArrayIndexOutOfBoundsException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   562
        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
   563
            throw new IllegalArgumentException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   565
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   566
            ensureOpen();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   567
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   568
            ByteBuffer input = this.input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   569
            if (finish) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   570
                // disregard given flush mode in this case
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   571
                flush = FINISH;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   572
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   573
            int params;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   574
            if (setParams) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   575
                // bit 0: true to set params
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   576
                // bit 1-2: strategy (0, 1, or 2)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   577
                // bit 3-31: level (0..9 or -1)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   578
                params = 1 | strategy << 1 | level << 3;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   579
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   580
                params = 0;
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 inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   583
            long result;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   584
            if (input == null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   585
                inputPos = this.inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   586
                result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   587
                    inputArray, inputPos, inputLim - inputPos,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   588
                    output, off, len,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   589
                    flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   590
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   591
                inputPos = input.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   592
                int inputRem = Math.max(input.limit() - inputPos, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   593
                if (input.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   594
                    try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   595
                        long inputAddress = ((DirectBuffer) input).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   596
                        result = deflateBufferBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   597
                            inputAddress + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   598
                            output, off, len,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   599
                            flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   600
                    } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   601
                        Reference.reachabilityFence(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   602
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   603
                } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   604
                    byte[] inputArray = ZipUtils.getBufferArray(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   605
                    int inputOffset = ZipUtils.getBufferOffset(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   606
                    result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   607
                        inputArray, inputOffset + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   608
                        output, off, len,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   609
                        flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   610
                }
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
            int read = (int) (result & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   613
            int written = (int) (result >>> 31 & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   614
            if ((result >>> 62 & 1) != 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   615
                finished = true;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   616
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   617
            if (params != 0 && (result >>> 63 & 1) == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   618
                setParams = false;
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
            if (input != null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   621
                input.position(inputPos + read);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   622
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   623
                this.inputPos = inputPos + read;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   624
            }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   625
            bytesWritten += written;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   626
            bytesRead += read;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   627
            return written;
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
    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   630
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   631
    /**
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   632
     * 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
   633
     * 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
   634
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   635
     * <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
   636
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   637
     * <ul>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   638
     * <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
   639
     * 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
   640
     * 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
   641
     * 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
   642
     * 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
   643
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   644
     * <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
   645
     * 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
   646
     * 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
   647
     * 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
   648
     * 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
   649
     * 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
   650
     * should be used only when necessary.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   651
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   652
     * <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
   653
     * {@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
   654
     * 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
   655
     * 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
   656
     * 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
   657
     * compression.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   658
     * </ul>
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   659
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   660
     * <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
   661
     * 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
   662
     * 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
   663
     * {@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
   664
     * 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
   665
     * 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
   666
     * every time this method is invoked.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   667
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   668
     * <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
   669
     * 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
   670
     * to the number returned by this method.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   671
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   672
     * <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
   673
     * 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
   674
     * consumed by this operation.
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   675
     *
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   676
     * @param output the buffer for the compressed data
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   677
     * @param flush the compression flush mode
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   678
     * @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
   679
     *         the output buffer
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
     * @throws IllegalArgumentException if the flush mode is invalid
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   682
     * @since 11
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   683
     */
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   684
    public int deflate(ByteBuffer output, int flush) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   685
        if (output.isReadOnly()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   686
            throw new ReadOnlyBufferException();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   687
        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   688
        if (flush != NO_FLUSH && flush != SYNC_FLUSH && flush != FULL_FLUSH) {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   689
            throw new IllegalArgumentException();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   690
        }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   691
        synchronized (zsRef) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   692
            ensureOpen();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   693
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   694
            ByteBuffer input = this.input;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   695
            if (finish) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   696
                // disregard given flush mode in this case
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   697
                flush = FINISH;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   698
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   699
            int params;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   700
            if (setParams) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   701
                // bit 0: true to set params
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   702
                // bit 1-2: strategy (0, 1, or 2)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   703
                // bit 3-31: level (0..9 or -1)
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   704
                params = 1 | strategy << 1 | level << 3;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   705
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   706
                params = 0;
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 outputPos = output.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   709
            int outputRem = Math.max(output.limit() - outputPos, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   710
            int inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   711
            long result;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   712
            if (input == null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   713
                inputPos = this.inputPos;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   714
                if (output.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   715
                    long outputAddress = ((DirectBuffer) output).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   716
                    try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   717
                        result = deflateBytesBuffer(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   718
                            inputArray, inputPos, inputLim - inputPos,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   719
                            outputAddress + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   720
                            flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   721
                    } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   722
                        Reference.reachabilityFence(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   723
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   724
                } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   725
                    byte[] outputArray = ZipUtils.getBufferArray(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   726
                    int outputOffset = ZipUtils.getBufferOffset(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   727
                    result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   728
                        inputArray, inputPos, inputLim - inputPos,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   729
                        outputArray, outputOffset + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   730
                        flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   731
                }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   732
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   733
                inputPos = input.position();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   734
                int inputRem = Math.max(input.limit() - inputPos, 0);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   735
                if (input.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   736
                    long inputAddress = ((DirectBuffer) input).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   737
                    try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   738
                        if (output.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   739
                            long outputAddress = outputPos + ((DirectBuffer) output).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   740
                            try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   741
                                result = deflateBufferBuffer(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   742
                                    inputAddress + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   743
                                    outputAddress, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   744
                                    flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   745
                            } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   746
                                Reference.reachabilityFence(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   747
                            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   748
                        } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   749
                            byte[] outputArray = ZipUtils.getBufferArray(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   750
                            int outputOffset = ZipUtils.getBufferOffset(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   751
                            result = deflateBufferBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   752
                                inputAddress + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   753
                                outputArray, outputOffset + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   754
                                flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   755
                        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   756
                    } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   757
                        Reference.reachabilityFence(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   758
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   759
                } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   760
                    byte[] inputArray = ZipUtils.getBufferArray(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   761
                    int inputOffset = ZipUtils.getBufferOffset(input);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   762
                    if (output.isDirect()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   763
                        long outputAddress = ((DirectBuffer) output).address();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   764
                        try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   765
                            result = deflateBytesBuffer(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   766
                                inputArray, inputOffset + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   767
                                outputAddress + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   768
                                flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   769
                        } finally {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   770
                            Reference.reachabilityFence(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   771
                        }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   772
                    } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   773
                        byte[] outputArray = ZipUtils.getBufferArray(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   774
                        int outputOffset = ZipUtils.getBufferOffset(output);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   775
                        result = deflateBytesBytes(zsRef.address(),
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   776
                            inputArray, inputOffset + inputPos, inputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   777
                            outputArray, outputOffset + outputPos, outputRem,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   778
                            flush, params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   779
                    }
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
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   782
            int read = (int) (result & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   783
            int written = (int) (result >>> 31 & 0x7fff_ffffL);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   784
            if ((result >>> 62 & 1) != 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   785
                finished = true;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   786
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   787
            if (params != 0 && (result >>> 63 & 1) == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   788
                setParams = false;
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
            if (input != null) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   791
                input.position(inputPos + read);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   792
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   793
                this.inputPos = inputPos + read;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   794
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   795
            output.position(outputPos + written);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   796
            bytesWritten += written;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   797
            bytesRead += read;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   798
            return written;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   799
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   806
    public int getAdler() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   807
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   808
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   809
            return getAdler(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   810
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Returns the total number of uncompressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @return the total number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   827
     * Returns the total number of uncompressed bytes input so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @return the total (non-negative) number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   832
    public long getBytesRead() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   833
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   834
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   835
            return bytesRead;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   836
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * Returns the total number of compressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @return the total number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
18560
75e936782d6b 8019228: Fix doclint issues in java.util.zip
darcy
parents: 14342
diff changeset
   853
     * Returns the total number of compressed bytes output so far.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @return the total (non-negative) number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   858
    public long getBytesWritten() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   859
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   860
            ensureOpen();
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   861
            return bytesWritten;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   862
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Resets deflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Keeps current compression level and strategy settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   869
    public void reset() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   870
        synchronized (zsRef) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   871
            ensureOpen();
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   872
            reset(zsRef.address());
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   873
            finish = false;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   874
            finished = false;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   875
            input = ZipUtils.defaultBuf;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   876
            inputArray = null;
13409
fe3ab27ef1a6 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native
sherman
parents: 7668
diff changeset
   877
            bytesRead = bytesWritten = 0;
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   878
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * Closes the compressor and discards any unprocessed input.
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   883
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * 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
   885
     * 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
   886
     * Deflater object is undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   888
    public void end() {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   889
        synchronized (zsRef) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47216
diff changeset
   890
            zsRef.clean();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   891
            input = ZipUtils.defaultBuf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    private void ensureOpen() {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   896
        assert Thread.holdsLock(zsRef);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   897
        if (zsRef.address() == 0)
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4178
diff changeset
   898
            throw new NullPointerException("Deflater has been closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   901
    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
   902
    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
   903
                                             int len);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   904
    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
   905
    private native long deflateBytesBytes(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   906
        byte[] inputArray, int inputOff, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   907
        byte[] outputArray, int outputOff, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   908
        int flush, int params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   909
    private native long deflateBytesBuffer(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   910
        byte[] inputArray, int inputOff, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   911
        long outputAddress, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   912
        int flush, int params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   913
    private native long deflateBufferBytes(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   914
        long inputAddress, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   915
        byte[] outputArray, int outputOff, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   916
        int flush, int params);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   917
    private native long deflateBufferBuffer(long addr,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   918
        long inputAddress, int inputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   919
        long outputAddress, int outputLen,
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 48337
diff changeset
   920
        int flush, int params);
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   921
    private static native int getAdler(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   922
    private static native void reset(long addr);
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32037
diff changeset
   923
    private static native void end(long addr);
48337
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   924
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   925
    /**
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   926
     * 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
   927
     * 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
   928
     * the Deflater is ended, closed or cleaned.
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   929
     */
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   930
    static class DeflaterZStreamRef implements Runnable {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   931
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   932
        private long address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   933
        private final Cleanable cleanable;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   934
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   935
        private DeflaterZStreamRef(Deflater owner, long addr) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   936
            this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   937
            this.address = addr;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   938
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   939
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   940
        long address() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   941
            return address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   942
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   943
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   944
        void clean() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   945
            cleanable.clean();
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   946
        }
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   947
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   948
        public synchronized void run() {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   949
            long addr = address;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   950
            address = 0;
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   951
            if (addr != 0) {
0ee20aad71c4 8193507: [REDO] Startup regression due to JDK-8185582
redestad
parents: 48238
diff changeset
   952
                end(addr);
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
        }
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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
}