emul/mini/src/main/java/java/util/zip/Inflater.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Jan 2013 14:00:17 +0100
branchjdk7-b147
changeset 609 48ef38e9677e
child 611 9839e9a75bcf
permissions -rw-r--r--
Adding classes necessary for usage of ZipInputStream
jaroslav@609
     1
/*
jaroslav@609
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@609
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@609
     4
 *
jaroslav@609
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@609
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@609
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@609
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@609
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@609
    10
 *
jaroslav@609
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@609
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@609
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@609
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@609
    15
 * accompanied this code).
jaroslav@609
    16
 *
jaroslav@609
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@609
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@609
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@609
    20
 *
jaroslav@609
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@609
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@609
    23
 * questions.
jaroslav@609
    24
 */
jaroslav@609
    25
jaroslav@609
    26
package java.util.zip;
jaroslav@609
    27
jaroslav@609
    28
/**
jaroslav@609
    29
 * This class provides support for general purpose decompression using the
jaroslav@609
    30
 * popular ZLIB compression library. The ZLIB compression library was
jaroslav@609
    31
 * initially developed as part of the PNG graphics standard and is not
jaroslav@609
    32
 * protected by patents. It is fully described in the specifications at
jaroslav@609
    33
 * the <a href="package-summary.html#package_description">java.util.zip
jaroslav@609
    34
 * package description</a>.
jaroslav@609
    35
 *
jaroslav@609
    36
 * <p>The following code fragment demonstrates a trivial compression
jaroslav@609
    37
 * and decompression of a string using <tt>Deflater</tt> and
jaroslav@609
    38
 * <tt>Inflater</tt>.
jaroslav@609
    39
 *
jaroslav@609
    40
 * <blockquote><pre>
jaroslav@609
    41
 * try {
jaroslav@609
    42
 *     // Encode a String into bytes
jaroslav@609
    43
 *     String inputString = "blahblahblah\u20AC\u20AC";
jaroslav@609
    44
 *     byte[] input = inputString.getBytes("UTF-8");
jaroslav@609
    45
 *
jaroslav@609
    46
 *     // Compress the bytes
jaroslav@609
    47
 *     byte[] output = new byte[100];
jaroslav@609
    48
 *     Deflater compresser = new Deflater();
jaroslav@609
    49
 *     compresser.setInput(input);
jaroslav@609
    50
 *     compresser.finish();
jaroslav@609
    51
 *     int compressedDataLength = compresser.deflate(output);
jaroslav@609
    52
 *
jaroslav@609
    53
 *     // Decompress the bytes
jaroslav@609
    54
 *     Inflater decompresser = new Inflater();
jaroslav@609
    55
 *     decompresser.setInput(output, 0, compressedDataLength);
jaroslav@609
    56
 *     byte[] result = new byte[100];
jaroslav@609
    57
 *     int resultLength = decompresser.inflate(result);
jaroslav@609
    58
 *     decompresser.end();
jaroslav@609
    59
 *
jaroslav@609
    60
 *     // Decode the bytes into a String
jaroslav@609
    61
 *     String outputString = new String(result, 0, resultLength, "UTF-8");
jaroslav@609
    62
 * } catch(java.io.UnsupportedEncodingException ex) {
jaroslav@609
    63
 *     // handle
jaroslav@609
    64
 * } catch (java.util.zip.DataFormatException ex) {
jaroslav@609
    65
 *     // handle
jaroslav@609
    66
 * }
jaroslav@609
    67
 * </pre></blockquote>
jaroslav@609
    68
 *
jaroslav@609
    69
 * @see         Deflater
jaroslav@609
    70
 * @author      David Connelly
jaroslav@609
    71
 *
jaroslav@609
    72
 */
jaroslav@609
    73
public
jaroslav@609
    74
class Inflater {
jaroslav@609
    75
jaroslav@609
    76
    private final ZStreamRef zsRef;
jaroslav@609
    77
    private byte[] buf = defaultBuf;
jaroslav@609
    78
    private int off, len;
jaroslav@609
    79
    private boolean finished;
jaroslav@609
    80
    private boolean needDict;
jaroslav@609
    81
jaroslav@609
    82
    private static final byte[] defaultBuf = new byte[0];
jaroslav@609
    83
jaroslav@609
    84
    static {
jaroslav@609
    85
        /* Zip library is loaded from System.initializeSystemClass */
jaroslav@609
    86
        initIDs();
jaroslav@609
    87
    }
jaroslav@609
    88
jaroslav@609
    89
    /**
jaroslav@609
    90
     * Creates a new decompressor. If the parameter 'nowrap' is true then
jaroslav@609
    91
     * the ZLIB header and checksum fields will not be used. This provides
jaroslav@609
    92
     * compatibility with the compression format used by both GZIP and PKZIP.
jaroslav@609
    93
     * <p>
jaroslav@609
    94
     * Note: When using the 'nowrap' option it is also necessary to provide
jaroslav@609
    95
     * an extra "dummy" byte as input. This is required by the ZLIB native
jaroslav@609
    96
     * library in order to support certain optimizations.
jaroslav@609
    97
     *
jaroslav@609
    98
     * @param nowrap if true then support GZIP compatible compression
jaroslav@609
    99
     */
jaroslav@609
   100
    public Inflater(boolean nowrap) {
jaroslav@609
   101
        zsRef = new ZStreamRef(init(nowrap));
jaroslav@609
   102
    }
jaroslav@609
   103
jaroslav@609
   104
    /**
jaroslav@609
   105
     * Creates a new decompressor.
jaroslav@609
   106
     */
jaroslav@609
   107
    public Inflater() {
jaroslav@609
   108
        this(false);
jaroslav@609
   109
    }
jaroslav@609
   110
jaroslav@609
   111
    /**
jaroslav@609
   112
     * Sets input data for decompression. Should be called whenever
jaroslav@609
   113
     * needsInput() returns true indicating that more input data is
jaroslav@609
   114
     * required.
jaroslav@609
   115
     * @param b the input data bytes
jaroslav@609
   116
     * @param off the start offset of the input data
jaroslav@609
   117
     * @param len the length of the input data
jaroslav@609
   118
     * @see Inflater#needsInput
jaroslav@609
   119
     */
jaroslav@609
   120
    public void setInput(byte[] b, int off, int len) {
jaroslav@609
   121
        if (b == null) {
jaroslav@609
   122
            throw new NullPointerException();
jaroslav@609
   123
        }
jaroslav@609
   124
        if (off < 0 || len < 0 || off > b.length - len) {
jaroslav@609
   125
            throw new ArrayIndexOutOfBoundsException();
jaroslav@609
   126
        }
jaroslav@609
   127
        synchronized (zsRef) {
jaroslav@609
   128
            this.buf = b;
jaroslav@609
   129
            this.off = off;
jaroslav@609
   130
            this.len = len;
jaroslav@609
   131
        }
jaroslav@609
   132
    }
jaroslav@609
   133
jaroslav@609
   134
    /**
jaroslav@609
   135
     * Sets input data for decompression. Should be called whenever
jaroslav@609
   136
     * needsInput() returns true indicating that more input data is
jaroslav@609
   137
     * required.
jaroslav@609
   138
     * @param b the input data bytes
jaroslav@609
   139
     * @see Inflater#needsInput
jaroslav@609
   140
     */
jaroslav@609
   141
    public void setInput(byte[] b) {
jaroslav@609
   142
        setInput(b, 0, b.length);
jaroslav@609
   143
    }
jaroslav@609
   144
jaroslav@609
   145
    /**
jaroslav@609
   146
     * Sets the preset dictionary to the given array of bytes. Should be
jaroslav@609
   147
     * called when inflate() returns 0 and needsDictionary() returns true
jaroslav@609
   148
     * indicating that a preset dictionary is required. The method getAdler()
jaroslav@609
   149
     * can be used to get the Adler-32 value of the dictionary needed.
jaroslav@609
   150
     * @param b the dictionary data bytes
jaroslav@609
   151
     * @param off the start offset of the data
jaroslav@609
   152
     * @param len the length of the data
jaroslav@609
   153
     * @see Inflater#needsDictionary
jaroslav@609
   154
     * @see Inflater#getAdler
jaroslav@609
   155
     */
jaroslav@609
   156
    public void setDictionary(byte[] b, int off, int len) {
jaroslav@609
   157
        if (b == null) {
jaroslav@609
   158
            throw new NullPointerException();
jaroslav@609
   159
        }
jaroslav@609
   160
        if (off < 0 || len < 0 || off > b.length - len) {
jaroslav@609
   161
            throw new ArrayIndexOutOfBoundsException();
jaroslav@609
   162
        }
jaroslav@609
   163
        synchronized (zsRef) {
jaroslav@609
   164
            ensureOpen();
jaroslav@609
   165
            setDictionary(zsRef.address(), b, off, len);
jaroslav@609
   166
            needDict = false;
jaroslav@609
   167
        }
jaroslav@609
   168
    }
jaroslav@609
   169
jaroslav@609
   170
    /**
jaroslav@609
   171
     * Sets the preset dictionary to the given array of bytes. Should be
jaroslav@609
   172
     * called when inflate() returns 0 and needsDictionary() returns true
jaroslav@609
   173
     * indicating that a preset dictionary is required. The method getAdler()
jaroslav@609
   174
     * can be used to get the Adler-32 value of the dictionary needed.
jaroslav@609
   175
     * @param b the dictionary data bytes
jaroslav@609
   176
     * @see Inflater#needsDictionary
jaroslav@609
   177
     * @see Inflater#getAdler
jaroslav@609
   178
     */
jaroslav@609
   179
    public void setDictionary(byte[] b) {
jaroslav@609
   180
        setDictionary(b, 0, b.length);
jaroslav@609
   181
    }
jaroslav@609
   182
jaroslav@609
   183
    /**
jaroslav@609
   184
     * Returns the total number of bytes remaining in the input buffer.
jaroslav@609
   185
     * This can be used to find out what bytes still remain in the input
jaroslav@609
   186
     * buffer after decompression has finished.
jaroslav@609
   187
     * @return the total number of bytes remaining in the input buffer
jaroslav@609
   188
     */
jaroslav@609
   189
    public int getRemaining() {
jaroslav@609
   190
        synchronized (zsRef) {
jaroslav@609
   191
            return len;
jaroslav@609
   192
        }
jaroslav@609
   193
    }
jaroslav@609
   194
jaroslav@609
   195
    /**
jaroslav@609
   196
     * Returns true if no data remains in the input buffer. This can
jaroslav@609
   197
     * be used to determine if #setInput should be called in order
jaroslav@609
   198
     * to provide more input.
jaroslav@609
   199
     * @return true if no data remains in the input buffer
jaroslav@609
   200
     */
jaroslav@609
   201
    public boolean needsInput() {
jaroslav@609
   202
        synchronized (zsRef) {
jaroslav@609
   203
            return len <= 0;
jaroslav@609
   204
        }
jaroslav@609
   205
    }
jaroslav@609
   206
jaroslav@609
   207
    /**
jaroslav@609
   208
     * Returns true if a preset dictionary is needed for decompression.
jaroslav@609
   209
     * @return true if a preset dictionary is needed for decompression
jaroslav@609
   210
     * @see Inflater#setDictionary
jaroslav@609
   211
     */
jaroslav@609
   212
    public boolean needsDictionary() {
jaroslav@609
   213
        synchronized (zsRef) {
jaroslav@609
   214
            return needDict;
jaroslav@609
   215
        }
jaroslav@609
   216
    }
jaroslav@609
   217
jaroslav@609
   218
    /**
jaroslav@609
   219
     * Returns true if the end of the compressed data stream has been
jaroslav@609
   220
     * reached.
jaroslav@609
   221
     * @return true if the end of the compressed data stream has been
jaroslav@609
   222
     * reached
jaroslav@609
   223
     */
jaroslav@609
   224
    public boolean finished() {
jaroslav@609
   225
        synchronized (zsRef) {
jaroslav@609
   226
            return finished;
jaroslav@609
   227
        }
jaroslav@609
   228
    }
jaroslav@609
   229
jaroslav@609
   230
    /**
jaroslav@609
   231
     * Uncompresses bytes into specified buffer. Returns actual number
jaroslav@609
   232
     * of bytes uncompressed. A return value of 0 indicates that
jaroslav@609
   233
     * needsInput() or needsDictionary() should be called in order to
jaroslav@609
   234
     * determine if more input data or a preset dictionary is required.
jaroslav@609
   235
     * In the latter case, getAdler() can be used to get the Adler-32
jaroslav@609
   236
     * value of the dictionary required.
jaroslav@609
   237
     * @param b the buffer for the uncompressed data
jaroslav@609
   238
     * @param off the start offset of the data
jaroslav@609
   239
     * @param len the maximum number of uncompressed bytes
jaroslav@609
   240
     * @return the actual number of uncompressed bytes
jaroslav@609
   241
     * @exception DataFormatException if the compressed data format is invalid
jaroslav@609
   242
     * @see Inflater#needsInput
jaroslav@609
   243
     * @see Inflater#needsDictionary
jaroslav@609
   244
     */
jaroslav@609
   245
    public int inflate(byte[] b, int off, int len)
jaroslav@609
   246
        throws DataFormatException
jaroslav@609
   247
    {
jaroslav@609
   248
        if (b == null) {
jaroslav@609
   249
            throw new NullPointerException();
jaroslav@609
   250
        }
jaroslav@609
   251
        if (off < 0 || len < 0 || off > b.length - len) {
jaroslav@609
   252
            throw new ArrayIndexOutOfBoundsException();
jaroslav@609
   253
        }
jaroslav@609
   254
        synchronized (zsRef) {
jaroslav@609
   255
            ensureOpen();
jaroslav@609
   256
            return inflateBytes(zsRef.address(), b, off, len);
jaroslav@609
   257
        }
jaroslav@609
   258
    }
jaroslav@609
   259
jaroslav@609
   260
    /**
jaroslav@609
   261
     * Uncompresses bytes into specified buffer. Returns actual number
jaroslav@609
   262
     * of bytes uncompressed. A return value of 0 indicates that
jaroslav@609
   263
     * needsInput() or needsDictionary() should be called in order to
jaroslav@609
   264
     * determine if more input data or a preset dictionary is required.
jaroslav@609
   265
     * In the latter case, getAdler() can be used to get the Adler-32
jaroslav@609
   266
     * value of the dictionary required.
jaroslav@609
   267
     * @param b the buffer for the uncompressed data
jaroslav@609
   268
     * @return the actual number of uncompressed bytes
jaroslav@609
   269
     * @exception DataFormatException if the compressed data format is invalid
jaroslav@609
   270
     * @see Inflater#needsInput
jaroslav@609
   271
     * @see Inflater#needsDictionary
jaroslav@609
   272
     */
jaroslav@609
   273
    public int inflate(byte[] b) throws DataFormatException {
jaroslav@609
   274
        return inflate(b, 0, b.length);
jaroslav@609
   275
    }
jaroslav@609
   276
jaroslav@609
   277
    /**
jaroslav@609
   278
     * Returns the ADLER-32 value of the uncompressed data.
jaroslav@609
   279
     * @return the ADLER-32 value of the uncompressed data
jaroslav@609
   280
     */
jaroslav@609
   281
    public int getAdler() {
jaroslav@609
   282
        synchronized (zsRef) {
jaroslav@609
   283
            ensureOpen();
jaroslav@609
   284
            return getAdler(zsRef.address());
jaroslav@609
   285
        }
jaroslav@609
   286
    }
jaroslav@609
   287
jaroslav@609
   288
    /**
jaroslav@609
   289
     * Returns the total number of compressed bytes input so far.
jaroslav@609
   290
     *
jaroslav@609
   291
     * <p>Since the number of bytes may be greater than
jaroslav@609
   292
     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
jaroslav@609
   293
     * the preferred means of obtaining this information.</p>
jaroslav@609
   294
     *
jaroslav@609
   295
     * @return the total number of compressed bytes input so far
jaroslav@609
   296
     */
jaroslav@609
   297
    public int getTotalIn() {
jaroslav@609
   298
        return (int) getBytesRead();
jaroslav@609
   299
    }
jaroslav@609
   300
jaroslav@609
   301
    /**
jaroslav@609
   302
     * Returns the total number of compressed bytes input so far.</p>
jaroslav@609
   303
     *
jaroslav@609
   304
     * @return the total (non-negative) number of compressed bytes input so far
jaroslav@609
   305
     * @since 1.5
jaroslav@609
   306
     */
jaroslav@609
   307
    public long getBytesRead() {
jaroslav@609
   308
        synchronized (zsRef) {
jaroslav@609
   309
            ensureOpen();
jaroslav@609
   310
            return getBytesRead(zsRef.address());
jaroslav@609
   311
        }
jaroslav@609
   312
    }
jaroslav@609
   313
jaroslav@609
   314
    /**
jaroslav@609
   315
     * Returns the total number of uncompressed bytes output so far.
jaroslav@609
   316
     *
jaroslav@609
   317
     * <p>Since the number of bytes may be greater than
jaroslav@609
   318
     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
jaroslav@609
   319
     * the preferred means of obtaining this information.</p>
jaroslav@609
   320
     *
jaroslav@609
   321
     * @return the total number of uncompressed bytes output so far
jaroslav@609
   322
     */
jaroslav@609
   323
    public int getTotalOut() {
jaroslav@609
   324
        return (int) getBytesWritten();
jaroslav@609
   325
    }
jaroslav@609
   326
jaroslav@609
   327
    /**
jaroslav@609
   328
     * Returns the total number of uncompressed bytes output so far.</p>
jaroslav@609
   329
     *
jaroslav@609
   330
     * @return the total (non-negative) number of uncompressed bytes output so far
jaroslav@609
   331
     * @since 1.5
jaroslav@609
   332
     */
jaroslav@609
   333
    public long getBytesWritten() {
jaroslav@609
   334
        synchronized (zsRef) {
jaroslav@609
   335
            ensureOpen();
jaroslav@609
   336
            return getBytesWritten(zsRef.address());
jaroslav@609
   337
        }
jaroslav@609
   338
    }
jaroslav@609
   339
jaroslav@609
   340
    /**
jaroslav@609
   341
     * Resets inflater so that a new set of input data can be processed.
jaroslav@609
   342
     */
jaroslav@609
   343
    public void reset() {
jaroslav@609
   344
        synchronized (zsRef) {
jaroslav@609
   345
            ensureOpen();
jaroslav@609
   346
            reset(zsRef.address());
jaroslav@609
   347
            buf = defaultBuf;
jaroslav@609
   348
            finished = false;
jaroslav@609
   349
            needDict = false;
jaroslav@609
   350
            off = len = 0;
jaroslav@609
   351
        }
jaroslav@609
   352
    }
jaroslav@609
   353
jaroslav@609
   354
    /**
jaroslav@609
   355
     * Closes the decompressor and discards any unprocessed input.
jaroslav@609
   356
     * This method should be called when the decompressor is no longer
jaroslav@609
   357
     * being used, but will also be called automatically by the finalize()
jaroslav@609
   358
     * method. Once this method is called, the behavior of the Inflater
jaroslav@609
   359
     * object is undefined.
jaroslav@609
   360
     */
jaroslav@609
   361
    public void end() {
jaroslav@609
   362
        synchronized (zsRef) {
jaroslav@609
   363
            long addr = zsRef.address();
jaroslav@609
   364
            zsRef.clear();
jaroslav@609
   365
            if (addr != 0) {
jaroslav@609
   366
                end(addr);
jaroslav@609
   367
                buf = null;
jaroslav@609
   368
            }
jaroslav@609
   369
        }
jaroslav@609
   370
    }
jaroslav@609
   371
jaroslav@609
   372
    /**
jaroslav@609
   373
     * Closes the decompressor when garbage is collected.
jaroslav@609
   374
     */
jaroslav@609
   375
    protected void finalize() {
jaroslav@609
   376
        end();
jaroslav@609
   377
    }
jaroslav@609
   378
jaroslav@609
   379
    private void ensureOpen () {
jaroslav@609
   380
        assert Thread.holdsLock(zsRef);
jaroslav@609
   381
        if (zsRef.address() == 0)
jaroslav@609
   382
            throw new NullPointerException("Inflater has been closed");
jaroslav@609
   383
    }
jaroslav@609
   384
jaroslav@609
   385
    boolean ended() {
jaroslav@609
   386
        synchronized (zsRef) {
jaroslav@609
   387
            return zsRef.address() == 0;
jaroslav@609
   388
        }
jaroslav@609
   389
    }
jaroslav@609
   390
jaroslav@609
   391
    private native static void initIDs();
jaroslav@609
   392
    private native static long init(boolean nowrap);
jaroslav@609
   393
    private native static void setDictionary(long addr, byte[] b, int off,
jaroslav@609
   394
                                             int len);
jaroslav@609
   395
    private native int inflateBytes(long addr, byte[] b, int off, int len)
jaroslav@609
   396
            throws DataFormatException;
jaroslav@609
   397
    private native static int getAdler(long addr);
jaroslav@609
   398
    private native static long getBytesRead(long addr);
jaroslav@609
   399
    private native static long getBytesWritten(long addr);
jaroslav@609
   400
    private native static void reset(long addr);
jaroslav@609
   401
    private native static void end(long addr);
jaroslav@609
   402
}