comparison src/com/jcraft/jzlib/ZStream.java @ 357:46c2115ae1c8

update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
author Carl Byington <carl@five-ten-sg.com>
date Fri, 01 Aug 2014 13:34:58 -0700
parents 0ce5cc452d02
children
comparison
equal deleted inserted replaced
356:5e91b559b5fe 357:46c2115ae1c8
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ 1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /* 2 /*
3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. 3 Copyright (c) 2000-2011 ymnk, JCraft,Inc. All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met: 6 modification, are permitted provided that the following conditions are met:
7 7
8 1. Redistributions of source code must retain the above copyright notice, 8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
10 10
11 2. Redistributions in binary form must reproduce the above copyright 11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in 12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution. 13 the documentation and/or other materials provided with the distribution.
14 14
15 3. The names of the authors may not be used to endorse or promote products 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. 16 derived from this software without specific prior written permission.
17 17
32 * and contributors of zlib. 32 * and contributors of zlib.
33 */ 33 */
34 34
35 package com.jcraft.jzlib; 35 package com.jcraft.jzlib;
36 36
37 final public class ZStream { 37 /**
38 38 * ZStream
39 static final private int MAX_WBITS = 15; // 32K LZ77 window 39 *
40 static final private int DEF_WBITS = MAX_WBITS; 40 * @deprecated Not for public use in the future.
41 41 */
42 static final private int Z_NO_FLUSH = 0; 42 @Deprecated
43 static final private int Z_PARTIAL_FLUSH = 1; 43 public class ZStream{
44 static final private int Z_SYNC_FLUSH = 2; 44
45 static final private int Z_FULL_FLUSH = 3; 45 static final private int MAX_WBITS=15; // 32K LZ77 window
46 static final private int Z_FINISH = 4; 46 static final private int DEF_WBITS=MAX_WBITS;
47 47
48 static final private int MAX_MEM_LEVEL = 9; 48 static final private int Z_NO_FLUSH=0;
49 49 static final private int Z_PARTIAL_FLUSH=1;
50 static final private int Z_OK = 0; 50 static final private int Z_SYNC_FLUSH=2;
51 static final private int Z_STREAM_END = 1; 51 static final private int Z_FULL_FLUSH=3;
52 static final private int Z_NEED_DICT = 2; 52 static final private int Z_FINISH=4;
53 static final private int Z_ERRNO = -1; 53
54 static final private int Z_STREAM_ERROR = -2; 54 static final private int MAX_MEM_LEVEL=9;
55 static final private int Z_DATA_ERROR = -3; 55
56 static final private int Z_MEM_ERROR = -4; 56 static final private int Z_OK=0;
57 static final private int Z_BUF_ERROR = -5; 57 static final private int Z_STREAM_END=1;
58 static final private int Z_VERSION_ERROR = -6; 58 static final private int Z_NEED_DICT=2;
59 59 static final private int Z_ERRNO=-1;
60 public byte[] next_in; // next input byte 60 static final private int Z_STREAM_ERROR=-2;
61 public int next_in_index; 61 static final private int Z_DATA_ERROR=-3;
62 public int avail_in; // number of bytes available at next_in 62 static final private int Z_MEM_ERROR=-4;
63 public long total_in; // total nb of input bytes read so far 63 static final private int Z_BUF_ERROR=-5;
64 64 static final private int Z_VERSION_ERROR=-6;
65 public byte[] next_out; // next output byte should be put there 65
66 public int next_out_index; 66 public byte[] next_in; // next input byte
67 public int avail_out; // remaining free space at next_out 67 public int next_in_index;
68 public long total_out; // total nb of bytes output so far 68 public int avail_in; // number of bytes available at next_in
69 69 public long total_in; // total nb of input bytes read so far
70 public String msg; 70
71 71 public byte[] next_out; // next output byte should be put there
72 Deflate dstate; 72 public int next_out_index;
73 Inflate istate; 73 public int avail_out; // remaining free space at next_out
74 74 public long total_out; // total nb of bytes output so far
75 int data_type; // best guess about the data type: ascii or binary 75
76 76 public String msg;
77 public long adler; 77
78 Adler32 _adler = new Adler32(); 78 Deflate dstate;
79 79 Inflate istate;
80 public int inflateInit() { 80
81 return inflateInit(DEF_WBITS); 81 int data_type; // best guess about the data type: ascii or binary
82 } 82
83 public int inflateInit(boolean nowrap) { 83 Checksum adler;
84 return inflateInit(DEF_WBITS, nowrap); 84
85 } 85 public ZStream(){
86 public int inflateInit(int w) { 86 this(new Adler32());
87 return inflateInit(w, false); 87 }
88 } 88
89 89 public ZStream(Checksum adler){
90 public int inflateInit(int w, boolean nowrap) { 90 this.adler=adler;
91 istate = new Inflate(); 91 }
92 return istate.inflateInit(this, nowrap ? -w : w); 92
93 } 93 public int inflateInit(){
94 94 return inflateInit(DEF_WBITS);
95 public int inflate(int f) { 95 }
96 if (istate == null) return Z_STREAM_ERROR; 96 public int inflateInit(boolean nowrap){
97 97 return inflateInit(DEF_WBITS, nowrap);
98 return istate.inflate(this, f); 98 }
99 } 99 public int inflateInit(int w){
100 public int inflateEnd() { 100 return inflateInit(w, false);
101 if (istate == null) return Z_STREAM_ERROR; 101 }
102 102 public int inflateInit(JZlib.WrapperType wrapperType) {
103 int ret = istate.inflateEnd(this); 103 return inflateInit(DEF_WBITS, wrapperType);
104 istate = null; 104 }
105 return ret; 105 public int inflateInit(int w, JZlib.WrapperType wrapperType) {
106 } 106 boolean nowrap = false;
107 public int inflateSync() { 107 if(wrapperType == JZlib.W_NONE){
108 if (istate == null) 108 nowrap = true;
109 return Z_STREAM_ERROR; 109 }
110 110 else if(wrapperType == JZlib.W_GZIP) {
111 return istate.inflateSync(this); 111 w += 16;
112 } 112 }
113 public int inflateSetDictionary(byte[] dictionary, int dictLength) { 113 else if(wrapperType == JZlib.W_ANY) {
114 if (istate == null) 114 w |= Inflate.INFLATE_ANY;
115 return Z_STREAM_ERROR; 115 }
116 116 else if(wrapperType == JZlib.W_ZLIB) {
117 return istate.inflateSetDictionary(this, dictionary, dictLength); 117 }
118 } 118 return inflateInit(w, nowrap);
119 119 }
120 public int deflateInit(int level) { 120 public int inflateInit(int w, boolean nowrap){
121 return deflateInit(level, MAX_WBITS); 121 istate=new Inflate(this);
122 } 122 return istate.inflateInit(nowrap?-w:w);
123 public int deflateInit(int level, boolean nowrap) { 123 }
124 return deflateInit(level, MAX_WBITS, nowrap); 124
125 } 125 public int inflate(int f){
126 public int deflateInit(int level, int bits) { 126 if(istate==null) return Z_STREAM_ERROR;
127 return deflateInit(level, bits, false); 127 return istate.inflate(f);
128 } 128 }
129 public int deflateInit(int level, int bits, boolean nowrap) { 129 public int inflateEnd(){
130 dstate = new Deflate(); 130 if(istate==null) return Z_STREAM_ERROR;
131 return dstate.deflateInit(this, level, nowrap ? -bits : bits); 131 int ret=istate.inflateEnd();
132 } 132 // istate = null;
133 public int deflate(int flush) { 133 return ret;
134 if (dstate == null) { 134 }
135 return Z_STREAM_ERROR; 135 public int inflateSync(){
136 } 136 if(istate == null)
137 137 return Z_STREAM_ERROR;
138 return dstate.deflate(this, flush); 138 return istate.inflateSync();
139 } 139 }
140 public int deflateEnd() { 140 public int inflateSyncPoint(){
141 if (dstate == null) return Z_STREAM_ERROR; 141 if(istate == null)
142 142 return Z_STREAM_ERROR;
143 int ret = dstate.deflateEnd(); 143 return istate.inflateSyncPoint();
144 dstate = null; 144 }
145 return ret; 145 public int inflateSetDictionary(byte[] dictionary, int dictLength){
146 } 146 if(istate == null)
147 public int deflateParams(int level, int strategy) { 147 return Z_STREAM_ERROR;
148 if (dstate == null) return Z_STREAM_ERROR; 148 return istate.inflateSetDictionary(dictionary, dictLength);
149 149 }
150 return dstate.deflateParams(this, level, strategy); 150 public boolean inflateFinished(){
151 } 151 return istate.mode==12 /*DONE*/;
152 public int deflateSetDictionary(byte[] dictionary, int dictLength) { 152 }
153 if (dstate == null) 153
154 return Z_STREAM_ERROR; 154 public int deflateInit(int level){
155 155 return deflateInit(level, MAX_WBITS);
156 return dstate.deflateSetDictionary(this, dictionary, dictLength); 156 }
157 } 157 public int deflateInit(int level, boolean nowrap){
158 158 return deflateInit(level, MAX_WBITS, nowrap);
159 // Flush as much pending output as possible. All deflate() output goes 159 }
160 // through this function so some applications may wish to modify it 160 public int deflateInit(int level, int bits){
161 // to avoid allocating a large strm->next_out buffer and copying into it. 161 return deflateInit(level, bits, false);
162 // (See also read_buf()). 162 }
163 void flush_pending() { 163 public int deflateInit(int level, int bits, int memlevel, JZlib.WrapperType wrapperType){
164 int len = dstate.pending; 164 if(bits < 9 || bits > 15){
165 165 return Z_STREAM_ERROR;
166 if (len > avail_out) len = avail_out; 166 }
167 167 if(wrapperType == JZlib.W_NONE) {
168 if (len == 0) return; 168 bits *= -1;
169 169 }
170 if (dstate.pending_buf.length <= dstate.pending_out || 170 else if(wrapperType == JZlib.W_GZIP) {
171 next_out.length <= next_out_index || 171 bits += 16;
172 dstate.pending_buf.length < (dstate.pending_out + len) || 172 }
173 next_out.length < (next_out_index + len)) { 173 else if(wrapperType == JZlib.W_ANY) {
174 System.out.println(dstate.pending_buf.length + ", " + dstate.pending_out + 174 return Z_STREAM_ERROR;
175 ", " + next_out.length + ", " + next_out_index + ", " + len); 175 }
176 System.out.println("avail_out=" + avail_out); 176 else if(wrapperType == JZlib.W_ZLIB) {
177 } 177 }
178 178 return this.deflateInit(level, bits, memlevel);
179 System.arraycopy(dstate.pending_buf, dstate.pending_out, 179 }
180 next_out, next_out_index, len); 180 public int deflateInit(int level, int bits, int memlevel){
181 next_out_index += len; 181 dstate=new Deflate(this);
182 dstate.pending_out += len; 182 return dstate.deflateInit(level, bits, memlevel);
183 total_out += len; 183 }
184 avail_out -= len; 184 public int deflateInit(int level, int bits, boolean nowrap){
185 dstate.pending -= len; 185 dstate=new Deflate(this);
186 186 return dstate.deflateInit(level, nowrap?-bits:bits);
187 if (dstate.pending == 0) { 187 }
188 dstate.pending_out = 0; 188 public int deflate(int flush){
189 } 189 if(dstate==null){
190 } 190 return Z_STREAM_ERROR;
191 191 }
192 // Read a new buffer from the current input stream, update the adler32 192 return dstate.deflate(flush);
193 // and total number of bytes read. All deflate() input goes through 193 }
194 // this function so some applications may wish to modify it to avoid 194 public int deflateEnd(){
195 // allocating a large strm->next_in buffer and copying from it. 195 if(dstate==null) return Z_STREAM_ERROR;
196 // (See also flush_pending()). 196 int ret=dstate.deflateEnd();
197 int read_buf(byte[] buf, int start, int size) { 197 dstate=null;
198 int len = avail_in; 198 return ret;
199 199 }
200 if (len > size) len = size; 200 public int deflateParams(int level, int strategy){
201 201 if(dstate==null) return Z_STREAM_ERROR;
202 if (len == 0) return 0; 202 return dstate.deflateParams(level, strategy);
203 203 }
204 avail_in -= len; 204 public int deflateSetDictionary (byte[] dictionary, int dictLength){
205 205 if(dstate == null)
206 if (dstate.noheader == 0) { 206 return Z_STREAM_ERROR;
207 adler = _adler.adler32(adler, next_in, next_in_index, len); 207 return dstate.deflateSetDictionary(dictionary, dictLength);
208 } 208 }
209 209
210 System.arraycopy(next_in, next_in_index, buf, start, len); 210 // Flush as much pending output as possible. All deflate() output goes
211 next_in_index += len; 211 // through this function so some applications may wish to modify it
212 total_in += len; 212 // to avoid allocating a large strm->next_out buffer and copying into it.
213 return len; 213 // (See also read_buf()).
214 } 214 void flush_pending(){
215 215 int len=dstate.pending;
216 public void free() { 216
217 next_in = null; 217 if(len>avail_out) len=avail_out;
218 next_out = null; 218 if(len==0) return;
219 msg = null; 219
220 _adler = null; 220 if(dstate.pending_buf.length<=dstate.pending_out ||
221 } 221 next_out.length<=next_out_index ||
222 dstate.pending_buf.length<(dstate.pending_out+len) ||
223 next_out.length<(next_out_index+len)){
224 //System.out.println(dstate.pending_buf.length+", "+dstate.pending_out+
225 // ", "+next_out.length+", "+next_out_index+", "+len);
226 //System.out.println("avail_out="+avail_out);
227 }
228
229 System.arraycopy(dstate.pending_buf, dstate.pending_out,
230 next_out, next_out_index, len);
231
232 next_out_index+=len;
233 dstate.pending_out+=len;
234 total_out+=len;
235 avail_out-=len;
236 dstate.pending-=len;
237 if(dstate.pending==0){
238 dstate.pending_out=0;
239 }
240 }
241
242 // Read a new buffer from the current input stream, update the adler32
243 // and total number of bytes read. All deflate() input goes through
244 // this function so some applications may wish to modify it to avoid
245 // allocating a large strm->next_in buffer and copying from it.
246 // (See also flush_pending()).
247 int read_buf(byte[] buf, int start, int size) {
248 int len=avail_in;
249
250 if(len>size) len=size;
251 if(len==0) return 0;
252
253 avail_in-=len;
254
255 if(dstate.wrap!=0) {
256 adler.update(next_in, next_in_index, len);
257 }
258 System.arraycopy(next_in, next_in_index, buf, start, len);
259 next_in_index += len;
260 total_in += len;
261 return len;
262 }
263
264 public long getAdler(){
265 return adler.getValue();
266 }
267
268 public void free(){
269 next_in=null;
270 next_out=null;
271 msg=null;
272 }
273
274 public void setOutput(byte[] buf){
275 setOutput(buf, 0, buf.length);
276 }
277
278 public void setOutput(byte[] buf, int off, int len){
279 next_out = buf;
280 next_out_index = off;
281 avail_out = len;
282 }
283
284 public void setInput(byte[] buf){
285 setInput(buf, 0, buf.length, false);
286 }
287
288 public void setInput(byte[] buf, boolean append){
289 setInput(buf, 0, buf.length, append);
290 }
291
292 public void setInput(byte[] buf, int off, int len, boolean append){
293 if(len<=0 && append && next_in!=null) return;
294
295 if(avail_in>0 && append){
296 byte[] tmp = new byte[avail_in+len];
297 System.arraycopy(next_in, next_in_index, tmp, 0, avail_in);
298 System.arraycopy(buf, off, tmp, avail_in, len);
299 next_in=tmp;
300 next_in_index=0;
301 avail_in+=len;
302 }
303 else{
304 next_in=buf;
305 next_in_index=off;
306 avail_in=len;
307 }
308 }
309
310 public byte[] getNextIn(){
311 return next_in;
312 }
313
314 public void setNextIn(byte[] next_in){
315 this.next_in = next_in;
316 }
317
318 public int getNextInIndex(){
319 return next_in_index;
320 }
321
322 public void setNextInIndex(int next_in_index){
323 this.next_in_index = next_in_index;
324 }
325
326 public int getAvailIn(){
327 return avail_in;
328 }
329
330 public void setAvailIn(int avail_in){
331 this.avail_in = avail_in;
332 }
333
334 public byte[] getNextOut(){
335 return next_out;
336 }
337
338 public void setNextOut(byte[] next_out){
339 this.next_out = next_out;
340 }
341
342 public int getNextOutIndex(){
343 return next_out_index;
344 }
345
346 public void setNextOutIndex(int next_out_index){
347 this.next_out_index = next_out_index;
348 }
349
350 public int getAvailOut(){
351 return avail_out;
352
353 }
354
355 public void setAvailOut(int avail_out){
356 this.avail_out = avail_out;
357 }
358
359 public long getTotalOut(){
360 return total_out;
361 }
362
363 public long getTotalIn(){
364 return total_in;
365 }
366
367 public String getMessage(){
368 return msg;
369 }
370
371 /**
372 * Those methods are expected to be override by Inflater and Deflater.
373 * In the future, they will become abstract methods.
374 */
375 public int end(){ return Z_OK; }
376 public boolean finished(){ return false; }
222 } 377 }