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