emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/JzLibInflater.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 12:58:12 +0100
branchemul
changeset 694 0d277415ed02
permissions -rw-r--r--
Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
     1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
     2 /*
     3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
     4 
     5 Redistribution and use in source and binary forms, with or without
     6 modification, are permitted provided that the following conditions are met:
     7 
     8   1. Redistributions of source code must retain the above copyright notice,
     9      this list of conditions and the following disclaimer.
    10 
    11   2. Redistributions in binary form must reproduce the above copyright 
    12      notice, this list of conditions and the following disclaimer in 
    13      the documentation and/or other materials provided with the distribution.
    14 
    15   3. The names of the authors may not be used to endorse or promote products
    16      derived from this software without specific prior written permission.
    17 
    18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
    21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
    22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    28  */
    29 /*
    30  * This program is based on zlib-1.1.3, so all credit should go authors
    31  * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
    32  * and contributors of zlib.
    33  */
    34 
    35 package org.apidesign.bck2brwsr.emul.zip;
    36 
    37 final class JzLibInflater extends ZStream{
    38 
    39   static final private int MAX_WBITS=15;        // 32K LZ77 window
    40   static final private int DEF_WBITS=MAX_WBITS;
    41 
    42   public static final int Z_NO_FLUSH=0;
    43   static final private int Z_PARTIAL_FLUSH=1;
    44   static final private int Z_SYNC_FLUSH=2;
    45   static final private int Z_FULL_FLUSH=3;
    46   static final private int Z_FINISH=4;
    47 
    48   static final private int MAX_MEM_LEVEL=9;
    49 
    50   static final private int Z_OK=0;
    51   static final private int Z_STREAM_END=1;
    52   static final private int Z_NEED_DICT=2;
    53   static final private int Z_ERRNO=-1;
    54   static final private int Z_STREAM_ERROR=-2;
    55   static final private int Z_DATA_ERROR=-3;
    56   static final private int Z_MEM_ERROR=-4;
    57   static final private int Z_BUF_ERROR=-5;
    58   static final private int Z_VERSION_ERROR=-6;
    59 
    60   public JzLibInflater() {
    61     super();
    62     init();
    63   }
    64 
    65   public JzLibInflater(int w)  {
    66     this(w, false);
    67   }
    68 
    69   public JzLibInflater(int w, boolean nowrap)  {
    70     super();
    71     int ret = init(w, nowrap);
    72     if(ret!=Z_OK)
    73       throw new IllegalStateException(ret+": "+msg);
    74   }
    75 
    76   private boolean finished = false;
    77 
    78   public int init(){
    79     return init(DEF_WBITS);
    80   }
    81 
    82   public int init(boolean nowrap){
    83     return init(DEF_WBITS, nowrap);
    84   }
    85 
    86   public int init(int w){
    87     return init(w, false);
    88   }
    89 
    90   public int init(int w, boolean nowrap){
    91     finished = false;
    92     istate=new Inflate(this);
    93     return istate.inflateInit(nowrap?-w:w);
    94   }
    95 
    96   public int inflate(int f){
    97     if(istate==null) return Z_STREAM_ERROR;
    98     int ret = istate.inflate(f);
    99     if(ret == Z_STREAM_END) 
   100       finished = true;
   101     return ret;
   102   }
   103 
   104   public int end(){
   105     finished = true;
   106     if(istate==null) return Z_STREAM_ERROR;
   107     int ret=istate.inflateEnd();
   108 //    istate = null;
   109     return ret;
   110   }
   111 
   112   public int sync(){
   113     if(istate == null)
   114       return Z_STREAM_ERROR;
   115     return istate.inflateSync();
   116   }
   117 
   118   public int syncPoint(){
   119     if(istate == null)
   120       return Z_STREAM_ERROR;
   121     return istate.inflateSyncPoint();
   122   }
   123 
   124   public int setDictionary(byte[] dictionary, int dictLength){
   125     if(istate == null)
   126       return Z_STREAM_ERROR;
   127     return istate.inflateSetDictionary(dictionary, dictLength);
   128   }
   129 
   130   public boolean finished(){
   131     return istate.mode==12 /*DONE*/;
   132   }
   133 
   134   public boolean needDict() {
   135     return istate == null ? false : istate.mode == Inflate.DICT0;
   136   }
   137 }