Source Code Cross Referenced for InflaterInputStream.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        import java.io.FilterInputStream;
029        import java.io.InputStream;
030        import java.io.IOException;
031        import java.io.EOFException;
032
033        /**
034         * This class implements a stream filter for uncompressing data in the
035         * "deflate" compression format. It is also used as the basis for other
036         * decompression filters, such as GZIPInputStream.
037         *
038         * @see		Inflater
039         * @version 	1.46, 05/05/07
040         * @author 	David Connelly
041         */
042        public class InflaterInputStream extends FilterInputStream {
043            /**
044             * Decompressor for this stream.
045             */
046            protected Inflater inf;
047
048            /**
049             * Input buffer for decompression.
050             */
051            protected byte[] buf;
052
053            /**
054             * Length of input buffer.
055             */
056            protected int len;
057
058            private boolean closed = false;
059            // this flag is set to true after EOF has reached
060            private boolean reachEOF = false;
061
062            /**
063             * Check to make sure that this stream has not been closed
064             */
065            private void ensureOpen() throws IOException {
066                if (closed) {
067                    throw new IOException("Stream closed");
068                }
069            }
070
071            /**
072             * Creates a new input stream with the specified decompressor and
073             * buffer size.
074             * @param in the input stream
075             * @param inf the decompressor ("inflater")
076             * @param size the input buffer size
077             * @exception IllegalArgumentException if size is <= 0
078             */
079            public InflaterInputStream(InputStream in, Inflater inf, int size) {
080                super (in);
081                if (in == null || inf == null) {
082                    throw new NullPointerException();
083                } else if (size <= 0) {
084                    throw new IllegalArgumentException("buffer size <= 0");
085                }
086                this .inf = inf;
087                buf = new byte[size];
088            }
089
090            /**
091             * Creates a new input stream with the specified decompressor and a
092             * default buffer size.
093             * @param in the input stream
094             * @param inf the decompressor ("inflater")
095             */
096            public InflaterInputStream(InputStream in, Inflater inf) {
097                this (in, inf, 512);
098            }
099
100            boolean usesDefaultInflater = false;
101
102            /**
103             * Creates a new input stream with a default decompressor and buffer size.
104             * @param in the input stream
105             */
106            public InflaterInputStream(InputStream in) {
107                this (in, new Inflater());
108                usesDefaultInflater = true;
109            }
110
111            private byte[] singleByteBuf = new byte[1];
112
113            /**
114             * Reads a byte of uncompressed data. This method will block until
115             * enough input is available for decompression.
116             * @return the byte read, or -1 if end of compressed input is reached
117             * @exception IOException if an I/O error has occurred
118             */
119            public int read() throws IOException {
120                ensureOpen();
121                return read(singleByteBuf, 0, 1) == -1 ? -1
122                        : singleByteBuf[0] & 0xff;
123            }
124
125            /**
126             * Reads uncompressed data into an array of bytes. If <code>len</code> is not
127             * zero, the method will block until some input can be decompressed; otherwise,
128             * no bytes are read and <code>0</code> is returned.
129             * @param b the buffer into which the data is read
130             * @param off the start offset in the destination array <code>b</code>
131             * @param len the maximum number of bytes read
132             * @return the actual number of bytes read, or -1 if the end of the
133             *         compressed input is reached or a preset dictionary is needed
134             * @exception  NullPointerException If <code>b</code> is <code>null</code>.
135             * @exception  IndexOutOfBoundsException If <code>off</code> is negative, 
136             * <code>len</code> is negative, or <code>len</code> is greater than 
137             * <code>b.length - off</code>
138             * @exception ZipException if a ZIP format error has occurred
139             * @exception IOException if an I/O error has occurred
140             */
141            public int read(byte[] b, int off, int len) throws IOException {
142                ensureOpen();
143                if (b == null) {
144                    throw new NullPointerException();
145                } else if (off < 0 || len < 0 || len > b.length - off) {
146                    throw new IndexOutOfBoundsException();
147                } else if (len == 0) {
148                    return 0;
149                }
150                try {
151                    int n;
152                    while ((n = inf.inflate(b, off, len)) == 0) {
153                        if (inf.finished() || inf.needsDictionary()) {
154                            reachEOF = true;
155                            return -1;
156                        }
157                        if (inf.needsInput()) {
158                            fill();
159                        }
160                    }
161                    return n;
162                } catch (DataFormatException e) {
163                    String s = e.getMessage();
164                    throw new ZipException(s != null ? s
165                            : "Invalid ZLIB data format");
166                }
167            }
168
169            /**
170             * Returns 0 after EOF has been reached, otherwise always return 1.
171             * <p>
172             * Programs should not count on this method to return the actual number
173             * of bytes that could be read without blocking.
174             *
175             * @return     1 before EOF and 0 after EOF.
176             * @exception  IOException  if an I/O error occurs.
177             * 
178             */
179            public int available() throws IOException {
180                ensureOpen();
181                if (reachEOF) {
182                    return 0;
183                } else {
184                    return 1;
185                }
186            }
187
188            private byte[] b = new byte[512];
189
190            /**
191             * Skips specified number of bytes of uncompressed data.
192             * @param n the number of bytes to skip
193             * @return the actual number of bytes skipped.
194             * @exception IOException if an I/O error has occurred
195             * @exception IllegalArgumentException if n < 0
196             */
197            public long skip(long n) throws IOException {
198                if (n < 0) {
199                    throw new IllegalArgumentException("negative skip length");
200                }
201                ensureOpen();
202                int max = (int) Math.min(n, Integer.MAX_VALUE);
203                int total = 0;
204                while (total < max) {
205                    int len = max - total;
206                    if (len > b.length) {
207                        len = b.length;
208                    }
209                    len = read(b, 0, len);
210                    if (len == -1) {
211                        reachEOF = true;
212                        break;
213                    }
214                    total += len;
215                }
216                return total;
217            }
218
219            /**
220             * Closes this input stream and releases any system resources associated
221             * with the stream.
222             * @exception IOException if an I/O error has occurred
223             */
224            public void close() throws IOException {
225                if (!closed) {
226                    if (usesDefaultInflater)
227                        inf.end();
228                    in.close();
229                    closed = true;
230                }
231            }
232
233            /**
234             * Fills input buffer with more data to decompress.
235             * @exception IOException if an I/O error has occurred
236             */
237            protected void fill() throws IOException {
238                ensureOpen();
239                len = in.read(buf, 0, buf.length);
240                if (len == -1) {
241                    throw new EOFException(
242                            "Unexpected end of ZLIB input stream");
243                }
244                inf.setInput(buf, 0, len);
245            }
246
247            /**
248             * Tests if this input stream supports the <code>mark</code> and
249             * <code>reset</code> methods. The <code>markSupported</code>
250             * method of <code>InflaterInputStream</code> returns
251             * <code>false</code>.
252             *
253             * @return  a <code>boolean</code> indicating if this stream type supports
254             *          the <code>mark</code> and <code>reset</code> methods.
255             * @see     java.io.InputStream#mark(int)
256             * @see     java.io.InputStream#reset()
257             */
258            public boolean markSupported() {
259                return false;
260            }
261
262            /**
263             * Marks the current position in this input stream.
264             *
265             * <p> The <code>mark</code> method of <code>InflaterInputStream</code>
266             * does nothing.
267             *
268             * @param   readlimit   the maximum limit of bytes that can be read before
269             *                      the mark position becomes invalid.
270             * @see     java.io.InputStream#reset()
271             */
272            public synchronized void mark(int readlimit) {
273            }
274
275            /**
276             * Repositions this stream to the position at the time the
277             * <code>mark</code> method was last called on this input stream.
278             *
279             * <p> The method <code>reset</code> for class
280             * <code>InflaterInputStream</code> does nothing except throw an
281             * <code>IOException</code>.
282             *
283             * @exception  IOException  if this method is invoked.
284             * @see     java.io.InputStream#mark(int)
285             * @see     java.io.IOException
286             */
287            public synchronized void reset() throws IOException {
288                throw new IOException("mark/reset not supported");
289            }
290        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.