View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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  
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; // next input byte
56      public int next_in_index;
57      public int avail_in; // number of bytes available at next_in
58      public long total_in; // total nb of input bytes read so far
59      public byte[] next_out; // next output byte should be put there
60      public int next_out_index;
61      public int avail_out; // remaining free space at next_out
62      public long total_out; // total nb of bytes output so far
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     // Flush as much pending output as possible. All deflate() output goes
164     // through this function so some applications may wish to modify it
165     // to avoid allocating a large strm->next_out buffer and copying into it.
166     // (See also read_buf()).
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     // Read a new buffer from the current input stream, update the adler32
201     // and total number of bytes read.  All deflate() input goes through
202     // this function so some applications may wish to modify it to avoid
203     // allocating a large strm->next_in buffer and copying from it.
204     // (See also flush_pending()).
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 }