jdk/src/share/classes/java/util/zip/Deflater.java
author sherman
Wed, 21 Oct 2009 11:50:25 -0700
changeset 4162 425328b81201
parent 2940 c6fe11c24d43
child 4178 61284fdd478f
permissions -rw-r--r--
4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH) Summary: Add sync_flush option into Deflater/DefalterOutputStream Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class provides support for general purpose compression using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * popular ZLIB compression library. The ZLIB compression library was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * initially developed as part of the PNG graphics standard and is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * protected by patents. It is fully described in the specifications at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the <a href="package-summary.html#package_description">java.util.zip
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * package description</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>The following code fragment demonstrates a trivial compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and decompression of a string using <tt>Deflater</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <tt>Inflater</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *     // Encode a String into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     String inputString = "blahblahblah\u20AC\u20AC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     byte[] input = inputString.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     // Compress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     byte[] output = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     Deflater compresser = new Deflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     compresser.setInput(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     compresser.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     int compressedDataLength = compresser.deflate(output);
2940
c6fe11c24d43 6808625: Incomplete code sample in Deflater javadoc
sherman
parents: 2
diff changeset
    52
 *     compresser.end();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     // Decompress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     Inflater decompresser = new Inflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     decompresser.setInput(output, 0, compressedDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     byte[] result = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     int resultLength = decompresser.inflate(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     decompresser.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     // Decode the bytes into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     String outputString = new String(result, 0, resultLength, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * } catch(java.io.UnsupportedEncodingException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * } catch (java.util.zip.DataFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @see         Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
class Deflater {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private long strm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private byte[] buf = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private int off, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private int level, strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean setParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean finish, finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Compression method for the deflate algorithm (the only one currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final int DEFLATED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Compression level for no compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final int NO_COMPRESSION = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Compression level for fastest compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static final int BEST_SPEED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Compression level for best compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static final int BEST_COMPRESSION = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final int DEFAULT_COMPRESSION = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Compression strategy best used for data consisting mostly of small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * values with a somewhat random distribution. Forces more Huffman coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * and less string matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static final int FILTERED = 1;
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 strategy for Huffman coding only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static final int HUFFMAN_ONLY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Default compression strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static final int DEFAULT_STRATEGY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   125
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   126
     * 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
   127
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   128
     * @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
   129
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   130
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   131
    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
   132
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   133
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   134
     * 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
   135
     * 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
   136
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   137
     * @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
   138
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   139
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   140
    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
   141
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   142
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   143
     * 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
   144
     * 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
   145
     * compression.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   146
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   147
     * @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
   148
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   149
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   150
    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
   151
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * If 'nowrap' is true then the ZLIB header and checksum fields will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * not be used in order to support the compression format used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param nowrap if true then use GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Deflater(int level, boolean nowrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.strategy = DEFAULT_STRATEGY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        strm = init(level, DEFAULT_STRATEGY, nowrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public Deflater(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this(level, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Creates a new compressor with the default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public Deflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this(DEFAULT_COMPRESSION, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Sets input data for compression. This should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * needsInput() returns true indicating that more input data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public synchronized void setInput(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (b== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        this.buf = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        this.off = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.len = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Sets input data for compression. This should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * needsInput() returns true indicating that more input data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void setInput(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        setInput(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public synchronized void setDictionary(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (strm == 0 || b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        setDictionary(strm, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void setDictionary(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        setDictionary(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Sets the compression strategy to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param strategy the new compression strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @exception IllegalArgumentException if the compression strategy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *                                     invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public synchronized void setStrategy(int strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        switch (strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
          case DEFAULT_STRATEGY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
          case FILTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
          case HUFFMAN_ONLY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (this.strategy != strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            this.strategy = strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            setParams = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Sets the current compression level to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param level the new compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public synchronized void setLevel(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new IllegalArgumentException("invalid compression level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (this.level != level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            setParams = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * should be called in order to provide more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * should be called in order to provide more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public boolean needsInput() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return len <= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * When called, indicates that compression should end with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * contents of the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public synchronized void finish() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        finish = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Returns true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public synchronized boolean finished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   319
     * 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
   320
     * data. Returns actual number of bytes of compressed data. A return value
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   321
     * of 0 indicates that {@link needsInput() needsInput} should be called
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   322
     * 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
   323
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   324
     * <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
   325
     * 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
   326
     * 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
   327
     * {@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
   328
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param b the buffer for the compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @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
   332
     * @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
   333
     *         output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   334
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   335
    public int deflate(byte[] b, int off, int len) {
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   336
        return deflateBytes(b, off, len, NO_FLUSH);
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   337
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   338
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   339
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   340
     * 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
   341
     * data. Returns actual number of bytes of compressed data. A return value
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   342
     * of 0 indicates that {@link needsInput() needsInput} should be called
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   343
     * 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
   344
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   345
     * <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
   346
     * 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
   347
     * 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
   348
     * {@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
   349
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   350
     * @param b the buffer for the compressed data
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   351
     * @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
   352
     *         output buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   354
    public int deflate(byte[] b) {
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   355
        return deflate(b, 0, b.length, NO_FLUSH);
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   356
    }
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   357
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   358
    /**
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   359
     * 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
   360
     * 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
   361
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   362
     * <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
   363
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   364
     * <ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   365
     * <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
   366
     * 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
   367
     * 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
   368
     * 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
   369
     * 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
   370
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   371
     * <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
   372
     * 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
   373
     * 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
   374
     * 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
   375
     * 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
   376
     * 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
   377
     * 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
   378
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   379
     * <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
   380
     * {@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
   381
     * 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
   382
     * 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
   383
     * 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
   384
     * compression.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   385
     * </ul>
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   386
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   387
     * <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
   388
     * 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
   389
     * buffer {@code b}, this method should be invoked again with the same
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   390
     * {@code flush} parameter and more output space.
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   391
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   392
     * @param b the buffer for the compressed data
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   393
     * @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
   394
     * @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
   395
     * @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
   396
     * @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
   397
     *         the output buffer
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   398
     *
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   399
     * @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
   400
     * @since 1.7
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   401
     */
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   402
    public synchronized int deflate(byte[] b, int off, int len, int flush) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   409
        if (flush == NO_FLUSH || flush == SYNC_FLUSH ||
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   410
            flush == FULL_FLUSH)
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   411
            return deflateBytes(b, off, len, flush);
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   412
        throw new IllegalArgumentException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public synchronized int getAdler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return getAdler(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Returns the total number of uncompressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return the total number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Returns the total number of uncompressed bytes input so far.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @return the total (non-negative) number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public synchronized long getBytesRead() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return getBytesRead(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Returns the total number of compressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @return the total number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Returns the total number of compressed bytes output so far.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @return the total (non-negative) number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public synchronized long getBytesWritten() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return getBytesWritten(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Resets deflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Keeps current compression level and strategy settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        reset(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        finish = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        finished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        off = len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Closes the compressor and discards any unprocessed input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * This method should be called when the compressor is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * being used, but will also be called automatically by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * finalize() method. Once this method is called, the behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * of the Deflater object is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public synchronized void end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (strm != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            end(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            strm = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Closes the compressor when garbage is collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (strm == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    private native static long init(int level, int strategy, boolean nowrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private native static void setDictionary(long strm, byte[] b, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                             int len);
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2940
diff changeset
   515
    private native int deflateBytes(byte[] b, int off, int len, int flush);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private native static int getAdler(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private native static long getBytesRead(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private native static long getBytesWritten(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private native static void reset(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private native static void end(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
}