rt/emul/mini/src/main/java/java/util/zip/Inflater.java
changeset 772 d382dacfd73f
parent 694 0d277415ed02
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/java/util/zip/Inflater.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,310 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util.zip;
    1.30 +
    1.31 +/**
    1.32 + * This class provides support for general purpose decompression using the
    1.33 + * popular ZLIB compression library. The ZLIB compression library was
    1.34 + * initially developed as part of the PNG graphics standard and is not
    1.35 + * protected by patents. It is fully described in the specifications at
    1.36 + * the <a href="package-summary.html#package_description">java.util.zip
    1.37 + * package description</a>.
    1.38 + *
    1.39 + * <p>The following code fragment demonstrates a trivial compression
    1.40 + * and decompression of a string using <tt>Deflater</tt> and
    1.41 + * <tt>Inflater</tt>.
    1.42 + *
    1.43 + * <blockquote><pre>
    1.44 + * try {
    1.45 + *     // Encode a String into bytes
    1.46 + *     String inputString = "blahblahblah\u20AC\u20AC";
    1.47 + *     byte[] input = inputString.getBytes("UTF-8");
    1.48 + *
    1.49 + *     // Compress the bytes
    1.50 + *     byte[] output = new byte[100];
    1.51 + *     Deflater compresser = new Deflater();
    1.52 + *     compresser.setInput(input);
    1.53 + *     compresser.finish();
    1.54 + *     int compressedDataLength = compresser.deflate(output);
    1.55 + *
    1.56 + *     // Decompress the bytes
    1.57 + *     Inflater decompresser = new Inflater();
    1.58 + *     decompresser.setInput(output, 0, compressedDataLength);
    1.59 + *     byte[] result = new byte[100];
    1.60 + *     int resultLength = decompresser.inflate(result);
    1.61 + *     decompresser.end();
    1.62 + *
    1.63 + *     // Decode the bytes into a String
    1.64 + *     String outputString = new String(result, 0, resultLength, "UTF-8");
    1.65 + * } catch(java.io.UnsupportedEncodingException ex) {
    1.66 + *     // handle
    1.67 + * } catch (java.util.zip.DataFormatException ex) {
    1.68 + *     // handle
    1.69 + * }
    1.70 + * </pre></blockquote>
    1.71 + *
    1.72 + * @see         Deflater
    1.73 + * @author      David Connelly
    1.74 + *
    1.75 + */
    1.76 +public
    1.77 +class Inflater {
    1.78 +    private final org.apidesign.bck2brwsr.emul.zip.Inflater impl;
    1.79 +    
    1.80 +    /**
    1.81 +     * Creates a new decompressor. If the parameter 'nowrap' is true then
    1.82 +     * the ZLIB header and checksum fields will not be used. This provides
    1.83 +     * compatibility with the compression format used by both GZIP and PKZIP.
    1.84 +     * <p>
    1.85 +     * Note: When using the 'nowrap' option it is also necessary to provide
    1.86 +     * an extra "dummy" byte as input. This is required by the ZLIB native
    1.87 +     * library in order to support certain optimizations.
    1.88 +     *
    1.89 +     * @param nowrap if true then support GZIP compatible compression
    1.90 +     */
    1.91 +    public Inflater(boolean nowrap) {
    1.92 +        if (getClass() == org.apidesign.bck2brwsr.emul.zip.Inflater.class) {
    1.93 +            impl = null;
    1.94 +        } else {
    1.95 +            impl = new org.apidesign.bck2brwsr.emul.zip.Inflater(nowrap);
    1.96 +        }
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Creates a new decompressor.
   1.101 +     */
   1.102 +    public Inflater() {
   1.103 +        this(false);
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Sets input data for decompression. Should be called whenever
   1.108 +     * needsInput() returns true indicating that more input data is
   1.109 +     * required.
   1.110 +     * @param b the input data bytes
   1.111 +     * @param off the start offset of the input data
   1.112 +     * @param len the length of the input data
   1.113 +     * @see Inflater#needsInput
   1.114 +     */
   1.115 +    public void setInput(byte[] b, int off, int len) {
   1.116 +        impl.setInput(b, off, len);
   1.117 +    }
   1.118 +
   1.119 +    /**
   1.120 +     * Sets input data for decompression. Should be called whenever
   1.121 +     * needsInput() returns true indicating that more input data is
   1.122 +     * required.
   1.123 +     * @param b the input data bytes
   1.124 +     * @see Inflater#needsInput
   1.125 +     */
   1.126 +    public void setInput(byte[] b) {
   1.127 +        impl.setInput(b);
   1.128 +    }
   1.129 +
   1.130 +    /**
   1.131 +     * Sets the preset dictionary to the given array of bytes. Should be
   1.132 +     * called when inflate() returns 0 and needsDictionary() returns true
   1.133 +     * indicating that a preset dictionary is required. The method getAdler()
   1.134 +     * can be used to get the Adler-32 value of the dictionary needed.
   1.135 +     * @param b the dictionary data bytes
   1.136 +     * @param off the start offset of the data
   1.137 +     * @param len the length of the data
   1.138 +     * @see Inflater#needsDictionary
   1.139 +     * @see Inflater#getAdler
   1.140 +     */
   1.141 +    public void setDictionary(byte[] b, int off, int len) {
   1.142 +        impl.setDictionary(b, off, len);
   1.143 +    }
   1.144 +
   1.145 +    /**
   1.146 +     * Sets the preset dictionary to the given array of bytes. Should be
   1.147 +     * called when inflate() returns 0 and needsDictionary() returns true
   1.148 +     * indicating that a preset dictionary is required. The method getAdler()
   1.149 +     * can be used to get the Adler-32 value of the dictionary needed.
   1.150 +     * @param b the dictionary data bytes
   1.151 +     * @see Inflater#needsDictionary
   1.152 +     * @see Inflater#getAdler
   1.153 +     */
   1.154 +    public void setDictionary(byte[] b) {
   1.155 +        impl.setDictionary(b);
   1.156 +    }
   1.157 +
   1.158 +    /**
   1.159 +     * Returns the total number of bytes remaining in the input buffer.
   1.160 +     * This can be used to find out what bytes still remain in the input
   1.161 +     * buffer after decompression has finished.
   1.162 +     * @return the total number of bytes remaining in the input buffer
   1.163 +     */
   1.164 +    public int getRemaining() {
   1.165 +        return impl.getRemaining();
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * Returns true if no data remains in the input buffer. This can
   1.170 +     * be used to determine if #setInput should be called in order
   1.171 +     * to provide more input.
   1.172 +     * @return true if no data remains in the input buffer
   1.173 +     */
   1.174 +    public boolean needsInput() {
   1.175 +        return impl.needsInput();
   1.176 +    }
   1.177 +
   1.178 +    /**
   1.179 +     * Returns true if a preset dictionary is needed for decompression.
   1.180 +     * @return true if a preset dictionary is needed for decompression
   1.181 +     * @see Inflater#setDictionary
   1.182 +     */
   1.183 +    public boolean needsDictionary() {
   1.184 +        return impl.needsDictionary();
   1.185 +    }
   1.186 +
   1.187 +    /**
   1.188 +     * Returns true if the end of the compressed data stream has been
   1.189 +     * reached.
   1.190 +     * @return true if the end of the compressed data stream has been
   1.191 +     * reached
   1.192 +     */
   1.193 +    public boolean finished() {
   1.194 +        return impl.finished();
   1.195 +    }
   1.196 +
   1.197 +    /**
   1.198 +     * Uncompresses bytes into specified buffer. Returns actual number
   1.199 +     * of bytes uncompressed. A return value of 0 indicates that
   1.200 +     * needsInput() or needsDictionary() should be called in order to
   1.201 +     * determine if more input data or a preset dictionary is required.
   1.202 +     * In the latter case, getAdler() can be used to get the Adler-32
   1.203 +     * value of the dictionary required.
   1.204 +     * @param b the buffer for the uncompressed data
   1.205 +     * @param off the start offset of the data
   1.206 +     * @param len the maximum number of uncompressed bytes
   1.207 +     * @return the actual number of uncompressed bytes
   1.208 +     * @exception DataFormatException if the compressed data format is invalid
   1.209 +     * @see Inflater#needsInput
   1.210 +     * @see Inflater#needsDictionary
   1.211 +     */
   1.212 +    public int inflate(byte[] b, int off, int len)
   1.213 +        throws DataFormatException
   1.214 +    {
   1.215 +        return impl.inflate(b, off, len);
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Uncompresses bytes into specified buffer. Returns actual number
   1.220 +     * of bytes uncompressed. A return value of 0 indicates that
   1.221 +     * needsInput() or needsDictionary() should be called in order to
   1.222 +     * determine if more input data or a preset dictionary is required.
   1.223 +     * In the latter case, getAdler() can be used to get the Adler-32
   1.224 +     * value of the dictionary required.
   1.225 +     * @param b the buffer for the uncompressed data
   1.226 +     * @return the actual number of uncompressed bytes
   1.227 +     * @exception DataFormatException if the compressed data format is invalid
   1.228 +     * @see Inflater#needsInput
   1.229 +     * @see Inflater#needsDictionary
   1.230 +     */
   1.231 +    public int inflate(byte[] b) throws DataFormatException {
   1.232 +        return impl.inflate(b);
   1.233 +    }
   1.234 +
   1.235 +    /**
   1.236 +     * Returns the ADLER-32 value of the uncompressed data.
   1.237 +     * @return the ADLER-32 value of the uncompressed data
   1.238 +     */
   1.239 +    public int getAdler() {
   1.240 +        return impl.getAdler();
   1.241 +    }
   1.242 +
   1.243 +    /**
   1.244 +     * Returns the total number of compressed bytes input so far.
   1.245 +     *
   1.246 +     * <p>Since the number of bytes may be greater than
   1.247 +     * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
   1.248 +     * the preferred means of obtaining this information.</p>
   1.249 +     *
   1.250 +     * @return the total number of compressed bytes input so far
   1.251 +     */
   1.252 +    public int getTotalIn() {
   1.253 +        return impl.getTotalIn();
   1.254 +    }
   1.255 +
   1.256 +    /**
   1.257 +     * Returns the total number of compressed bytes input so far.</p>
   1.258 +     *
   1.259 +     * @return the total (non-negative) number of compressed bytes input so far
   1.260 +     * @since 1.5
   1.261 +     */
   1.262 +    public long getBytesRead() {
   1.263 +        return impl.getBytesRead();
   1.264 +    }
   1.265 +
   1.266 +    /**
   1.267 +     * Returns the total number of uncompressed bytes output so far.
   1.268 +     *
   1.269 +     * <p>Since the number of bytes may be greater than
   1.270 +     * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
   1.271 +     * the preferred means of obtaining this information.</p>
   1.272 +     *
   1.273 +     * @return the total number of uncompressed bytes output so far
   1.274 +     */
   1.275 +    public int getTotalOut() {
   1.276 +        return impl.getTotalOut();
   1.277 +    }
   1.278 +
   1.279 +    /**
   1.280 +     * Returns the total number of uncompressed bytes output so far.</p>
   1.281 +     *
   1.282 +     * @return the total (non-negative) number of uncompressed bytes output so far
   1.283 +     * @since 1.5
   1.284 +     */
   1.285 +    public long getBytesWritten() {
   1.286 +        return impl.getBytesWritten();
   1.287 +    }
   1.288 +
   1.289 +    /**
   1.290 +     * Resets inflater so that a new set of input data can be processed.
   1.291 +     */
   1.292 +    public void reset() {
   1.293 +        impl.reset();
   1.294 +    }
   1.295 +
   1.296 +    /**
   1.297 +     * Closes the decompressor and discards any unprocessed input.
   1.298 +     * This method should be called when the decompressor is no longer
   1.299 +     * being used, but will also be called automatically by the finalize()
   1.300 +     * method. Once this method is called, the behavior of the Inflater
   1.301 +     * object is undefined.
   1.302 +     */
   1.303 +    public void end() {
   1.304 +        impl.end();
   1.305 +    }
   1.306 +
   1.307 +    /**
   1.308 +     * Closes the decompressor when garbage is collected.
   1.309 +     */
   1.310 +    protected void finalize() {
   1.311 +        end();
   1.312 +    }
   1.313 +}