0N/A/*
3909N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.zip;
0N/A
0N/A/**
0N/A * This class provides support for general purpose decompression using the
0N/A * popular ZLIB compression library. The ZLIB compression library was
0N/A * initially developed as part of the PNG graphics standard and is not
0N/A * protected by patents. It is fully described in the specifications at
0N/A * the <a href="package-summary.html#package_description">java.util.zip
0N/A * package description</a>.
0N/A *
0N/A * <p>The following code fragment demonstrates a trivial compression
0N/A * and decompression of a string using <tt>Deflater</tt> and
0N/A * <tt>Inflater</tt>.
0N/A *
0N/A * <blockquote><pre>
0N/A * try {
0N/A * // Encode a String into bytes
0N/A * String inputString = "blahblahblah\u20AC\u20AC";
0N/A * byte[] input = inputString.getBytes("UTF-8");
0N/A *
0N/A * // Compress the bytes
0N/A * byte[] output = new byte[100];
0N/A * Deflater compresser = new Deflater();
0N/A * compresser.setInput(input);
0N/A * compresser.finish();
0N/A * int compressedDataLength = compresser.deflate(output);
0N/A *
0N/A * // Decompress the bytes
0N/A * Inflater decompresser = new Inflater();
0N/A * decompresser.setInput(output, 0, compressedDataLength);
0N/A * byte[] result = new byte[100];
0N/A * int resultLength = decompresser.inflate(result);
0N/A * decompresser.end();
0N/A *
0N/A * // Decode the bytes into a String
0N/A * String outputString = new String(result, 0, resultLength, "UTF-8");
0N/A * } catch(java.io.UnsupportedEncodingException ex) {
0N/A * // handle
0N/A * } catch (java.util.zip.DataFormatException ex) {
0N/A * // handle
0N/A * }
0N/A * </pre></blockquote>
0N/A *
0N/A * @see Deflater
0N/A * @author David Connelly
0N/A *
0N/A */
0N/Apublic
0N/Aclass Inflater {
2252N/A
2252N/A private final ZStreamRef zsRef;
530N/A private byte[] buf = defaultBuf;
0N/A private int off, len;
0N/A private boolean finished;
0N/A private boolean needDict;
5352N/A private long bytesRead;
5352N/A private long bytesWritten;
0N/A
530N/A private static final byte[] defaultBuf = new byte[0];
530N/A
0N/A static {
0N/A /* Zip library is loaded from System.initializeSystemClass */
0N/A initIDs();
0N/A }
0N/A
0N/A /**
0N/A * Creates a new decompressor. If the parameter 'nowrap' is true then
0N/A * the ZLIB header and checksum fields will not be used. This provides
0N/A * compatibility with the compression format used by both GZIP and PKZIP.
0N/A * <p>
0N/A * Note: When using the 'nowrap' option it is also necessary to provide
0N/A * an extra "dummy" byte as input. This is required by the ZLIB native
0N/A * library in order to support certain optimizations.
0N/A *
0N/A * @param nowrap if true then support GZIP compatible compression
0N/A */
0N/A public Inflater(boolean nowrap) {
2252N/A zsRef = new ZStreamRef(init(nowrap));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new decompressor.
0N/A */
0N/A public Inflater() {
0N/A this(false);
0N/A }
0N/A
0N/A /**
0N/A * Sets input data for decompression. Should be called whenever
0N/A * needsInput() returns true indicating that more input data is
0N/A * required.
0N/A * @param b the input data bytes
0N/A * @param off the start offset of the input data
0N/A * @param len the length of the input data
0N/A * @see Inflater#needsInput
0N/A */
2252N/A public void setInput(byte[] b, int off, int len) {
0N/A if (b == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (off < 0 || len < 0 || off > b.length - len) {
0N/A throw new ArrayIndexOutOfBoundsException();
0N/A }
2252N/A synchronized (zsRef) {
2252N/A this.buf = b;
2252N/A this.off = off;
2252N/A this.len = len;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets input data for decompression. Should be called whenever
0N/A * needsInput() returns true indicating that more input data is
0N/A * required.
0N/A * @param b the input data bytes
0N/A * @see Inflater#needsInput
0N/A */
0N/A public void setInput(byte[] b) {
0N/A setInput(b, 0, b.length);
0N/A }
0N/A
0N/A /**
0N/A * Sets the preset dictionary to the given array of bytes. Should be
0N/A * called when inflate() returns 0 and needsDictionary() returns true
0N/A * indicating that a preset dictionary is required. The method getAdler()
0N/A * can be used to get the Adler-32 value of the dictionary needed.
0N/A * @param b the dictionary data bytes
0N/A * @param off the start offset of the data
0N/A * @param len the length of the data
0N/A * @see Inflater#needsDictionary
0N/A * @see Inflater#getAdler
0N/A */
2252N/A public void setDictionary(byte[] b, int off, int len) {
2252N/A if (b == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (off < 0 || len < 0 || off > b.length - len) {
0N/A throw new ArrayIndexOutOfBoundsException();
0N/A }
2252N/A synchronized (zsRef) {
2252N/A ensureOpen();
2252N/A setDictionary(zsRef.address(), b, off, len);
2252N/A needDict = false;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the preset dictionary to the given array of bytes. Should be
0N/A * called when inflate() returns 0 and needsDictionary() returns true
0N/A * indicating that a preset dictionary is required. The method getAdler()
0N/A * can be used to get the Adler-32 value of the dictionary needed.
0N/A * @param b the dictionary data bytes
0N/A * @see Inflater#needsDictionary
0N/A * @see Inflater#getAdler
0N/A */
0N/A public void setDictionary(byte[] b) {
0N/A setDictionary(b, 0, b.length);
0N/A }
0N/A
0N/A /**
0N/A * Returns the total number of bytes remaining in the input buffer.
0N/A * This can be used to find out what bytes still remain in the input
0N/A * buffer after decompression has finished.
0N/A * @return the total number of bytes remaining in the input buffer
0N/A */
2252N/A public int getRemaining() {
2252N/A synchronized (zsRef) {
2252N/A return len;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if no data remains in the input buffer. This can
0N/A * be used to determine if #setInput should be called in order
0N/A * to provide more input.
0N/A * @return true if no data remains in the input buffer
0N/A */
2252N/A public boolean needsInput() {
2252N/A synchronized (zsRef) {
2252N/A return len <= 0;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if a preset dictionary is needed for decompression.
0N/A * @return true if a preset dictionary is needed for decompression
0N/A * @see Inflater#setDictionary
0N/A */
2252N/A public boolean needsDictionary() {
2252N/A synchronized (zsRef) {
2252N/A return needDict;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the end of the compressed data stream has been
0N/A * reached.
0N/A * @return true if the end of the compressed data stream has been
0N/A * reached
0N/A */
2252N/A public boolean finished() {
2252N/A synchronized (zsRef) {
2252N/A return finished;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Uncompresses bytes into specified buffer. Returns actual number
0N/A * of bytes uncompressed. A return value of 0 indicates that
0N/A * needsInput() or needsDictionary() should be called in order to
0N/A * determine if more input data or a preset dictionary is required.
0N/A * In the latter case, getAdler() can be used to get the Adler-32
0N/A * value of the dictionary required.
0N/A * @param b the buffer for the uncompressed data
0N/A * @param off the start offset of the data
0N/A * @param len the maximum number of uncompressed bytes
0N/A * @return the actual number of uncompressed bytes
0N/A * @exception DataFormatException if the compressed data format is invalid
0N/A * @see Inflater#needsInput
0N/A * @see Inflater#needsDictionary
0N/A */
2252N/A public int inflate(byte[] b, int off, int len)
0N/A throws DataFormatException
0N/A {
0N/A if (b == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (off < 0 || len < 0 || off > b.length - len) {
0N/A throw new ArrayIndexOutOfBoundsException();
0N/A }
2252N/A synchronized (zsRef) {
2252N/A ensureOpen();
5352N/A int thisLen = this.len;
5352N/A int n = inflateBytes(zsRef.address(), b, off, len);
5352N/A bytesWritten += n;
5352N/A bytesRead += (thisLen - this.len);
5352N/A return n;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Uncompresses bytes into specified buffer. Returns actual number
0N/A * of bytes uncompressed. A return value of 0 indicates that
0N/A * needsInput() or needsDictionary() should be called in order to
0N/A * determine if more input data or a preset dictionary is required.
0N/A * In the latter case, getAdler() can be used to get the Adler-32
0N/A * value of the dictionary required.
0N/A * @param b the buffer for the uncompressed data
0N/A * @return the actual number of uncompressed bytes
0N/A * @exception DataFormatException if the compressed data format is invalid
0N/A * @see Inflater#needsInput
0N/A * @see Inflater#needsDictionary
0N/A */
0N/A public int inflate(byte[] b) throws DataFormatException {
0N/A return inflate(b, 0, b.length);
0N/A }
0N/A
0N/A /**
0N/A * Returns the ADLER-32 value of the uncompressed data.
0N/A * @return the ADLER-32 value of the uncompressed data
0N/A */
2252N/A public int getAdler() {
2252N/A synchronized (zsRef) {
2252N/A ensureOpen();
2252N/A return getAdler(zsRef.address());
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the total number of compressed bytes input so far.
0N/A *
0N/A * <p>Since the number of bytes may be greater than
0N/A * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
0N/A * the preferred means of obtaining this information.</p>
0N/A *
0N/A * @return the total number of compressed bytes input so far
0N/A */
0N/A public int getTotalIn() {
0N/A return (int) getBytesRead();
0N/A }
0N/A
0N/A /**
0N/A * Returns the total number of compressed bytes input so far.</p>
0N/A *
0N/A * @return the total (non-negative) number of compressed bytes input so far
0N/A * @since 1.5
0N/A */
2252N/A public long getBytesRead() {
2252N/A synchronized (zsRef) {
2252N/A ensureOpen();
5352N/A return bytesRead;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the total number of uncompressed bytes output so far.
0N/A *
0N/A * <p>Since the number of bytes may be greater than
0N/A * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
0N/A * the preferred means of obtaining this information.</p>
0N/A *
0N/A * @return the total number of uncompressed bytes output so far
0N/A */
0N/A public int getTotalOut() {
0N/A return (int) getBytesWritten();
0N/A }
0N/A
0N/A /**
0N/A * Returns the total number of uncompressed bytes output so far.</p>
0N/A *
0N/A * @return the total (non-negative) number of uncompressed bytes output so far
0N/A * @since 1.5
0N/A */
2252N/A public long getBytesWritten() {
2252N/A synchronized (zsRef) {
2252N/A ensureOpen();
5352N/A return bytesWritten;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Resets inflater so that a new set of input data can be processed.
0N/A */
2252N/A public void reset() {
2252N/A synchronized (zsRef) {
2252N/A ensureOpen();
2252N/A reset(zsRef.address());
2252N/A buf = defaultBuf;
2252N/A finished = false;
2252N/A needDict = false;
2252N/A off = len = 0;
5352N/A bytesRead = bytesWritten = 0;
2252N/A }
0N/A }
0N/A
0N/A /**
0N/A * Closes the decompressor and discards any unprocessed input.
0N/A * This method should be called when the decompressor is no longer
0N/A * being used, but will also be called automatically by the finalize()
0N/A * method. Once this method is called, the behavior of the Inflater
0N/A * object is undefined.
0N/A */
2252N/A public void end() {
2252N/A synchronized (zsRef) {
2252N/A long addr = zsRef.address();
2252N/A zsRef.clear();
2252N/A if (addr != 0) {
2252N/A end(addr);
2252N/A buf = null;
2252N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Closes the decompressor when garbage is collected.
0N/A */
0N/A protected void finalize() {
0N/A end();
0N/A }
0N/A
0N/A private void ensureOpen () {
2252N/A assert Thread.holdsLock(zsRef);
2252N/A if (zsRef.address() == 0)
2252N/A throw new NullPointerException("Inflater has been closed");
0N/A }
0N/A
3335N/A boolean ended() {
3335N/A synchronized (zsRef) {
3335N/A return zsRef.address() == 0;
3335N/A }
3335N/A }
3335N/A
0N/A private native static void initIDs();
0N/A private native static long init(boolean nowrap);
2252N/A private native static void setDictionary(long addr, byte[] b, int off,
0N/A int len);
2252N/A private native int inflateBytes(long addr, byte[] b, int off, int len)
0N/A throws DataFormatException;
2252N/A private native static int getAdler(long addr);
2252N/A private native static void reset(long addr);
2252N/A private native static void end(long addr);
0N/A}