Source Code Cross Referenced for Deflater.java in  » 6.0-JDK-Modules » j2me » java » util » zip » Java Source Code / Java DocumentationJava Source Code and Java Documentation

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 geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.util.zip 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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