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