1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/util/zip/Inflater.java Wed Jan 30 14:00:17 2013 +0100
1.3 @@ -0,0 +1,402 @@
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 +
1.79 + private final ZStreamRef zsRef;
1.80 + private byte[] buf = defaultBuf;
1.81 + private int off, len;
1.82 + private boolean finished;
1.83 + private boolean needDict;
1.84 +
1.85 + private static final byte[] defaultBuf = new byte[0];
1.86 +
1.87 + static {
1.88 + /* Zip library is loaded from System.initializeSystemClass */
1.89 + initIDs();
1.90 + }
1.91 +
1.92 + /**
1.93 + * Creates a new decompressor. If the parameter 'nowrap' is true then
1.94 + * the ZLIB header and checksum fields will not be used. This provides
1.95 + * compatibility with the compression format used by both GZIP and PKZIP.
1.96 + * <p>
1.97 + * Note: When using the 'nowrap' option it is also necessary to provide
1.98 + * an extra "dummy" byte as input. This is required by the ZLIB native
1.99 + * library in order to support certain optimizations.
1.100 + *
1.101 + * @param nowrap if true then support GZIP compatible compression
1.102 + */
1.103 + public Inflater(boolean nowrap) {
1.104 + zsRef = new ZStreamRef(init(nowrap));
1.105 + }
1.106 +
1.107 + /**
1.108 + * Creates a new decompressor.
1.109 + */
1.110 + public Inflater() {
1.111 + this(false);
1.112 + }
1.113 +
1.114 + /**
1.115 + * Sets input data for decompression. Should be called whenever
1.116 + * needsInput() returns true indicating that more input data is
1.117 + * required.
1.118 + * @param b the input data bytes
1.119 + * @param off the start offset of the input data
1.120 + * @param len the length of the input data
1.121 + * @see Inflater#needsInput
1.122 + */
1.123 + public void setInput(byte[] b, int off, int len) {
1.124 + if (b == null) {
1.125 + throw new NullPointerException();
1.126 + }
1.127 + if (off < 0 || len < 0 || off > b.length - len) {
1.128 + throw new ArrayIndexOutOfBoundsException();
1.129 + }
1.130 + synchronized (zsRef) {
1.131 + this.buf = b;
1.132 + this.off = off;
1.133 + this.len = len;
1.134 + }
1.135 + }
1.136 +
1.137 + /**
1.138 + * Sets input data for decompression. Should be called whenever
1.139 + * needsInput() returns true indicating that more input data is
1.140 + * required.
1.141 + * @param b the input data bytes
1.142 + * @see Inflater#needsInput
1.143 + */
1.144 + public void setInput(byte[] b) {
1.145 + setInput(b, 0, b.length);
1.146 + }
1.147 +
1.148 + /**
1.149 + * Sets the preset dictionary to the given array of bytes. Should be
1.150 + * called when inflate() returns 0 and needsDictionary() returns true
1.151 + * indicating that a preset dictionary is required. The method getAdler()
1.152 + * can be used to get the Adler-32 value of the dictionary needed.
1.153 + * @param b the dictionary data bytes
1.154 + * @param off the start offset of the data
1.155 + * @param len the length of the data
1.156 + * @see Inflater#needsDictionary
1.157 + * @see Inflater#getAdler
1.158 + */
1.159 + public void setDictionary(byte[] b, int off, int len) {
1.160 + if (b == null) {
1.161 + throw new NullPointerException();
1.162 + }
1.163 + if (off < 0 || len < 0 || off > b.length - len) {
1.164 + throw new ArrayIndexOutOfBoundsException();
1.165 + }
1.166 + synchronized (zsRef) {
1.167 + ensureOpen();
1.168 + setDictionary(zsRef.address(), b, off, len);
1.169 + needDict = false;
1.170 + }
1.171 + }
1.172 +
1.173 + /**
1.174 + * Sets the preset dictionary to the given array of bytes. Should be
1.175 + * called when inflate() returns 0 and needsDictionary() returns true
1.176 + * indicating that a preset dictionary is required. The method getAdler()
1.177 + * can be used to get the Adler-32 value of the dictionary needed.
1.178 + * @param b the dictionary data bytes
1.179 + * @see Inflater#needsDictionary
1.180 + * @see Inflater#getAdler
1.181 + */
1.182 + public void setDictionary(byte[] b) {
1.183 + setDictionary(b, 0, b.length);
1.184 + }
1.185 +
1.186 + /**
1.187 + * Returns the total number of bytes remaining in the input buffer.
1.188 + * This can be used to find out what bytes still remain in the input
1.189 + * buffer after decompression has finished.
1.190 + * @return the total number of bytes remaining in the input buffer
1.191 + */
1.192 + public int getRemaining() {
1.193 + synchronized (zsRef) {
1.194 + return len;
1.195 + }
1.196 + }
1.197 +
1.198 + /**
1.199 + * Returns true if no data remains in the input buffer. This can
1.200 + * be used to determine if #setInput should be called in order
1.201 + * to provide more input.
1.202 + * @return true if no data remains in the input buffer
1.203 + */
1.204 + public boolean needsInput() {
1.205 + synchronized (zsRef) {
1.206 + return len <= 0;
1.207 + }
1.208 + }
1.209 +
1.210 + /**
1.211 + * Returns true if a preset dictionary is needed for decompression.
1.212 + * @return true if a preset dictionary is needed for decompression
1.213 + * @see Inflater#setDictionary
1.214 + */
1.215 + public boolean needsDictionary() {
1.216 + synchronized (zsRef) {
1.217 + return needDict;
1.218 + }
1.219 + }
1.220 +
1.221 + /**
1.222 + * Returns true if the end of the compressed data stream has been
1.223 + * reached.
1.224 + * @return true if the end of the compressed data stream has been
1.225 + * reached
1.226 + */
1.227 + public boolean finished() {
1.228 + synchronized (zsRef) {
1.229 + return finished;
1.230 + }
1.231 + }
1.232 +
1.233 + /**
1.234 + * Uncompresses bytes into specified buffer. Returns actual number
1.235 + * of bytes uncompressed. A return value of 0 indicates that
1.236 + * needsInput() or needsDictionary() should be called in order to
1.237 + * determine if more input data or a preset dictionary is required.
1.238 + * In the latter case, getAdler() can be used to get the Adler-32
1.239 + * value of the dictionary required.
1.240 + * @param b the buffer for the uncompressed data
1.241 + * @param off the start offset of the data
1.242 + * @param len the maximum number of uncompressed bytes
1.243 + * @return the actual number of uncompressed bytes
1.244 + * @exception DataFormatException if the compressed data format is invalid
1.245 + * @see Inflater#needsInput
1.246 + * @see Inflater#needsDictionary
1.247 + */
1.248 + public int inflate(byte[] b, int off, int len)
1.249 + throws DataFormatException
1.250 + {
1.251 + if (b == null) {
1.252 + throw new NullPointerException();
1.253 + }
1.254 + if (off < 0 || len < 0 || off > b.length - len) {
1.255 + throw new ArrayIndexOutOfBoundsException();
1.256 + }
1.257 + synchronized (zsRef) {
1.258 + ensureOpen();
1.259 + return inflateBytes(zsRef.address(), b, off, len);
1.260 + }
1.261 + }
1.262 +
1.263 + /**
1.264 + * Uncompresses bytes into specified buffer. Returns actual number
1.265 + * of bytes uncompressed. A return value of 0 indicates that
1.266 + * needsInput() or needsDictionary() should be called in order to
1.267 + * determine if more input data or a preset dictionary is required.
1.268 + * In the latter case, getAdler() can be used to get the Adler-32
1.269 + * value of the dictionary required.
1.270 + * @param b the buffer for the uncompressed data
1.271 + * @return the actual number of uncompressed bytes
1.272 + * @exception DataFormatException if the compressed data format is invalid
1.273 + * @see Inflater#needsInput
1.274 + * @see Inflater#needsDictionary
1.275 + */
1.276 + public int inflate(byte[] b) throws DataFormatException {
1.277 + return inflate(b, 0, b.length);
1.278 + }
1.279 +
1.280 + /**
1.281 + * Returns the ADLER-32 value of the uncompressed data.
1.282 + * @return the ADLER-32 value of the uncompressed data
1.283 + */
1.284 + public int getAdler() {
1.285 + synchronized (zsRef) {
1.286 + ensureOpen();
1.287 + return getAdler(zsRef.address());
1.288 + }
1.289 + }
1.290 +
1.291 + /**
1.292 + * Returns the total number of compressed bytes input so far.
1.293 + *
1.294 + * <p>Since the number of bytes may be greater than
1.295 + * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
1.296 + * the preferred means of obtaining this information.</p>
1.297 + *
1.298 + * @return the total number of compressed bytes input so far
1.299 + */
1.300 + public int getTotalIn() {
1.301 + return (int) getBytesRead();
1.302 + }
1.303 +
1.304 + /**
1.305 + * Returns the total number of compressed bytes input so far.</p>
1.306 + *
1.307 + * @return the total (non-negative) number of compressed bytes input so far
1.308 + * @since 1.5
1.309 + */
1.310 + public long getBytesRead() {
1.311 + synchronized (zsRef) {
1.312 + ensureOpen();
1.313 + return getBytesRead(zsRef.address());
1.314 + }
1.315 + }
1.316 +
1.317 + /**
1.318 + * Returns the total number of uncompressed bytes output so far.
1.319 + *
1.320 + * <p>Since the number of bytes may be greater than
1.321 + * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
1.322 + * the preferred means of obtaining this information.</p>
1.323 + *
1.324 + * @return the total number of uncompressed bytes output so far
1.325 + */
1.326 + public int getTotalOut() {
1.327 + return (int) getBytesWritten();
1.328 + }
1.329 +
1.330 + /**
1.331 + * Returns the total number of uncompressed bytes output so far.</p>
1.332 + *
1.333 + * @return the total (non-negative) number of uncompressed bytes output so far
1.334 + * @since 1.5
1.335 + */
1.336 + public long getBytesWritten() {
1.337 + synchronized (zsRef) {
1.338 + ensureOpen();
1.339 + return getBytesWritten(zsRef.address());
1.340 + }
1.341 + }
1.342 +
1.343 + /**
1.344 + * Resets inflater so that a new set of input data can be processed.
1.345 + */
1.346 + public void reset() {
1.347 + synchronized (zsRef) {
1.348 + ensureOpen();
1.349 + reset(zsRef.address());
1.350 + buf = defaultBuf;
1.351 + finished = false;
1.352 + needDict = false;
1.353 + off = len = 0;
1.354 + }
1.355 + }
1.356 +
1.357 + /**
1.358 + * Closes the decompressor and discards any unprocessed input.
1.359 + * This method should be called when the decompressor is no longer
1.360 + * being used, but will also be called automatically by the finalize()
1.361 + * method. Once this method is called, the behavior of the Inflater
1.362 + * object is undefined.
1.363 + */
1.364 + public void end() {
1.365 + synchronized (zsRef) {
1.366 + long addr = zsRef.address();
1.367 + zsRef.clear();
1.368 + if (addr != 0) {
1.369 + end(addr);
1.370 + buf = null;
1.371 + }
1.372 + }
1.373 + }
1.374 +
1.375 + /**
1.376 + * Closes the decompressor when garbage is collected.
1.377 + */
1.378 + protected void finalize() {
1.379 + end();
1.380 + }
1.381 +
1.382 + private void ensureOpen () {
1.383 + assert Thread.holdsLock(zsRef);
1.384 + if (zsRef.address() == 0)
1.385 + throw new NullPointerException("Inflater has been closed");
1.386 + }
1.387 +
1.388 + boolean ended() {
1.389 + synchronized (zsRef) {
1.390 + return zsRef.address() == 0;
1.391 + }
1.392 + }
1.393 +
1.394 + private native static void initIDs();
1.395 + private native static long init(boolean nowrap);
1.396 + private native static void setDictionary(long addr, byte[] b, int off,
1.397 + int len);
1.398 + private native int inflateBytes(long addr, byte[] b, int off, int len)
1.399 + throws DataFormatException;
1.400 + private native static int getAdler(long addr);
1.401 + private native static long getBytesRead(long addr);
1.402 + private native static long getBytesWritten(long addr);
1.403 + private native static void reset(long addr);
1.404 + private native static void end(long addr);
1.405 +}