1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/Inflater.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,338 @@
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 org.apidesign.bck2brwsr.emul.zip;
1.30 +
1.31 +import java.util.zip.*;
1.32 +import java.io.IOException;
1.33 +
1.34 +/**
1.35 + * This class provides support for general purpose decompression using the
1.36 + * popular ZLIB compression library. The ZLIB compression library was
1.37 + * initially developed as part of the PNG graphics standard and is not
1.38 + * protected by patents. It is fully described in the specifications at
1.39 + * the <a href="package-summary.html#package_description">java.util.zip
1.40 + * package description</a>.
1.41 + *
1.42 + * <p>The following code fragment demonstrates a trivial compression
1.43 + * and decompression of a string using <tt>Deflater</tt> and
1.44 + * <tt>Inflater</tt>.
1.45 + *
1.46 + * <blockquote><pre>
1.47 + * try {
1.48 + * // Encode a String into bytes
1.49 + * String inputString = "blahblahblah\u20AC\u20AC";
1.50 + * byte[] input = inputString.getBytes("UTF-8");
1.51 + *
1.52 + * // Compress the bytes
1.53 + * byte[] output = new byte[100];
1.54 + * Deflater compresser = new Deflater();
1.55 + * compresser.setInput(input);
1.56 + * compresser.finish();
1.57 + * int compressedDataLength = compresser.deflate(output);
1.58 + *
1.59 + * // Decompress the bytes
1.60 + * Inflater decompresser = new Inflater();
1.61 + * decompresser.setInput(output, 0, compressedDataLength);
1.62 + * byte[] result = new byte[100];
1.63 + * int resultLength = decompresser.inflate(result);
1.64 + * decompresser.end();
1.65 + *
1.66 + * // Decode the bytes into a String
1.67 + * String outputString = new String(result, 0, resultLength, "UTF-8");
1.68 + * } catch(java.io.UnsupportedEncodingException ex) {
1.69 + * // handle
1.70 + * } catch (java.util.zip.DataFormatException ex) {
1.71 + * // handle
1.72 + * }
1.73 + * </pre></blockquote>
1.74 + *
1.75 + * @see Deflater
1.76 + * @author David Connelly
1.77 + *
1.78 + */
1.79 +public
1.80 +class Inflater extends java.util.zip.Inflater {
1.81 + private final boolean nowrap;
1.82 + private JzLibInflater impl;
1.83 +
1.84 + /**
1.85 + * Creates a new decompressor. If the parameter 'nowrap' is true then
1.86 + * the ZLIB header and checksum fields will not be used. This provides
1.87 + * compatibility with the compression format used by both GZIP and PKZIP.
1.88 + * <p>
1.89 + * Note: When using the 'nowrap' option it is also necessary to provide
1.90 + * an extra "dummy" byte as input. This is required by the ZLIB native
1.91 + * library in order to support certain optimizations.
1.92 + *
1.93 + * @param nowrap if true then support GZIP compatible compression
1.94 + */
1.95 + public Inflater(boolean nowrap) {
1.96 + this.nowrap = nowrap;
1.97 + reset();
1.98 + }
1.99 +
1.100 + /**
1.101 + * Creates a new decompressor.
1.102 + */
1.103 + public Inflater() {
1.104 + this(false);
1.105 + }
1.106 +
1.107 + /**
1.108 + * Sets input data for decompression. Should be called whenever
1.109 + * needsInput() returns true indicating that more input data is
1.110 + * required.
1.111 + * @param b the input data bytes
1.112 + * @param off the start offset of the input data
1.113 + * @param len the length of the input data
1.114 + * @see Inflater#needsInput
1.115 + */
1.116 + public void setInput(byte[] b, int off, int len) {
1.117 + if (b == null) {
1.118 + throw new NullPointerException();
1.119 + }
1.120 + if (off < 0 || len < 0 || off > b.length - len) {
1.121 + throw new ArrayIndexOutOfBoundsException();
1.122 + }
1.123 + impl.setInput(b, off, len, false);
1.124 + }
1.125 +
1.126 + /**
1.127 + * Sets input data for decompression. Should be called whenever
1.128 + * needsInput() returns true indicating that more input data is
1.129 + * required.
1.130 + * @param b the input data bytes
1.131 + * @see Inflater#needsInput
1.132 + */
1.133 + public void setInput(byte[] b) {
1.134 + setInput(b, 0, b.length);
1.135 + }
1.136 +
1.137 + /**
1.138 + * Sets the preset dictionary to the given array of bytes. Should be
1.139 + * called when inflate() returns 0 and needsDictionary() returns true
1.140 + * indicating that a preset dictionary is required. The method getAdler()
1.141 + * can be used to get the Adler-32 value of the dictionary needed.
1.142 + * @param b the dictionary data bytes
1.143 + * @param off the start offset of the data
1.144 + * @param len the length of the data
1.145 + * @see Inflater#needsDictionary
1.146 + * @see Inflater#getAdler
1.147 + */
1.148 + public void setDictionary(byte[] b, int off, int len) {
1.149 + if (b == null) {
1.150 + throw new NullPointerException();
1.151 + }
1.152 + if (off < 0 || len < 0 || off > b.length - len) {
1.153 + throw new ArrayIndexOutOfBoundsException();
1.154 + }
1.155 + byte[] arr;
1.156 + if (off == 0) {
1.157 + arr = b;
1.158 + } else {
1.159 + arr = new byte[len];
1.160 + org.apidesign.bck2brwsr.emul.lang.System.arraycopy(b, off, arr, 0, len);
1.161 + }
1.162 + impl.setDictionary(arr, len);
1.163 + }
1.164 +
1.165 + /**
1.166 + * Sets the preset dictionary to the given array of bytes. Should be
1.167 + * called when inflate() returns 0 and needsDictionary() returns true
1.168 + * indicating that a preset dictionary is required. The method getAdler()
1.169 + * can be used to get the Adler-32 value of the dictionary needed.
1.170 + * @param b the dictionary data bytes
1.171 + * @see Inflater#needsDictionary
1.172 + * @see Inflater#getAdler
1.173 + */
1.174 + public void setDictionary(byte[] b) {
1.175 + impl.setDictionary(b, b.length);
1.176 + }
1.177 +
1.178 + /**
1.179 + * Returns the total number of bytes remaining in the input buffer.
1.180 + * This can be used to find out what bytes still remain in the input
1.181 + * buffer after decompression has finished.
1.182 + * @return the total number of bytes remaining in the input buffer
1.183 + */
1.184 + public int getRemaining() {
1.185 + return impl.getAvailIn();
1.186 + }
1.187 +
1.188 + /**
1.189 + * Returns true if no data remains in the input buffer. This can
1.190 + * be used to determine if #setInput should be called in order
1.191 + * to provide more input.
1.192 + * @return true if no data remains in the input buffer
1.193 + */
1.194 + public boolean needsInput() {
1.195 + return getRemaining() <= 0;
1.196 + }
1.197 +
1.198 + /**
1.199 + * Returns true if a preset dictionary is needed for decompression.
1.200 + * @return true if a preset dictionary is needed for decompression
1.201 + * @see Inflater#setDictionary
1.202 + */
1.203 + public boolean needsDictionary() {
1.204 + return impl.needDict();
1.205 + }
1.206 +
1.207 + /**
1.208 + * Returns true if the end of the compressed data stream has been
1.209 + * reached.
1.210 + * @return true if the end of the compressed data stream has been
1.211 + * reached
1.212 + */
1.213 + public boolean finished() {
1.214 + return impl.finished();
1.215 + }
1.216 +
1.217 + /**
1.218 + * Uncompresses bytes into specified buffer. Returns actual number
1.219 + * of bytes uncompressed. A return value of 0 indicates that
1.220 + * needsInput() or needsDictionary() should be called in order to
1.221 + * determine if more input data or a preset dictionary is required.
1.222 + * In the latter case, getAdler() can be used to get the Adler-32
1.223 + * value of the dictionary required.
1.224 + * @param b the buffer for the uncompressed data
1.225 + * @param off the start offset of the data
1.226 + * @param len the maximum number of uncompressed bytes
1.227 + * @return the actual number of uncompressed bytes
1.228 + * @exception DataFormatException if the compressed data format is invalid
1.229 + * @see Inflater#needsInput
1.230 + * @see Inflater#needsDictionary
1.231 + */
1.232 + public int inflate(byte[] b, int off, int len)
1.233 + throws DataFormatException
1.234 + {
1.235 + if (b == null) {
1.236 + throw new NullPointerException();
1.237 + }
1.238 + if (off < 0 || len < 0 || off > b.length - len) {
1.239 + throw new ArrayIndexOutOfBoundsException();
1.240 + }
1.241 + impl.setOutput(b, off, len);
1.242 + int err = impl.inflate(JzLibInflater.Z_NO_FLUSH);
1.243 + return impl.next_out_index - off;
1.244 + }
1.245 +
1.246 + /**
1.247 + * Uncompresses bytes into specified buffer. Returns actual number
1.248 + * of bytes uncompressed. A return value of 0 indicates that
1.249 + * needsInput() or needsDictionary() should be called in order to
1.250 + * determine if more input data or a preset dictionary is required.
1.251 + * In the latter case, getAdler() can be used to get the Adler-32
1.252 + * value of the dictionary required.
1.253 + * @param b the buffer for the uncompressed data
1.254 + * @return the actual number of uncompressed bytes
1.255 + * @exception DataFormatException if the compressed data format is invalid
1.256 + * @see Inflater#needsInput
1.257 + * @see Inflater#needsDictionary
1.258 + */
1.259 + public int inflate(byte[] b) throws DataFormatException {
1.260 + return inflate(b, 0, b.length);
1.261 + }
1.262 +
1.263 + /**
1.264 + * Returns the ADLER-32 value of the uncompressed data.
1.265 + * @return the ADLER-32 value of the uncompressed data
1.266 + */
1.267 + public int getAdler() {
1.268 + return (int) impl.getAdler();
1.269 + }
1.270 +
1.271 + /**
1.272 + * Returns the total number of compressed bytes input so far.
1.273 + *
1.274 + * <p>Since the number of bytes may be greater than
1.275 + * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
1.276 + * the preferred means of obtaining this information.</p>
1.277 + *
1.278 + * @return the total number of compressed bytes input so far
1.279 + */
1.280 + public int getTotalIn() {
1.281 + return (int) getBytesRead();
1.282 + }
1.283 +
1.284 + /**
1.285 + * Returns the total number of compressed bytes input so far.</p>
1.286 + *
1.287 + * @return the total (non-negative) number of compressed bytes input so far
1.288 + * @since 1.5
1.289 + */
1.290 + public long getBytesRead() {
1.291 + return impl.total_in;
1.292 + }
1.293 +
1.294 + /**
1.295 + * Returns the total number of uncompressed bytes output so far.
1.296 + *
1.297 + * <p>Since the number of bytes may be greater than
1.298 + * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
1.299 + * the preferred means of obtaining this information.</p>
1.300 + *
1.301 + * @return the total number of uncompressed bytes output so far
1.302 + */
1.303 + public int getTotalOut() {
1.304 + return (int) getBytesWritten();
1.305 + }
1.306 +
1.307 + /**
1.308 + * Returns the total number of uncompressed bytes output so far.</p>
1.309 + *
1.310 + * @return the total (non-negative) number of uncompressed bytes output so far
1.311 + * @since 1.5
1.312 + */
1.313 + public long getBytesWritten() {
1.314 + return impl.total_out;
1.315 + }
1.316 +
1.317 + /**
1.318 + * Resets inflater so that a new set of input data can be processed.
1.319 + */
1.320 + public void reset() {
1.321 + impl = new JzLibInflater(15, nowrap);
1.322 + }
1.323 +
1.324 + /**
1.325 + * Closes the decompressor and discards any unprocessed input.
1.326 + * This method should be called when the decompressor is no longer
1.327 + * being used, but will also be called automatically by the finalize()
1.328 + * method. Once this method is called, the behavior of the Inflater
1.329 + * object is undefined.
1.330 + */
1.331 + public void end() {
1.332 + impl.end();
1.333 + }
1.334 +
1.335 + /**
1.336 + * Closes the decompressor when garbage is collected.
1.337 + */
1.338 + protected void finalize() {
1.339 + end();
1.340 + }
1.341 +}