查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  /*
17  Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
18  
19  Redistribution and use in source and binary forms, with or without
20  modification, are permitted provided that the following conditions are met:
21  
22    1. Redistributions of source code must retain the above copyright notice,
23       this list of conditions and the following disclaimer.
24  
25    2. Redistributions in binary form must reproduce the above copyright
26       notice, this list of conditions and the following disclaimer in
27       the documentation and/or other materials provided with the distribution.
28  
29    3. The names of the authors may not be used to endorse or promote products
30       derived from this software without specific prior written permission.
31  
32  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
33  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
35  INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
36  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
38  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  /*
44   * This program is based on zlib-1.1.3, so all credit should go authors
45   * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
46   * and contributors of zlib.
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; // next input byte
55      public int next_in_index;
56      public int avail_in; // number of bytes available at next_in
57      public long total_in; // total nb of input bytes read so far
58      public byte[] next_out; // next output byte should be put there
59      public int next_out_index;
60      public int avail_out; // remaining free space at next_out
61      public long total_out; // total nb of bytes output so far
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     // Flush as much pending output as possible. All deflate() output goes
167     // through this function so some applications may wish to modify it
168     // to avoid allocating a large strm->next_out buffer and copying into it.
169     // (See also read_buf()).
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     // Read a new buffer from the current input stream, update the adler32
204     // and total number of bytes read.  All deflate() input goes through
205     // this function so some applications may wish to modify it to avoid
206     // allocating a large strm->next_in buffer and copying from it.
207     // (See also flush_pending()).
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 }