Source Code Cross Referenced for Deflater.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » zip » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util.zip 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util.zip;
027
028        /**
029         * This class provides support for general purpose compression using the
030         * popular ZLIB compression library. The ZLIB compression library was
031         * initially developed as part of the PNG graphics standard and is not
032         * protected by patents. It is fully described in the specifications at 
033         * the <a href="package-summary.html#package_description">java.util.zip
034         * package description</a>.
035         *
036         * <p>The following code fragment demonstrates a trivial compression
037         * and decompression of a string using <tt>Deflater</tt> and
038         * <tt>Inflater</tt>.
039         *
040         * <blockquote><pre>
041         * try {
042         *     // Encode a String into bytes
043         *     String inputString = "blahblahblah\u20AC\u20AC";
044         *     byte[] input = inputString.getBytes("UTF-8");
045         *
046         *     // Compress the bytes
047         *     byte[] output = new byte[100];
048         *     Deflater compresser = new Deflater();
049         *     compresser.setInput(input);
050         *     compresser.finish();
051         *     int compressedDataLength = compresser.deflate(output);
052         *
053         *     // Decompress the bytes
054         *     Inflater decompresser = new Inflater();
055         *     decompresser.setInput(output, 0, compressedDataLength);
056         *     byte[] result = new byte[100];
057         *     int resultLength = decompresser.inflate(result);
058         *     decompresser.end();
059         *
060         *     // Decode the bytes into a String
061         *     String outputString = new String(result, 0, resultLength, "UTF-8");
062         * } catch(java.io.UnsupportedEncodingException ex) {
063         *     // handle
064         * } catch (java.util.zip.DataFormatException ex) {
065         *     // handle
066         * }
067         * </pre></blockquote>
068         *
069         * @see		Inflater
070         * @version 	1.51, 05/05/07
071         * @author 	David Connelly
072         */
073        public class Deflater {
074            private long strm;
075            private byte[] buf = new byte[0];
076            private int off, len;
077            private int level, strategy;
078            private boolean setParams;
079            private boolean finish, finished;
080
081            /**
082             * Compression method for the deflate algorithm (the only one currently
083             * supported).
084             */
085            public static final int DEFLATED = 8;
086
087            /**
088             * Compression level for no compression.
089             */
090            public static final int NO_COMPRESSION = 0;
091
092            /**
093             * Compression level for fastest compression.
094             */
095            public static final int BEST_SPEED = 1;
096
097            /**
098             * Compression level for best compression.
099             */
100            public static final int BEST_COMPRESSION = 9;
101
102            /**
103             * Default compression level.
104             */
105            public static final int DEFAULT_COMPRESSION = -1;
106
107            /**
108             * Compression strategy best used for data consisting mostly of small
109             * values with a somewhat random distribution. Forces more Huffman coding
110             * and less string matching.
111             */
112            public static final int FILTERED = 1;
113
114            /**
115             * Compression strategy for Huffman coding only.
116             */
117            public static final int HUFFMAN_ONLY = 2;
118
119            /**
120             * Default compression strategy.
121             */
122            public static final int DEFAULT_STRATEGY = 0;
123
124            static {
125                /* Zip library is loaded from System.initializeSystemClass */
126                initIDs();
127            }
128
129            /**
130             * Creates a new compressor using the specified compression level.
131             * If 'nowrap' is true then the ZLIB header and checksum fields will
132             * not be used in order to support the compression format used in
133             * both GZIP and PKZIP.
134             * @param level the compression level (0-9)
135             * @param nowrap if true then use GZIP compatible compression
136             */
137            public Deflater(int level, boolean nowrap) {
138                this .level = level;
139                this .strategy = DEFAULT_STRATEGY;
140                strm = init(level, DEFAULT_STRATEGY, nowrap);
141            }
142
143            /** 
144             * Creates a new compressor using the specified compression level.
145             * Compressed data will be generated in ZLIB format.
146             * @param level the compression level (0-9)
147             */
148            public Deflater(int level) {
149                this (level, false);
150            }
151
152            /**
153             * Creates a new compressor with the default compression level.
154             * Compressed data will be generated in ZLIB format.
155             */
156            public Deflater() {
157                this (DEFAULT_COMPRESSION, false);
158            }
159
160            /**
161             * Sets input data for compression. This should be called whenever
162             * needsInput() returns true indicating that more input data is required.
163             * @param b the input data bytes
164             * @param off the start offset of the data
165             * @param len the length of the data
166             * @see Deflater#needsInput
167             */
168            public synchronized void setInput(byte[] b, int off, int len) {
169                if (b == null) {
170                    throw new NullPointerException();
171                }
172                if (off < 0 || len < 0 || off > b.length - len) {
173                    throw new ArrayIndexOutOfBoundsException();
174                }
175                this .buf = b;
176                this .off = off;
177                this .len = len;
178            }
179
180            /**
181             * Sets input data for compression. This should be called whenever
182             * needsInput() returns true indicating that more input data is required.
183             * @param b the input data bytes
184             * @see Deflater#needsInput
185             */
186            public void setInput(byte[] b) {
187                setInput(b, 0, b.length);
188            }
189
190            /**
191             * Sets preset dictionary for compression. A preset dictionary is used
192             * when the history buffer can be predetermined. When the data is later
193             * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
194             * in order to get the Adler-32 value of the dictionary required for
195             * decompression.
196             * @param b the dictionary data bytes
197             * @param off the start offset of the data
198             * @param len the length of the data
199             * @see Inflater#inflate
200             * @see Inflater#getAdler
201             */
202            public synchronized void setDictionary(byte[] b, int off, int len) {
203                if (strm == 0 || b == null) {
204                    throw new NullPointerException();
205                }
206                if (off < 0 || len < 0 || off > b.length - len) {
207                    throw new ArrayIndexOutOfBoundsException();
208                }
209                setDictionary(strm, b, off, len);
210            }
211
212            /**
213             * Sets preset dictionary for compression. A preset dictionary is used
214             * when the history buffer can be predetermined. When the data is later
215             * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
216             * in order to get the Adler-32 value of the dictionary required for
217             * decompression.
218             * @param b the dictionary data bytes
219             * @see Inflater#inflate
220             * @see Inflater#getAdler
221             */
222            public void setDictionary(byte[] b) {
223                setDictionary(b, 0, b.length);
224            }
225
226            /**
227             * Sets the compression strategy to the specified value.
228             * @param strategy the new compression strategy
229             * @exception IllegalArgumentException if the compression strategy is
230             *				           invalid
231             */
232            public synchronized void setStrategy(int strategy) {
233                switch (strategy) {
234                case DEFAULT_STRATEGY:
235                case FILTERED:
236                case HUFFMAN_ONLY:
237                    break;
238                default:
239                    throw new IllegalArgumentException();
240                }
241                if (this .strategy != strategy) {
242                    this .strategy = strategy;
243                    setParams = true;
244                }
245            }
246
247            /**
248             * Sets the current compression level to the specified value.
249             * @param level the new compression level (0-9)
250             * @exception IllegalArgumentException if the compression level is invalid
251             */
252            public synchronized void setLevel(int level) {
253                if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
254                    throw new IllegalArgumentException(
255                            "invalid compression level");
256                }
257                if (this .level != level) {
258                    this .level = level;
259                    setParams = true;
260                }
261            }
262
263            /**
264             * Returns true if the input data buffer is empty and setInput()
265             * should be called in order to provide more input.
266             * @return true if the input data buffer is empty and setInput()
267             * should be called in order to provide more input
268             */
269            public boolean needsInput() {
270                return len <= 0;
271            }
272
273            /**
274             * When called, indicates that compression should end with the current
275             * contents of the input buffer.
276             */
277            public synchronized void finish() {
278                finish = true;
279            }
280
281            /**
282             * Returns true if the end of the compressed data output stream has
283             * been reached.
284             * @return true if the end of the compressed data output stream has
285             * been reached
286             */
287            public synchronized boolean finished() {
288                return finished;
289            }
290
291            /**
292             * Fills specified buffer with compressed data. Returns actual number
293             * of bytes of compressed data. A return value of 0 indicates that
294             * needsInput() should be called in order to determine if more input
295             * data is required.
296             * @param b the buffer for the compressed data
297             * @param off the start offset of the data
298             * @param len the maximum number of bytes of compressed data
299             * @return the actual number of bytes of compressed data
300             */
301            public synchronized int deflate(byte[] b, int off, int len) {
302                if (b == null) {
303                    throw new NullPointerException();
304                }
305                if (off < 0 || len < 0 || off > b.length - len) {
306                    throw new ArrayIndexOutOfBoundsException();
307                }
308                return deflateBytes(b, off, len);
309            }
310
311            /**
312             * Fills specified buffer with compressed data. Returns actual number
313             * of bytes of compressed data. A return value of 0 indicates that
314             * needsInput() should be called in order to determine if more input
315             * data is required.
316             * @param b the buffer for the compressed data
317             * @return the actual number of bytes of compressed data
318             */
319            public int deflate(byte[] b) {
320                return deflate(b, 0, b.length);
321            }
322
323            /**
324             * Returns the ADLER-32 value of the uncompressed data.
325             * @return the ADLER-32 value of the uncompressed data
326             */
327            public synchronized int getAdler() {
328                ensureOpen();
329                return getAdler(strm);
330            }
331
332            /**
333             * Returns the total number of uncompressed bytes input so far.
334             *
335             * <p>Since the number of bytes may be greater than
336             * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
337             * the preferred means of obtaining this information.</p>
338             *
339             * @return the total number of uncompressed bytes input so far
340             */
341            public int getTotalIn() {
342                return (int) getBytesRead();
343            }
344
345            /**
346             * Returns the total number of uncompressed bytes input so far.</p>
347             *
348             * @return the total (non-negative) number of uncompressed bytes input so far
349             * @since 1.5
350             */
351            public synchronized long getBytesRead() {
352                ensureOpen();
353                return getBytesRead(strm);
354            }
355
356            /**
357             * Returns the total number of compressed bytes output so far.
358             *
359             * <p>Since the number of bytes may be greater than
360             * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now
361             * the preferred means of obtaining this information.</p>
362             *
363             * @return the total number of compressed bytes output so far
364             */
365            public int getTotalOut() {
366                return (int) getBytesWritten();
367            }
368
369            /**
370             * Returns the total number of compressed bytes output so far.</p>
371             *
372             * @return the total (non-negative) number of compressed bytes output so far
373             * @since 1.5
374             */
375            public synchronized long getBytesWritten() {
376                ensureOpen();
377                return getBytesWritten(strm);
378            }
379
380            /**
381             * Resets deflater so that a new set of input data can be processed.
382             * Keeps current compression level and strategy settings.
383             */
384            public synchronized void reset() {
385                ensureOpen();
386                reset(strm);
387                finish = false;
388                finished = false;
389                off = len = 0;
390            }
391
392            /**
393             * Closes the compressor and discards any unprocessed input.
394             * This method should be called when the compressor is no longer
395             * being used, but will also be called automatically by the
396             * finalize() method. Once this method is called, the behavior
397             * of the Deflater object is undefined.
398             */
399            public synchronized void end() {
400                if (strm != 0) {
401                    end(strm);
402                    strm = 0;
403                    buf = null;
404                }
405            }
406
407            /**
408             * Closes the compressor when garbage is collected.
409             */
410            protected void finalize() {
411                end();
412            }
413
414            private void ensureOpen() {
415                if (strm == 0)
416                    throw new NullPointerException();
417            }
418
419            private static native void initIDs();
420
421            private native static long init(int level, int strategy,
422                    boolean nowrap);
423
424            private native static void setDictionary(long strm, byte[] b,
425                    int off, int len);
426
427            private native int deflateBytes(byte[] b, int off, int len);
428
429            private native static int getAdler(long strm);
430
431            private native static long getBytesRead(long strm);
432
433            private native static long getBytesWritten(long strm);
434
435            private native static void reset(long strm);
436
437            private native static void end(long strm);
438        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.