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