1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
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.
35 package com.jcraft.jzlib;
37 final public class ZStream{
39 static final private int MAX_WBITS=15; // 32K LZ77 window
40 static final private int DEF_WBITS=MAX_WBITS;
42 static final private 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;
48 static final private int MAX_MEM_LEVEL=9;
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;
60 public byte[] next_in; // next input byte
61 public int next_in_index;
62 public int avail_in; // number of bytes available at next_in
63 public long total_in; // total nb of input bytes read so far
65 public byte[] next_out; // next output byte should be put there
66 public int next_out_index;
67 public int avail_out; // remaining free space at next_out
68 public long total_out; // total nb of bytes output so far
75 int data_type; // best guess about the data type: ascii or binary
78 Adler32 _adler=new Adler32();
80 public int inflateInit(){
81 return inflateInit(DEF_WBITS);
83 public int inflateInit(boolean nowrap){
84 return inflateInit(DEF_WBITS, nowrap);
86 public int inflateInit(int w){
87 return inflateInit(w, false);
90 public int inflateInit(int w, boolean nowrap){
92 return istate.inflateInit(this, nowrap?-w:w);
95 public int inflate(int f){
96 if(istate==null) return Z_STREAM_ERROR;
97 return istate.inflate(this, f);
99 public int inflateEnd(){
100 if(istate==null) return Z_STREAM_ERROR;
101 int ret=istate.inflateEnd(this);
105 public int inflateSync(){
107 return Z_STREAM_ERROR;
108 return istate.inflateSync(this);
110 public int inflateSetDictionary(byte[] dictionary, int dictLength){
112 return Z_STREAM_ERROR;
113 return istate.inflateSetDictionary(this, dictionary, dictLength);
116 public int deflateInit(int level){
117 return deflateInit(level, MAX_WBITS);
119 public int deflateInit(int level, boolean nowrap){
120 return deflateInit(level, MAX_WBITS, nowrap);
122 public int deflateInit(int level, int bits){
123 return deflateInit(level, bits, false);
125 public int deflateInit(int level, int bits, boolean nowrap){
126 dstate=new Deflate();
127 return dstate.deflateInit(this, level, nowrap?-bits:bits);
129 public int deflate(int flush){
131 return Z_STREAM_ERROR;
133 return dstate.deflate(this, flush);
135 public int deflateEnd(){
136 if(dstate==null) return Z_STREAM_ERROR;
137 int ret=dstate.deflateEnd();
141 public int deflateParams(int level, int strategy){
142 if(dstate==null) return Z_STREAM_ERROR;
143 return dstate.deflateParams(this, level, strategy);
145 public int deflateSetDictionary (byte[] dictionary, int dictLength){
147 return Z_STREAM_ERROR;
148 return dstate.deflateSetDictionary(this, dictionary, dictLength);
151 // Flush as much pending output as possible. All deflate() output goes
152 // through this function so some applications may wish to modify it
153 // to avoid allocating a large strm->next_out buffer and copying into it.
154 // (See also read_buf()).
155 void flush_pending(){
156 int len=dstate.pending;
158 if(len>avail_out) len=avail_out;
161 if(dstate.pending_buf.length<=dstate.pending_out ||
162 next_out.length<=next_out_index ||
163 dstate.pending_buf.length<(dstate.pending_out+len) ||
164 next_out.length<(next_out_index+len)){
165 System.out.println(dstate.pending_buf.length+", "+dstate.pending_out+
166 ", "+next_out.length+", "+next_out_index+", "+len);
167 System.out.println("avail_out="+avail_out);
170 System.arraycopy(dstate.pending_buf, dstate.pending_out,
171 next_out, next_out_index, len);
174 dstate.pending_out+=len;
178 if(dstate.pending==0){
179 dstate.pending_out=0;
183 // Read a new buffer from the current input stream, update the adler32
184 // and total number of bytes read. All deflate() input goes through
185 // this function so some applications may wish to modify it to avoid
186 // allocating a large strm->next_in buffer and copying from it.
187 // (See also flush_pending()).
188 int read_buf(byte[] buf, int start, int size) {
191 if(len>size) len=size;
196 if(dstate.noheader==0) {
197 adler=_adler.adler32(adler, next_in, next_in_index, len);
199 System.arraycopy(next_in, next_in_index, buf, start, len);
200 next_in_index += len;