Source Code Cross Referenced for InflaterOutputStream.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 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        import java.io.FilterOutputStream;
029        import java.io.IOException;
030        import java.io.OutputStream;
031
032        /**
033         * Implements an output stream filter for uncompressing data stored in the
034         * "deflate" compression format.
035         *
036         * @version	1.7
037         * @since	1.6
038         * @author	David R Tribble (david@tribble.com)
039         *
040         * @see	InflaterInputStream
041         * @see	DeflaterInputStream
042         * @see	DeflaterOutputStream
043         */
044
045        public class InflaterOutputStream extends FilterOutputStream {
046            /** Decompressor for this stream. */
047            protected final Inflater inf;
048
049            /** Output buffer for writing uncompressed data. */
050            protected final byte[] buf;
051
052            /** Temporary write buffer. */
053            private final byte[] wbuf = new byte[1];
054
055            /** Default decompressor is used. */
056            private boolean usesDefaultInflater = false;
057
058            /** true iff {@link #close()} has been called. */
059            private boolean closed = false;
060
061            /**
062             * Checks to make sure that this stream has not been closed.
063             */
064            private void ensureOpen() throws IOException {
065                if (closed) {
066                    throw new IOException("Stream closed");
067                }
068            }
069
070            /**
071             * Creates a new output stream with a default decompressor and buffer
072             * size.
073             *
074             * @param out output stream to write the uncompressed data to
075             * @throws NullPointerException if {@code out} is null
076             */
077            public InflaterOutputStream(OutputStream out) {
078                this (out, new Inflater());
079                usesDefaultInflater = true;
080            }
081
082            /**
083             * Creates a new output stream with the specified decompressor and a
084             * default buffer size.
085             *
086             * @param out output stream to write the uncompressed data to
087             * @param infl decompressor ("inflater") for this stream
088             * @throws NullPointerException if {@code out} or {@code infl} is null
089             */
090            public InflaterOutputStream(OutputStream out, Inflater infl) {
091                this (out, infl, 512);
092            }
093
094            /**
095             * Creates a new output stream with the specified decompressor and
096             * buffer size.
097             *
098             * @param out output stream to write the uncompressed data to
099             * @param infl decompressor ("inflater") for this stream
100             * @param bufLen decompression buffer size
101             * @throws IllegalArgumentException if {@code bufLen} is <= 0
102             * @throws NullPointerException if {@code out} or {@code infl} is null
103             */
104            public InflaterOutputStream(OutputStream out, Inflater infl,
105                    int bufLen) {
106                super (out);
107
108                // Sanity checks
109                if (out == null)
110                    throw new NullPointerException("Null output");
111                if (infl == null)
112                    throw new NullPointerException("Null inflater");
113                if (bufLen <= 0)
114                    throw new IllegalArgumentException("Buffer size < 1");
115
116                // Initialize
117                inf = infl;
118                buf = new byte[bufLen];
119            }
120
121            /**
122             * Writes any remaining uncompressed data to the output stream and closes
123             * the underlying output stream.
124             *
125             * @throws IOException if an I/O error occurs
126             */
127            public void close() throws IOException {
128                if (!closed) {
129                    // Complete the uncompressed output
130                    try {
131                        finish();
132                    } finally {
133                        out.close();
134                        closed = true;
135                    }
136                }
137            }
138
139            /**
140             * Flushes this output stream, forcing any pending buffered output bytes to be
141             * written.
142             *
143             * @throws IOException if an I/O error occurs or this stream is already
144             * closed
145             */
146            public void flush() throws IOException {
147                ensureOpen();
148
149                // Finish decompressing and writing pending output data
150                if (!inf.finished()) {
151                    try {
152                        while (!inf.finished() && !inf.needsInput()) {
153                            int n;
154
155                            // Decompress pending output data
156                            n = inf.inflate(buf, 0, buf.length);
157                            if (n < 1) {
158                                break;
159                            }
160
161                            // Write the uncompressed output data block
162                            out.write(buf, 0, n);
163                        }
164                        super .flush();
165                    } catch (DataFormatException ex) {
166                        // Improperly formatted compressed (ZIP) data
167                        String msg = ex.getMessage();
168                        if (msg == null) {
169                            msg = "Invalid ZLIB data format";
170                        }
171                        throw new ZipException(msg);
172                    }
173                }
174            }
175
176            /**
177             * Finishes writing uncompressed data to the output stream without closing
178             * the underlying stream.  Use this method when applying multiple filters in
179             * succession to the same output stream.
180             *
181             * @throws IOException if an I/O error occurs or this stream is already
182             * closed
183             */
184            public void finish() throws IOException {
185                ensureOpen();
186
187                // Finish decompressing and writing pending output data
188                flush();
189                if (usesDefaultInflater) {
190                    inf.end();
191                }
192            }
193
194            /**
195             * Writes a byte to the uncompressed output stream.
196             *
197             * @param b a single byte of compressed data to decompress and write to
198             * the output stream
199             * @throws IOException if an I/O error occurs or this stream is already
200             * closed
201             * @throws ZipException if a compression (ZIP) format error occurs
202             */
203            public void write(int b) throws IOException {
204                // Write a single byte of data
205                wbuf[0] = (byte) b;
206                write(wbuf, 0, 1);
207            }
208
209            /**
210             * Writes an array of bytes to the uncompressed output stream.
211             *
212             * @param b buffer containing compressed data to decompress and write to
213             * the output stream
214             * @param off starting offset of the compressed data within {@code b}
215             * @param len number of bytes to decompress from {@code b}
216             * @throws IndexOutOfBoundsException if {@code off} < 0, or if
217             * {@code len} < 0, or if {@code len} > {@code b.length - off}
218             * @throws IOException if an I/O error occurs or this stream is already
219             * closed
220             * @throws NullPointerException if {@code b} is null
221             * @throws ZipException if a compression (ZIP) format error occurs
222             */
223            public void write(byte[] b, int off, int len) throws IOException {
224                // Sanity checks
225                ensureOpen();
226                if (b == null) {
227                    throw new NullPointerException("Null buffer for read");
228                } else if (off < 0 || len < 0 || len > b.length - off) {
229                    throw new IndexOutOfBoundsException();
230                } else if (len == 0) {
231                    return;
232                }
233
234                // Write uncompressed data to the output stream
235                try {
236                    for (;;) {
237                        int n;
238
239                        // Fill the decompressor buffer with output data
240                        if (inf.needsInput()) {
241                            int part;
242
243                            if (len < 1) {
244                                break;
245                            }
246
247                            part = (len < 512 ? len : 512);
248                            inf.setInput(b, off, part);
249                            off += part;
250                            len -= part;
251                        }
252
253                        // Decompress and write blocks of output data
254                        do {
255                            n = inf.inflate(buf, 0, buf.length);
256                            if (n > 0) {
257                                out.write(buf, 0, n);
258                            }
259                        } while (n > 0);
260
261                        // Check the decompressor
262                        if (inf.finished()) {
263                            break;
264                        }
265                        if (inf.needsDictionary()) {
266                            throw new ZipException("ZLIB dictionary missing");
267                        }
268                    }
269                } catch (DataFormatException ex) {
270                    // Improperly formatted compressed (ZIP) data
271                    String msg = ex.getMessage();
272                    if (msg == null) {
273                        msg = "Invalid ZLIB data format";
274                    }
275                    throw new ZipException(msg);
276                }
277            }
278        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.