comparison app/src/main/java/com/jcraft/jzlib/ZStream.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/jcraft/jzlib/ZStream.java@46c2115ae1c8
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2000-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 com.jcraft.jzlib;
36
37 /**
38 * ZStream
39 *
40 * @deprecated Not for public use in the future.
41 */
42 @Deprecated
43 public class ZStream{
44
45 static final private int MAX_WBITS=15; // 32K LZ77 window
46 static final private int DEF_WBITS=MAX_WBITS;
47
48 static final private int Z_NO_FLUSH=0;
49 static final private int Z_PARTIAL_FLUSH=1;
50 static final private int Z_SYNC_FLUSH=2;
51 static final private int Z_FULL_FLUSH=3;
52 static final private int Z_FINISH=4;
53
54 static final private int MAX_MEM_LEVEL=9;
55
56 static final private int Z_OK=0;
57 static final private int Z_STREAM_END=1;
58 static final private int Z_NEED_DICT=2;
59 static final private int Z_ERRNO=-1;
60 static final private int Z_STREAM_ERROR=-2;
61 static final private int Z_DATA_ERROR=-3;
62 static final private int Z_MEM_ERROR=-4;
63 static final private int Z_BUF_ERROR=-5;
64 static final private int Z_VERSION_ERROR=-6;
65
66 public byte[] next_in; // next input byte
67 public int next_in_index;
68 public int avail_in; // number of bytes available at next_in
69 public long total_in; // total nb of input bytes read so far
70
71 public byte[] next_out; // next output byte should be put there
72 public int next_out_index;
73 public int avail_out; // remaining free space at next_out
74 public long total_out; // total nb of bytes output so far
75
76 public String msg;
77
78 Deflate dstate;
79 Inflate istate;
80
81 int data_type; // best guess about the data type: ascii or binary
82
83 Checksum adler;
84
85 public ZStream(){
86 this(new Adler32());
87 }
88
89 public ZStream(Checksum adler){
90 this.adler=adler;
91 }
92
93 public int inflateInit(){
94 return inflateInit(DEF_WBITS);
95 }
96 public int inflateInit(boolean nowrap){
97 return inflateInit(DEF_WBITS, nowrap);
98 }
99 public int inflateInit(int w){
100 return inflateInit(w, false);
101 }
102 public int inflateInit(JZlib.WrapperType wrapperType) {
103 return inflateInit(DEF_WBITS, wrapperType);
104 }
105 public int inflateInit(int w, JZlib.WrapperType wrapperType) {
106 boolean nowrap = false;
107 if(wrapperType == JZlib.W_NONE){
108 nowrap = true;
109 }
110 else if(wrapperType == JZlib.W_GZIP) {
111 w += 16;
112 }
113 else if(wrapperType == JZlib.W_ANY) {
114 w |= Inflate.INFLATE_ANY;
115 }
116 else if(wrapperType == JZlib.W_ZLIB) {
117 }
118 return inflateInit(w, nowrap);
119 }
120 public int inflateInit(int w, boolean nowrap){
121 istate=new Inflate(this);
122 return istate.inflateInit(nowrap?-w:w);
123 }
124
125 public int inflate(int f){
126 if(istate==null) return Z_STREAM_ERROR;
127 return istate.inflate(f);
128 }
129 public int inflateEnd(){
130 if(istate==null) return Z_STREAM_ERROR;
131 int ret=istate.inflateEnd();
132 // istate = null;
133 return ret;
134 }
135 public int inflateSync(){
136 if(istate == null)
137 return Z_STREAM_ERROR;
138 return istate.inflateSync();
139 }
140 public int inflateSyncPoint(){
141 if(istate == null)
142 return Z_STREAM_ERROR;
143 return istate.inflateSyncPoint();
144 }
145 public int inflateSetDictionary(byte[] dictionary, int dictLength){
146 if(istate == null)
147 return Z_STREAM_ERROR;
148 return istate.inflateSetDictionary(dictionary, dictLength);
149 }
150 public boolean inflateFinished(){
151 return istate.mode==12 /*DONE*/;
152 }
153
154 public int deflateInit(int level){
155 return deflateInit(level, MAX_WBITS);
156 }
157 public int deflateInit(int level, boolean nowrap){
158 return deflateInit(level, MAX_WBITS, nowrap);
159 }
160 public int deflateInit(int level, int bits){
161 return deflateInit(level, bits, false);
162 }
163 public int deflateInit(int level, int bits, int memlevel, JZlib.WrapperType wrapperType){
164 if(bits < 9 || bits > 15){
165 return Z_STREAM_ERROR;
166 }
167 if(wrapperType == JZlib.W_NONE) {
168 bits *= -1;
169 }
170 else if(wrapperType == JZlib.W_GZIP) {
171 bits += 16;
172 }
173 else if(wrapperType == JZlib.W_ANY) {
174 return Z_STREAM_ERROR;
175 }
176 else if(wrapperType == JZlib.W_ZLIB) {
177 }
178 return this.deflateInit(level, bits, memlevel);
179 }
180 public int deflateInit(int level, int bits, int memlevel){
181 dstate=new Deflate(this);
182 return dstate.deflateInit(level, bits, memlevel);
183 }
184 public int deflateInit(int level, int bits, boolean nowrap){
185 dstate=new Deflate(this);
186 return dstate.deflateInit(level, nowrap?-bits:bits);
187 }
188 public int deflate(int flush){
189 if(dstate==null){
190 return Z_STREAM_ERROR;
191 }
192 return dstate.deflate(flush);
193 }
194 public int deflateEnd(){
195 if(dstate==null) return Z_STREAM_ERROR;
196 int ret=dstate.deflateEnd();
197 dstate=null;
198 return ret;
199 }
200 public int deflateParams(int level, int strategy){
201 if(dstate==null) return Z_STREAM_ERROR;
202 return dstate.deflateParams(level, strategy);
203 }
204 public int deflateSetDictionary (byte[] dictionary, int dictLength){
205 if(dstate == null)
206 return Z_STREAM_ERROR;
207 return dstate.deflateSetDictionary(dictionary, dictLength);
208 }
209
210 // Flush as much pending output as possible. All deflate() output goes
211 // through this function so some applications may wish to modify it
212 // to avoid allocating a large strm->next_out buffer and copying into it.
213 // (See also read_buf()).
214 void flush_pending(){
215 int len=dstate.pending;
216
217 if(len>avail_out) len=avail_out;
218 if(len==0) return;
219
220 if(dstate.pending_buf.length<=dstate.pending_out ||
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; }
377 }