Source Code Cross Referenced for GZIPInputStream.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.SequenceInputStream;
029        import java.io.ByteArrayInputStream;
030        import java.io.InputStream;
031        import java.io.IOException;
032        import java.io.EOFException;
033
034        /**
035         * This class implements a stream filter for reading compressed data in
036         * the GZIP file format.
037         *
038         * @see		InflaterInputStream
039         * @version 	1.39, 05/05/07
040         * @author 	David Connelly
041         *
042         */
043        public class GZIPInputStream extends InflaterInputStream {
044            /**
045             * CRC-32 for uncompressed data.
046             */
047            protected CRC32 crc = new CRC32();
048
049            /**
050             * Indicates end of input stream.
051             */
052            protected boolean eos;
053
054            private boolean closed = false;
055
056            /**
057             * Check to make sure that this stream has not been closed
058             */
059            private void ensureOpen() throws IOException {
060                if (closed) {
061                    throw new IOException("Stream closed");
062                }
063            }
064
065            /**
066             * Creates a new input stream with the specified buffer size.
067             * @param in the input stream
068             * @param size the input buffer size
069             * @exception IOException if an I/O error has occurred
070             * @exception IllegalArgumentException if size is <= 0
071             */
072            public GZIPInputStream(InputStream in, int size) throws IOException {
073                super (in, new Inflater(true), size);
074                usesDefaultInflater = true;
075                readHeader();
076                crc.reset();
077            }
078
079            /**
080             * Creates a new input stream with a default buffer size.
081             * @param in the input stream
082             * @exception IOException if an I/O error has occurred
083             */
084            public GZIPInputStream(InputStream in) throws IOException {
085                this (in, 512);
086            }
087
088            /**
089             * Reads uncompressed data into an array of bytes. If <code>len</code> is not
090             * zero, the method will block until some input can be decompressed; otherwise,
091             * no bytes are read and <code>0</code> is returned.
092             * @param buf the buffer into which the data is read
093             * @param off the start offset in the destination array <code>b</code>
094             * @param len the maximum number of bytes read
095             * @return	the actual number of bytes read, or -1 if the end of the
096             *		compressed input stream is reached
097             * @exception  NullPointerException If <code>buf</code> is <code>null</code>.
098             * @exception  IndexOutOfBoundsException If <code>off</code> is negative, 
099             * <code>len</code> is negative, or <code>len</code> is greater than 
100             * <code>buf.length - off</code>
101             * @exception IOException if an I/O error has occurred or the compressed
102             *			      input data is corrupt
103             */
104            public int read(byte[] buf, int off, int len) throws IOException {
105                ensureOpen();
106                if (eos) {
107                    return -1;
108                }
109                len = super .read(buf, off, len);
110                if (len == -1) {
111                    readTrailer();
112                    eos = true;
113                } else {
114                    crc.update(buf, off, len);
115                }
116                return len;
117            }
118
119            /**
120             * Closes this input stream and releases any system resources associated
121             * with the stream.
122             * @exception IOException if an I/O error has occurred
123             */
124            public void close() throws IOException {
125                if (!closed) {
126                    super .close();
127                    eos = true;
128                    closed = true;
129                }
130            }
131
132            /**
133             * GZIP header magic number.
134             */
135            public final static int GZIP_MAGIC = 0x8b1f;
136
137            /*
138             * File header flags.
139             */
140            private final static int FTEXT = 1; // Extra text
141            private final static int FHCRC = 2; // Header CRC
142            private final static int FEXTRA = 4; // Extra field
143            private final static int FNAME = 8; // File name
144            private final static int FCOMMENT = 16; // File comment
145
146            /*
147             * Reads GZIP member header.
148             */
149            private void readHeader() throws IOException {
150                CheckedInputStream in = new CheckedInputStream(this .in, crc);
151                crc.reset();
152                // Check header magic
153                if (readUShort(in) != GZIP_MAGIC) {
154                    throw new IOException("Not in GZIP format");
155                }
156                // Check compression method
157                if (readUByte(in) != 8) {
158                    throw new IOException("Unsupported compression method");
159                }
160                // Read flags
161                int flg = readUByte(in);
162                // Skip MTIME, XFL, and OS fields
163                skipBytes(in, 6);
164                // Skip optional extra field
165                if ((flg & FEXTRA) == FEXTRA) {
166                    skipBytes(in, readUShort(in));
167                }
168                // Skip optional file name
169                if ((flg & FNAME) == FNAME) {
170                    while (readUByte(in) != 0)
171                        ;
172                }
173                // Skip optional file comment
174                if ((flg & FCOMMENT) == FCOMMENT) {
175                    while (readUByte(in) != 0)
176                        ;
177                }
178                // Check optional header CRC
179                if ((flg & FHCRC) == FHCRC) {
180                    int v = (int) crc.getValue() & 0xffff;
181                    if (readUShort(in) != v) {
182                        throw new IOException("Corrupt GZIP header");
183                    }
184                }
185            }
186
187            /*
188             * Reads GZIP member trailer.
189             */
190            private void readTrailer() throws IOException {
191                InputStream in = this .in;
192                int n = inf.getRemaining();
193                if (n > 0) {
194                    in = new SequenceInputStream(new ByteArrayInputStream(buf,
195                            len - n, n), in);
196                }
197                // Uses left-to-right evaluation order
198                if ((readUInt(in) != crc.getValue()) ||
199                // rfc1952; ISIZE is the input size modulo 2^32
200                        (readUInt(in) != (inf.getBytesWritten() & 0xffffffffL)))
201                    throw new IOException("Corrupt GZIP trailer");
202            }
203
204            /*
205             * Reads unsigned integer in Intel byte order.
206             */
207            private long readUInt(InputStream in) throws IOException {
208                long s = readUShort(in);
209                return ((long) readUShort(in) << 16) | s;
210            }
211
212            /*
213             * Reads unsigned short in Intel byte order.
214             */
215            private int readUShort(InputStream in) throws IOException {
216                int b = readUByte(in);
217                return ((int) readUByte(in) << 8) | b;
218            }
219
220            /*
221             * Reads unsigned byte.
222             */
223            private int readUByte(InputStream in) throws IOException {
224                int b = in.read();
225                if (b == -1) {
226                    throw new EOFException();
227                }
228                if (b < -1 || b > 255) {
229                    // Report on this.in, not argument in; see read{Header, Trailer}.
230                    throw new IOException(this .in.getClass().getName()
231                            + ".read() returned value out of range -1..255: "
232                            + b);
233                }
234                return b;
235            }
236
237            private byte[] tmpbuf = new byte[128];
238
239            /*
240             * Skips bytes of input data blocking until all bytes are skipped.
241             * Does not assume that the input stream is capable of seeking.
242             */
243            private void skipBytes(InputStream in, int n) throws IOException {
244                while (n > 0) {
245                    int len = in.read(tmpbuf, 0, n < tmpbuf.length ? n
246                            : tmpbuf.length);
247                    if (len == -1) {
248                        throw new EOFException();
249                    }
250                    n -= len;
251                }
252            }
253        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.