jdk/src/share/classes/java/util/zip/Deflater.java
author sherman
Wed, 04 Mar 2009 09:26:41 -0800
changeset 2178 5a730fff4d0d
parent 2 90ce3da70b43
child 2940 c6fe11c24d43
permissions -rw-r--r--
6812879: Excess code line in ArrayList method Summary: Removed the line of "oldData" which is no longer used. Reviewed-by: martin
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     // Decompress the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     Inflater decompresser = new Inflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     decompresser.setInput(output, 0, compressedDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     byte[] result = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     int resultLength = decompresser.inflate(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     decompresser.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     // Decode the bytes into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     String outputString = new String(result, 0, resultLength, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * } catch(java.io.UnsupportedEncodingException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * } catch (java.util.zip.DataFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     // handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @see         Inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
class Deflater {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private long strm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private byte[] buf = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int off, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private int level, strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean setParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean finish, finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Compression method for the deflate algorithm (the only one currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final int DEFLATED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Compression level for no compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int NO_COMPRESSION = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Compression level for fastest compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static final int BEST_SPEED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Compression level for best compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final int BEST_COMPRESSION = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static final int DEFAULT_COMPRESSION = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Compression strategy best used for data consisting mostly of small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * values with a somewhat random distribution. Forces more Huffman coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * and less string matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static final int FILTERED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Compression strategy for Huffman coding only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static final int HUFFMAN_ONLY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Default compression strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static final int DEFAULT_STRATEGY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * If 'nowrap' is true then the ZLIB header and checksum fields will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * not be used in order to support the compression format used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * both GZIP and PKZIP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param nowrap if true then use GZIP compatible compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public Deflater(int level, boolean nowrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.strategy = DEFAULT_STRATEGY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        strm = init(level, DEFAULT_STRATEGY, nowrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Creates a new compressor using the specified compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public Deflater(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this(level, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Creates a new compressor with the default compression level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Compressed data will be generated in ZLIB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public Deflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this(DEFAULT_COMPRESSION, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Sets input data for compression. This should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * needsInput() returns true indicating that more input data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public synchronized void setInput(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (b== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this.buf = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        this.off = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.len = len;
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
     * Sets input data for compression. This should be called whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * needsInput() returns true indicating that more input data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param b the input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see Deflater#needsInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public void setInput(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        setInput(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public synchronized void setDictionary(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (strm == 0 || b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        setDictionary(strm, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Sets preset dictionary for compression. A preset dictionary is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * when the history buffer can be predetermined. When the data is later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * in order to get the Adler-32 value of the dictionary required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * decompression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param b the dictionary data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see Inflater#inflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see Inflater#getAdler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void setDictionary(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        setDictionary(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Sets the compression strategy to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param strategy the new compression strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception IllegalArgumentException if the compression strategy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *                                     invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public synchronized void setStrategy(int strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        switch (strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
          case DEFAULT_STRATEGY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
          case FILTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
          case HUFFMAN_ONLY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (this.strategy != strategy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            this.strategy = strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            setParams = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Sets the current compression level to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param level the new compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public synchronized void setLevel(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new IllegalArgumentException("invalid compression level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (this.level != level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            setParams = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * should be called in order to provide more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return true if the input data buffer is empty and setInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * should be called in order to provide more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public boolean needsInput() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return len <= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * When called, indicates that compression should end with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * contents of the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public synchronized void finish() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        finish = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Returns true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return true if the end of the compressed data output stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public synchronized boolean finished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return finished;
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
     * Fills specified buffer with compressed data. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * of bytes of compressed data. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * needsInput() should be called in order to determine if more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param b the buffer for the compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param len the maximum number of bytes of compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return the actual number of bytes of compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public synchronized int deflate(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return deflateBytes(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Fills specified buffer with compressed data. Returns actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * of bytes of compressed data. A return value of 0 indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * needsInput() should be called in order to determine if more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * data is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param b the buffer for the compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return the actual number of bytes of compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public int deflate(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return deflate(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Returns the ADLER-32 value of the uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return the ADLER-32 value of the uncompressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public synchronized int getAdler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return getAdler(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns the total number of uncompressed bytes input so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return the total number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public int getTotalIn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return (int) getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns the total number of uncompressed bytes input so far.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return the total (non-negative) number of uncompressed bytes input so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public synchronized long getBytesRead() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return getBytesRead(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns the total number of compressed bytes output so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <p>Since the number of bytes may be greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * the preferred means of obtaining this information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return the total number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public int getTotalOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return (int) getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Returns the total number of compressed bytes output so far.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @return the total (non-negative) number of compressed bytes output so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public synchronized long getBytesWritten() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return getBytesWritten(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Resets deflater so that a new set of input data can be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Keeps current compression level and strategy settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        reset(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        finish = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        finished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        off = len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Closes the compressor and discards any unprocessed input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * This method should be called when the compressor is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * being used, but will also be called automatically by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * finalize() method. Once this method is called, the behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * of the Deflater object is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public synchronized void end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (strm != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            end(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            strm = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Closes the compressor when garbage is collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (strm == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private native static long init(int level, int strategy, boolean nowrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private native static void setDictionary(long strm, byte[] b, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                             int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private native int deflateBytes(byte[] b, int off, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private native static int getAdler(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private native static long getBytesRead(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    private native static long getBytesWritten(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private native static void reset(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private native static void end(long strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
}