emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/ZStream.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.
jaroslav@694
     1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
jaroslav@694
     2
/*
jaroslav@694
     3
Copyright (c) 2000-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
import org.apidesign.bck2brwsr.emul.lang.System;
jaroslav@694
    38
jaroslav@694
    39
/**
jaroslav@694
    40
 * ZStream
jaroslav@694
    41
 *
jaroslav@694
    42
 * @deprecated  Not for public use in the future.
jaroslav@694
    43
 */
jaroslav@694
    44
@Deprecated
jaroslav@694
    45
class ZStream{
jaroslav@694
    46
jaroslav@694
    47
  static final private int MAX_WBITS=15;        // 32K LZ77 window
jaroslav@694
    48
  static final private int DEF_WBITS=MAX_WBITS;
jaroslav@694
    49
jaroslav@694
    50
  static final private int Z_NO_FLUSH=0;
jaroslav@694
    51
  static final private int Z_PARTIAL_FLUSH=1;
jaroslav@694
    52
  static final private int Z_SYNC_FLUSH=2;
jaroslav@694
    53
  static final private int Z_FULL_FLUSH=3;
jaroslav@694
    54
  static final private int Z_FINISH=4;
jaroslav@694
    55
jaroslav@694
    56
  static final private int MAX_MEM_LEVEL=9;
jaroslav@694
    57
jaroslav@694
    58
  static final private int Z_OK=0;
jaroslav@694
    59
  static final private int Z_STREAM_END=1;
jaroslav@694
    60
  static final private int Z_NEED_DICT=2;
jaroslav@694
    61
  static final private int Z_ERRNO=-1;
jaroslav@694
    62
  static final private int Z_STREAM_ERROR=-2;
jaroslav@694
    63
  static final private int Z_DATA_ERROR=-3;
jaroslav@694
    64
  static final private int Z_MEM_ERROR=-4;
jaroslav@694
    65
  static final private int Z_BUF_ERROR=-5;
jaroslav@694
    66
  static final private int Z_VERSION_ERROR=-6;
jaroslav@694
    67
jaroslav@694
    68
  public byte[] next_in;     // next input byte
jaroslav@694
    69
  public int next_in_index;
jaroslav@694
    70
  public int avail_in;       // number of bytes available at next_in
jaroslav@694
    71
  public long total_in;      // total nb of input bytes read so far
jaroslav@694
    72
jaroslav@694
    73
  public byte[] next_out;    // next output byte should be put there
jaroslav@694
    74
  public int next_out_index;
jaroslav@694
    75
  public int avail_out;      // remaining free space at next_out
jaroslav@694
    76
  public long total_out;     // total nb of bytes output so far
jaroslav@694
    77
jaroslav@694
    78
  public String msg;
jaroslav@694
    79
jaroslav@694
    80
  Inflate istate; 
jaroslav@694
    81
jaroslav@694
    82
  int data_type; // best guess about the data type: ascii or binary
jaroslav@694
    83
jaroslav@694
    84
  Checksum adler;
jaroslav@694
    85
jaroslav@694
    86
  public ZStream(){
jaroslav@694
    87
    this(new Adler32());
jaroslav@694
    88
  }
jaroslav@694
    89
jaroslav@694
    90
  public ZStream(Checksum adler){
jaroslav@694
    91
    this.adler=adler;
jaroslav@694
    92
  }
jaroslav@694
    93
jaroslav@694
    94
  public int inflateInit(){
jaroslav@694
    95
    return inflateInit(DEF_WBITS);
jaroslav@694
    96
  }
jaroslav@694
    97
  public int inflateInit(boolean nowrap){
jaroslav@694
    98
    return inflateInit(DEF_WBITS, nowrap);
jaroslav@694
    99
  }
jaroslav@694
   100
  public int inflateInit(int w){
jaroslav@694
   101
    return inflateInit(w, false);
jaroslav@694
   102
  }
jaroslav@694
   103
jaroslav@694
   104
  public int inflateInit(int w, boolean nowrap){
jaroslav@694
   105
    istate=new Inflate(this);
jaroslav@694
   106
    return istate.inflateInit(nowrap?-w:w);
jaroslav@694
   107
  }
jaroslav@694
   108
jaroslav@694
   109
  public int inflate(int f){
jaroslav@694
   110
    if(istate==null) return Z_STREAM_ERROR;
jaroslav@694
   111
    return istate.inflate(f);
jaroslav@694
   112
  }
jaroslav@694
   113
  public int inflateEnd(){
jaroslav@694
   114
    if(istate==null) return Z_STREAM_ERROR;
jaroslav@694
   115
    int ret=istate.inflateEnd();
jaroslav@694
   116
//    istate = null;
jaroslav@694
   117
    return ret;
jaroslav@694
   118
  }
jaroslav@694
   119
  
jaroslav@694
   120
  public int inflateSync(){
jaroslav@694
   121
    if(istate == null)
jaroslav@694
   122
      return Z_STREAM_ERROR;
jaroslav@694
   123
    return istate.inflateSync();
jaroslav@694
   124
  }
jaroslav@694
   125
  public int inflateSyncPoint(){
jaroslav@694
   126
    if(istate == null)
jaroslav@694
   127
      return Z_STREAM_ERROR;
jaroslav@694
   128
    return istate.inflateSyncPoint();
jaroslav@694
   129
  }
jaroslav@694
   130
  public int inflateSetDictionary(byte[] dictionary, int dictLength){
jaroslav@694
   131
    if(istate == null)
jaroslav@694
   132
      return Z_STREAM_ERROR;
jaroslav@694
   133
    return istate.inflateSetDictionary(dictionary, dictLength);
jaroslav@694
   134
  }
jaroslav@694
   135
  public boolean inflateFinished(){
jaroslav@694
   136
    return istate.mode==12 /*DONE*/;
jaroslav@694
   137
  }
jaroslav@694
   138
jaroslav@694
   139
jaroslav@694
   140
  public long getAdler(){
jaroslav@694
   141
    return adler.getValue();
jaroslav@694
   142
  }
jaroslav@694
   143
jaroslav@694
   144
  public void free(){
jaroslav@694
   145
    next_in=null;
jaroslav@694
   146
    next_out=null;
jaroslav@694
   147
    msg=null;
jaroslav@694
   148
  }
jaroslav@694
   149
jaroslav@694
   150
  public void setOutput(byte[] buf){
jaroslav@694
   151
    setOutput(buf, 0, buf.length); 
jaroslav@694
   152
  }
jaroslav@694
   153
jaroslav@694
   154
  public void setOutput(byte[] buf, int off, int len){
jaroslav@694
   155
    next_out = buf;
jaroslav@694
   156
    next_out_index = off;
jaroslav@694
   157
    avail_out = len;
jaroslav@694
   158
  }
jaroslav@694
   159
jaroslav@694
   160
  public void setInput(byte[] buf){
jaroslav@694
   161
    setInput(buf, 0, buf.length, false); 
jaroslav@694
   162
  }
jaroslav@694
   163
jaroslav@694
   164
  public void setInput(byte[] buf, boolean append){
jaroslav@694
   165
    setInput(buf, 0, buf.length, append); 
jaroslav@694
   166
  }
jaroslav@694
   167
jaroslav@694
   168
  public void setInput(byte[] buf, int off, int len, boolean append){
jaroslav@694
   169
    if(len<=0 && append && next_in!=null) return;
jaroslav@694
   170
jaroslav@694
   171
    if(avail_in>0 && append){  
jaroslav@694
   172
      byte[] tmp = new byte[avail_in+len];
jaroslav@694
   173
      System.arraycopy(next_in, next_in_index, tmp, 0, avail_in);
jaroslav@694
   174
      System.arraycopy(buf, off, tmp, avail_in, len);
jaroslav@694
   175
      next_in=tmp;
jaroslav@694
   176
      next_in_index=0;
jaroslav@694
   177
      avail_in+=len;
jaroslav@694
   178
    }
jaroslav@694
   179
    else{
jaroslav@694
   180
      next_in=buf;
jaroslav@694
   181
      next_in_index=off;
jaroslav@694
   182
      avail_in=len;
jaroslav@694
   183
    }
jaroslav@694
   184
  }
jaroslav@694
   185
jaroslav@694
   186
  public byte[] getNextIn(){
jaroslav@694
   187
    return next_in;
jaroslav@694
   188
  }
jaroslav@694
   189
jaroslav@694
   190
  public void setNextIn(byte[] next_in){
jaroslav@694
   191
    this.next_in = next_in;
jaroslav@694
   192
  }
jaroslav@694
   193
jaroslav@694
   194
  public int getNextInIndex(){
jaroslav@694
   195
    return next_in_index;
jaroslav@694
   196
  }
jaroslav@694
   197
jaroslav@694
   198
  public void setNextInIndex(int next_in_index){
jaroslav@694
   199
    this.next_in_index = next_in_index;
jaroslav@694
   200
  }
jaroslav@694
   201
jaroslav@694
   202
  public int getAvailIn(){
jaroslav@694
   203
    return avail_in;
jaroslav@694
   204
  }
jaroslav@694
   205
jaroslav@694
   206
  public void setAvailIn(int avail_in){
jaroslav@694
   207
    this.avail_in = avail_in;
jaroslav@694
   208
  }
jaroslav@694
   209
jaroslav@694
   210
  public byte[] getNextOut(){
jaroslav@694
   211
    return next_out;
jaroslav@694
   212
  }
jaroslav@694
   213
jaroslav@694
   214
  public void setNextOut(byte[] next_out){
jaroslav@694
   215
    this.next_out = next_out;
jaroslav@694
   216
  }
jaroslav@694
   217
jaroslav@694
   218
  public int getNextOutIndex(){
jaroslav@694
   219
    return next_out_index;
jaroslav@694
   220
  }
jaroslav@694
   221
jaroslav@694
   222
  public void setNextOutIndex(int next_out_index){
jaroslav@694
   223
    this.next_out_index = next_out_index;
jaroslav@694
   224
  }
jaroslav@694
   225
jaroslav@694
   226
  public int getAvailOut(){
jaroslav@694
   227
    return avail_out;
jaroslav@694
   228
jaroslav@694
   229
  }
jaroslav@694
   230
jaroslav@694
   231
  public void setAvailOut(int avail_out){
jaroslav@694
   232
    this.avail_out = avail_out;
jaroslav@694
   233
  }
jaroslav@694
   234
jaroslav@694
   235
  public long getTotalOut(){
jaroslav@694
   236
    return total_out;
jaroslav@694
   237
  }
jaroslav@694
   238
jaroslav@694
   239
  public long getTotalIn(){
jaroslav@694
   240
    return total_in;
jaroslav@694
   241
  }
jaroslav@694
   242
jaroslav@694
   243
  public String getMessage(){
jaroslav@694
   244
    return msg;
jaroslav@694
   245
  }
jaroslav@694
   246
jaroslav@694
   247
  /**
jaroslav@694
   248
   * Those methods are expected to be override by Inflater and Deflater.
jaroslav@694
   249
   * In the future, they will become abstract methods.
jaroslav@694
   250
   */ 
jaroslav@694
   251
  public int end(){ return Z_OK; }
jaroslav@694
   252
  public boolean finished(){ return false; }
jaroslav@694
   253
}