Mercurial > 510Connectbot
comparison src/com/jcraft/jzlib/ZStream.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children | 46c2115ae1c8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ | |
2 /* | |
3 Copyright (c) 2000,2001,2002,2003 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 final public class ZStream { | |
38 | |
39 static final private int MAX_WBITS = 15; // 32K LZ77 window | |
40 static final private int DEF_WBITS = MAX_WBITS; | |
41 | |
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; | |
47 | |
48 static final private int MAX_MEM_LEVEL = 9; | |
49 | |
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; | |
59 | |
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 | |
64 | |
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 | |
69 | |
70 public String msg; | |
71 | |
72 Deflate dstate; | |
73 Inflate istate; | |
74 | |
75 int data_type; // best guess about the data type: ascii or binary | |
76 | |
77 public long adler; | |
78 Adler32 _adler = new Adler32(); | |
79 | |
80 public int inflateInit() { | |
81 return inflateInit(DEF_WBITS); | |
82 } | |
83 public int inflateInit(boolean nowrap) { | |
84 return inflateInit(DEF_WBITS, nowrap); | |
85 } | |
86 public int inflateInit(int w) { | |
87 return inflateInit(w, false); | |
88 } | |
89 | |
90 public int inflateInit(int w, boolean nowrap) { | |
91 istate = new Inflate(); | |
92 return istate.inflateInit(this, nowrap ? -w : w); | |
93 } | |
94 | |
95 public int inflate(int f) { | |
96 if (istate == null) return Z_STREAM_ERROR; | |
97 | |
98 return istate.inflate(this, f); | |
99 } | |
100 public int inflateEnd() { | |
101 if (istate == null) return Z_STREAM_ERROR; | |
102 | |
103 int ret = istate.inflateEnd(this); | |
104 istate = null; | |
105 return ret; | |
106 } | |
107 public int inflateSync() { | |
108 if (istate == null) | |
109 return Z_STREAM_ERROR; | |
110 | |
111 return istate.inflateSync(this); | |
112 } | |
113 public int inflateSetDictionary(byte[] dictionary, int dictLength) { | |
114 if (istate == null) | |
115 return Z_STREAM_ERROR; | |
116 | |
117 return istate.inflateSetDictionary(this, dictionary, dictLength); | |
118 } | |
119 | |
120 public int deflateInit(int level) { | |
121 return deflateInit(level, MAX_WBITS); | |
122 } | |
123 public int deflateInit(int level, boolean nowrap) { | |
124 return deflateInit(level, MAX_WBITS, nowrap); | |
125 } | |
126 public int deflateInit(int level, int bits) { | |
127 return deflateInit(level, bits, false); | |
128 } | |
129 public int deflateInit(int level, int bits, boolean nowrap) { | |
130 dstate = new Deflate(); | |
131 return dstate.deflateInit(this, level, nowrap ? -bits : bits); | |
132 } | |
133 public int deflate(int flush) { | |
134 if (dstate == null) { | |
135 return Z_STREAM_ERROR; | |
136 } | |
137 | |
138 return dstate.deflate(this, flush); | |
139 } | |
140 public int deflateEnd() { | |
141 if (dstate == null) return Z_STREAM_ERROR; | |
142 | |
143 int ret = dstate.deflateEnd(); | |
144 dstate = null; | |
145 return ret; | |
146 } | |
147 public int deflateParams(int level, int strategy) { | |
148 if (dstate == null) return Z_STREAM_ERROR; | |
149 | |
150 return dstate.deflateParams(this, level, strategy); | |
151 } | |
152 public int deflateSetDictionary(byte[] dictionary, int dictLength) { | |
153 if (dstate == null) | |
154 return Z_STREAM_ERROR; | |
155 | |
156 return dstate.deflateSetDictionary(this, dictionary, dictLength); | |
157 } | |
158 | |
159 // Flush as much pending output as possible. All deflate() output goes | |
160 // through this function so some applications may wish to modify it | |
161 // to avoid allocating a large strm->next_out buffer and copying into it. | |
162 // (See also read_buf()). | |
163 void flush_pending() { | |
164 int len = dstate.pending; | |
165 | |
166 if (len > avail_out) len = avail_out; | |
167 | |
168 if (len == 0) return; | |
169 | |
170 if (dstate.pending_buf.length <= dstate.pending_out || | |
171 next_out.length <= next_out_index || | |
172 dstate.pending_buf.length < (dstate.pending_out + len) || | |
173 next_out.length < (next_out_index + len)) { | |
174 System.out.println(dstate.pending_buf.length + ", " + dstate.pending_out + | |
175 ", " + next_out.length + ", " + next_out_index + ", " + len); | |
176 System.out.println("avail_out=" + avail_out); | |
177 } | |
178 | |
179 System.arraycopy(dstate.pending_buf, dstate.pending_out, | |
180 next_out, next_out_index, len); | |
181 next_out_index += len; | |
182 dstate.pending_out += len; | |
183 total_out += len; | |
184 avail_out -= len; | |
185 dstate.pending -= len; | |
186 | |
187 if (dstate.pending == 0) { | |
188 dstate.pending_out = 0; | |
189 } | |
190 } | |
191 | |
192 // Read a new buffer from the current input stream, update the adler32 | |
193 // and total number of bytes read. All deflate() input goes through | |
194 // this function so some applications may wish to modify it to avoid | |
195 // allocating a large strm->next_in buffer and copying from it. | |
196 // (See also flush_pending()). | |
197 int read_buf(byte[] buf, int start, int size) { | |
198 int len = avail_in; | |
199 | |
200 if (len > size) len = size; | |
201 | |
202 if (len == 0) return 0; | |
203 | |
204 avail_in -= len; | |
205 | |
206 if (dstate.noheader == 0) { | |
207 adler = _adler.adler32(adler, next_in, next_in_index, len); | |
208 } | |
209 | |
210 System.arraycopy(next_in, next_in_index, buf, start, len); | |
211 next_in_index += len; | |
212 total_in += len; | |
213 return len; | |
214 } | |
215 | |
216 public void free() { | |
217 next_in = null; | |
218 next_out = null; | |
219 msg = null; | |
220 _adler = null; | |
221 } | |
222 } |