KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > util > Deflater


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2001 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.util;
54
55 /******************************************************************************
56  * A zlib deflater interface that matches {@link java.util.zip.Deflater},
57  * except additional flush operations are supported. This class requires native
58  * code support and looks for a library named "TroveZip".
59  *
60  * @author Brian S O'Neill
61  * @version
62  * <!--$$Revision:--> 2 <!-- $-->, <!--$$JustDate:--> 01/06/13 <!-- $-->
63  */

64 public class Deflater {
65     public static final int
66         DEFLATED = 8,
67         NO_COMPRESSION = 0,
68         BEST_SPEED = 1,
69         BEST_COMPRESSION = 9,
70         DEFAULT_COMPRESSION = -1,
71         FILTERED = 1,
72         HUFFMAN_ONLY = 2,
73         DEFAULT_STRATEGY = 0;
74
75     private static final int
76         NO_FLUSH = 0,
77         SYNC_FLUSH = 2,
78         FULL_FLUSH = 3,
79         FINISH = 4;
80
81     static {
82         System.loadLibrary("TroveZip");
83         initIDs();
84     }
85
86     // Pointer to strm used by native deflate functions.
87
private long mStream;
88     private boolean mNoWrap;
89
90     private int mStrategy;
91     private int mLevel;
92     private boolean mSetParams;
93
94     private int mFlushOption = NO_FLUSH;
95     private boolean mFinished;
96
97     private byte[] mInputBuf;
98     private int mInputOffset;
99     private int mInputLength;
100
101     public Deflater(int level, boolean nowrap) {
102         mStream = init(DEFAULT_STRATEGY, level, nowrap);
103         mStrategy = DEFAULT_STRATEGY;
104         mLevel = level;
105         mNoWrap = nowrap;
106     }
107
108     public Deflater(int level) {
109         this(level, false);
110     }
111
112     public Deflater() {
113         this(DEFAULT_COMPRESSION, false);
114     }
115
116     public boolean isNoWrap() {
117         return mNoWrap;
118     }
119
120     public synchronized void setInput(byte[] b, int off, int len) {
121         boundsCheck(b, off, len);
122         mInputLength = len;
123         mInputOffset = off;
124         mInputBuf = b;
125     }
126     
127     public synchronized void setInput(byte[] b) {
128         mInputLength = b.length;
129         mInputOffset = 0;
130         mInputBuf = b;
131     }
132     
133     public synchronized void setDictionary(byte[] b, int off, int len) {
134         boundsCheck(b, off, len);
135         setDictionary(mStream, b, off, len);
136     }
137     
138     public synchronized void setDictionary(byte[] b) {
139         setDictionary(mStream, b, 0, b.length);
140     }
141
142     public synchronized void setStrategy(int strategy) {
143         mStrategy = strategy;
144         mSetParams = true;
145     }
146     
147     public synchronized void setLevel(int level) {
148         mLevel = level;
149         mSetParams = true;
150     }
151
152     public boolean needsInput() {
153         return mInputLength <= 0;
154     }
155
156     /**
157      * When called, indicates that the current input buffer contents should be
158      * flushed out when deflate is next called.
159      */

160     public void flush() {
161         mFlushOption = SYNC_FLUSH;
162     }
163
164     /**
165      * When called, indicates that the current input buffer contents should be
166      * flushed out when deflate is next called, but all compression information
167      * up to this point is cleared.
168      */

169     public void fullFlush() {
170         mFlushOption = FULL_FLUSH;
171     }
172
173     /**
174      * When called, indicates that compression should end with the current
175      * contents of the input buffer. Deflate must be called to get the final
176      * compressed bytes.
177      */

178     public void finish() {
179         mFlushOption = FINISH;
180     }
181
182     public synchronized boolean finished() {
183         return mFinished;
184     }
185
186     public int deflate(byte[] b, int off, int len) {
187         boundsCheck(b, off, len);
188         return deflate0(b, off, len);
189     }
190
191     public int deflate(byte[] b) {
192         return deflate0(b, 0, b.length);
193     }
194
195     private synchronized int deflate0(byte[] b, int off, int len) {
196         int amt = deflate(mStream, mFlushOption, mSetParams,
197                           mInputBuf, mInputOffset, mInputLength,
198                           b, off, len);
199         if (amt < len) {
200             if (mFlushOption == SYNC_FLUSH || mFlushOption == FULL_FLUSH) {
201                 mFlushOption = NO_FLUSH;
202             }
203         }
204         return amt;
205     }
206
207     public synchronized int getAdler() {
208         return getAdler(mStream);
209     }
210
211     public synchronized int getTotalIn() {
212         return getTotalIn(mStream);
213     }
214
215     public synchronized int getTotalOut() {
216         return getTotalOut(mStream);
217     }
218
219     private int mResetCount;
220
221     public synchronized void reset() {
222         mFinished = false;
223         mFlushOption = NO_FLUSH;
224         mInputBuf = null;
225         mInputLength = 0;
226         reset(mStream);
227     }
228
229     public synchronized void end() {
230         end(mStream);
231     }
232
233     protected void finalize() {
234         end();
235     }
236
237     private void boundsCheck(byte[] b, int off, int len) {
238         if (off < 0 || len < 0 || off + len > b.length) {
239             throw new ArrayIndexOutOfBoundsException JavaDoc();
240         }
241     }
242
243     private static native void initIDs();
244
245     private native long init(int strategy, int level, boolean nowrap);
246
247     private native void setDictionary(long strm, byte[] b, int off, int len);
248
249     private native int deflate(long strm, int flushOpt, boolean setParams,
250                                byte[] inBuf, int inOff, int inLen,
251                                byte[] outBuf, int outOff, int outLen);
252
253     private native int getAdler(long strm);
254     private native int getTotalIn(long strm);
255     private native int getTotalOut(long strm);
256     private native void reset(long strm);
257     private native void end(long strm);
258 }
259
Popular Tags