1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 package org.jboss.netty.util.internal.jzlib;
49
50 import org.jboss.netty.util.internal.jzlib.JZlib.WrapperType;
51
52 public final class ZStream {
53
54 public byte[] next_in;
55 public int next_in_index;
56 public int avail_in;
57 public long total_in;
58 public byte[] next_out;
59 public int next_out_index;
60 public int avail_out;
61 public long total_out;
62 public String msg;
63 Deflate dstate;
64 Inflate istate;
65 long adler;
66 int crc32;
67
68 public int inflateInit() {
69 return inflateInit(JZlib.DEF_WBITS);
70 }
71
72 public int inflateInit(Enum<?> wrapperType) {
73 return inflateInit(JZlib.DEF_WBITS, wrapperType);
74 }
75
76 public int inflateInit(int w) {
77 return inflateInit(w, WrapperType.ZLIB);
78 }
79
80 public int inflateInit(int w, @SuppressWarnings("rawtypes") Enum wrapperType) {
81 istate = new Inflate();
82 return istate.inflateInit(this, w, (WrapperType) wrapperType);
83 }
84
85 public int inflate(int f) {
86 if (istate == null) {
87 return JZlib.Z_STREAM_ERROR;
88 }
89 return istate.inflate(this, f);
90 }
91
92 public int inflateEnd() {
93 if (istate == null) {
94 return JZlib.Z_STREAM_ERROR;
95 }
96 int ret = istate.inflateEnd(this);
97 istate = null;
98 return ret;
99 }
100
101 public int inflateSync() {
102 if (istate == null) {
103 return JZlib.Z_STREAM_ERROR;
104 }
105 return istate.inflateSync(this);
106 }
107
108 public int inflateSetDictionary(byte[] dictionary, int dictLength) {
109 if (istate == null) {
110 return JZlib.Z_STREAM_ERROR;
111 }
112 return Inflate.inflateSetDictionary(this, dictionary, dictLength);
113 }
114
115 public int deflateInit(int level) {
116 return deflateInit(level, JZlib.MAX_WBITS);
117 }
118
119 public int deflateInit(int level, Enum<?> wrapperType) {
120 return deflateInit(level, JZlib.MAX_WBITS, wrapperType);
121 }
122
123 public int deflateInit(int level, int bits) {
124 return deflateInit(level, bits, WrapperType.ZLIB);
125 }
126
127 public int deflateInit(int level, int bits, Enum<?> wrapperType) {
128 return deflateInit(level, bits, JZlib.DEF_MEM_LEVEL, wrapperType);
129 }
130
131 public int deflateInit(int level, int bits, int memLevel, @SuppressWarnings("rawtypes") Enum wrapperType) {
132 dstate = new Deflate();
133 return dstate.deflateInit(this, level, bits, memLevel, (WrapperType) wrapperType);
134 }
135
136 public int deflate(int flush) {
137 if (dstate == null) {
138 return JZlib.Z_STREAM_ERROR;
139 }
140 return dstate.deflate(this, flush);
141 }
142
143 public int deflateEnd() {
144 if (dstate == null) {
145 return JZlib.Z_STREAM_ERROR;
146 }
147 int ret = dstate.deflateEnd();
148 dstate = null;
149 return ret;
150 }
151
152 public int deflateParams(int level, int strategy) {
153 if (dstate == null) {
154 return JZlib.Z_STREAM_ERROR;
155 }
156 return dstate.deflateParams(this, level, strategy);
157 }
158
159 public int deflateSetDictionary(byte[] dictionary, int dictLength) {
160 if (dstate == null) {
161 return JZlib.Z_STREAM_ERROR;
162 }
163 return dstate.deflateSetDictionary(this, dictionary, dictLength);
164 }
165
166
167
168
169
170 void flush_pending() {
171 int len = dstate.pending;
172
173 if (len > avail_out) {
174 len = avail_out;
175 }
176 if (len == 0) {
177 return;
178 }
179
180 if (dstate.pending_buf.length <= dstate.pending_out ||
181 next_out.length <= next_out_index ||
182 dstate.pending_buf.length < dstate.pending_out + len ||
183 next_out.length < next_out_index + len) {
184 System.out.println(dstate.pending_buf.length + ", " +
185 dstate.pending_out + ", " + next_out.length + ", " +
186 next_out_index + ", " + len);
187 System.out.println("avail_out=" + avail_out);
188 }
189
190 System.arraycopy(dstate.pending_buf, dstate.pending_out, next_out,
191 next_out_index, len);
192
193 next_out_index += len;
194 dstate.pending_out += len;
195 total_out += len;
196 avail_out -= len;
197 dstate.pending -= len;
198 if (dstate.pending == 0) {
199 dstate.pending_out = 0;
200 }
201 }
202
203
204
205
206
207
208 int read_buf(byte[] buf, int start, int size) {
209 int len = avail_in;
210
211 if (len > size) {
212 len = size;
213 }
214 if (len == 0) {
215 return 0;
216 }
217
218 avail_in -= len;
219
220 switch (dstate.wrapperType) {
221 case ZLIB:
222 adler = Adler32.adler32(adler, next_in, next_in_index, len);
223 break;
224 case GZIP:
225 crc32 = CRC32.crc32(crc32, next_in, next_in_index, len);
226 break;
227 }
228
229 System.arraycopy(next_in, next_in_index, buf, start, len);
230 next_in_index += len;
231 total_in += len;
232 return len;
233 }
234
235 public void free() {
236 next_in = null;
237 next_out = null;
238 msg = null;
239 }
240 }