Source Code Cross Referenced for GZIPOutputStream.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-2002 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.OutputStream;
029        import java.io.IOException;
030
031        /**
032         * This class implements a stream filter for writing compressed data in
033         * the GZIP file format.
034         * @version 	1.29, 05/05/07
035         * @author 	David Connelly
036         *
037         */
038        public class GZIPOutputStream extends DeflaterOutputStream {
039            /**
040             * CRC-32 of uncompressed data.
041             */
042            protected CRC32 crc = new CRC32();
043
044            /*
045             * GZIP header magic number.
046             */
047            private final static int GZIP_MAGIC = 0x8b1f;
048
049            /*
050             * Trailer size in bytes.
051             *
052             */
053            private final static int TRAILER_SIZE = 8;
054
055            /**
056             * Creates a new output stream with the specified buffer size.
057             * @param out the output stream
058             * @param size the output buffer size
059             * @exception IOException If an I/O error has occurred.
060             * @exception IllegalArgumentException if size is <= 0
061             */
062            public GZIPOutputStream(OutputStream out, int size)
063                    throws IOException {
064                super (out, new Deflater(Deflater.DEFAULT_COMPRESSION, true),
065                        size);
066                usesDefaultDeflater = true;
067                writeHeader();
068                crc.reset();
069            }
070
071            /**
072             * Creates a new output stream with a default buffer size.
073             * @param out the output stream
074             * @exception IOException If an I/O error has occurred.
075             */
076            public GZIPOutputStream(OutputStream out) throws IOException {
077                this (out, 512);
078            }
079
080            /**
081             * Writes array of bytes to the compressed output stream. This method
082             * will block until all the bytes are written.
083             * @param buf the data to be written
084             * @param off the start offset of the data
085             * @param len the length of the data
086             * @exception IOException If an I/O error has occurred.
087             */
088            public synchronized void write(byte[] buf, int off, int len)
089                    throws IOException {
090                super .write(buf, off, len);
091                crc.update(buf, off, len);
092            }
093
094            /**
095             * Finishes writing compressed data to the output stream without closing
096             * the underlying stream. Use this method when applying multiple filters
097             * in succession to the same output stream.
098             * @exception IOException if an I/O error has occurred
099             */
100            public void finish() throws IOException {
101                if (!def.finished()) {
102                    def.finish();
103                    while (!def.finished()) {
104                        int len = def.deflate(buf, 0, buf.length);
105                        if (def.finished() && len <= buf.length - TRAILER_SIZE) {
106                            // last deflater buffer. Fit trailer at the end 
107                            writeTrailer(buf, len);
108                            len = len + TRAILER_SIZE;
109                            out.write(buf, 0, len);
110                            return;
111                        }
112                        if (len > 0)
113                            out.write(buf, 0, len);
114                    }
115                    // if we can't fit the trailer at the end of the last
116                    // deflater buffer, we write it separately
117                    byte[] trailer = new byte[TRAILER_SIZE];
118                    writeTrailer(trailer, 0);
119                    out.write(trailer);
120                }
121            }
122
123            /*
124             * Writes GZIP member header.
125             */
126
127            private final static byte[] header = { (byte) GZIP_MAGIC, // Magic number (short)
128                    (byte) (GZIP_MAGIC >> 8), // Magic number (short)
129                    Deflater.DEFLATED, // Compression method (CM)
130                    0, // Flags (FLG)
131                    0, // Modification time MTIME (int)
132                    0, // Modification time MTIME (int)
133                    0, // Modification time MTIME (int)
134                    0, // Modification time MTIME (int)
135                    0, // Extra flags (XFLG)
136                    0 // Operating system (OS)
137            };
138
139            private void writeHeader() throws IOException {
140                out.write(header);
141            }
142
143            /*
144             * Writes GZIP member trailer to a byte array, starting at a given
145             * offset.
146             */
147            private void writeTrailer(byte[] buf, int offset)
148                    throws IOException {
149                writeInt((int) crc.getValue(), buf, offset); // CRC-32 of uncompr. data
150                writeInt(def.getTotalIn(), buf, offset + 4); // Number of uncompr. bytes
151            }
152
153            /*
154             * Writes integer in Intel byte order to a byte array, starting at a
155             * given offset.
156             */
157            private void writeInt(int i, byte[] buf, int offset)
158                    throws IOException {
159                writeShort(i & 0xffff, buf, offset);
160                writeShort((i >> 16) & 0xffff, buf, offset + 2);
161            }
162
163            /*
164             * Writes short integer in Intel byte order to a byte array, starting
165             * at a given offset
166             */
167            private void writeShort(int s, byte[] buf, int offset)
168                    throws IOException {
169                buf[offset] = (byte) (s & 0xff);
170                buf[offset + 1] = (byte) ((s >> 8) & 0xff);
171            }
172        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.