KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > zip > GZIPOutputStream


1 /*
2  * @(#)GZIPOutputStream.java 1.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.zip;
9
10 import java.io.OutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 /**
14  * This class implements a stream filter for writing compressed data in
15  * the GZIP file format.
16  * @version 1.22, 12/19/03
17  * @author David Connelly
18  *
19  */

20 public
21 class GZIPOutputStream extends DeflaterOutputStream JavaDoc {
22     /**
23      * CRC-32 of uncompressed data.
24      */

25     protected CRC32 JavaDoc crc = new CRC32 JavaDoc();
26
27     /*
28      * GZIP header magic number.
29      */

30     private final static int GZIP_MAGIC = 0x8b1f;
31
32     /*
33      * Trailer size in bytes.
34      *
35      */

36     private final static int TRAILER_SIZE = 8;
37
38     /**
39      * Creates a new output stream with the specified buffer size.
40      * @param out the output stream
41      * @param size the output buffer size
42      * @exception IOException If an I/O error has occurred.
43      * @exception IllegalArgumentException if size is <= 0
44      */

45     public GZIPOutputStream(OutputStream JavaDoc out, int size) throws IOException JavaDoc {
46     super(out, new Deflater JavaDoc(Deflater.DEFAULT_COMPRESSION, true), size);
47         usesDefaultDeflater = true;
48     writeHeader();
49     crc.reset();
50     }
51
52     /**
53      * Creates a new output stream with a default buffer size.
54      * @param out the output stream
55      * @exception IOException If an I/O error has occurred.
56      */

57     public GZIPOutputStream(OutputStream JavaDoc out) throws IOException JavaDoc {
58     this(out, 512);
59     }
60
61     /**
62      * Writes array of bytes to the compressed output stream. This method
63      * will block until all the bytes are written.
64      * @param buf the data to be written
65      * @param off the start offset of the data
66      * @param len the length of the data
67      * @exception IOException If an I/O error has occurred.
68      */

69     public synchronized void write(byte[] buf, int off, int len)
70     throws IOException JavaDoc
71     {
72     super.write(buf, off, len);
73     crc.update(buf, off, len);
74     }
75
76     /**
77      * Finishes writing compressed data to the output stream without closing
78      * the underlying stream. Use this method when applying multiple filters
79      * in succession to the same output stream.
80      * @exception IOException if an I/O error has occurred
81      */

82     public void finish() throws IOException JavaDoc {
83     if (!def.finished()) {
84         def.finish();
85         while (!def.finished()) {
86                 int len = def.deflate(buf, 0, buf.length);
87                 if (def.finished() && len <= buf.length - TRAILER_SIZE) {
88                     // last deflater buffer. Fit trailer at the end
89
writeTrailer(buf, len);
90                     len = len + TRAILER_SIZE;
91                     out.write(buf, 0, len);
92                     return;
93                 }
94                 if (len > 0)
95                     out.write(buf, 0, len);
96         }
97             // if we can't fit the trailer at the end of the last
98
// deflater buffer, we write it separately
99
byte[] trailer = new byte[TRAILER_SIZE];
100         writeTrailer(trailer, 0);
101             out.write(trailer);
102     }
103     }
104   
105     /*
106      * Writes GZIP member header.
107      */

108
109     private final static byte[] header = {
110         (byte) GZIP_MAGIC, // Magic number (short)
111
(byte)(GZIP_MAGIC >> 8), // Magic number (short)
112
Deflater.DEFLATED, // Compression method (CM)
113
0, // Flags (FLG)
114
0, // Modification time MTIME (int)
115
0, // Modification time MTIME (int)
116
0, // Modification time MTIME (int)
117
0, // Modification time MTIME (int)
118
0, // Extra flags (XFLG)
119
0 // Operating system (OS)
120
};
121
122     private void writeHeader() throws IOException JavaDoc {
123         out.write(header);
124     }
125
126     /*
127      * Writes GZIP member trailer to a byte array, starting at a given
128      * offset.
129      */

130     private void writeTrailer(byte[] buf, int offset) throws IOException JavaDoc {
131         writeInt((int)crc.getValue(), buf, offset); // CRC-32 of uncompr. data
132
writeInt(def.getTotalIn(), buf, offset + 4); // Number of uncompr. bytes
133
}
134
135     /*
136      * Writes integer in Intel byte order to a byte array, starting at a
137      * given offset.
138      */

139     private void writeInt(int i, byte[] buf, int offset) throws IOException JavaDoc {
140         writeShort(i & 0xffff, buf, offset);
141         writeShort((i >> 16) & 0xffff, buf, offset + 2);
142     }
143
144     /*
145      * Writes short integer in Intel byte order to a byte array, starting
146      * at a given offset
147      */

148     private void writeShort(int s, byte[] buf, int offset) throws IOException JavaDoc {
149         buf[offset] = (byte)(s & 0xff);
150         buf[offset + 1] = (byte)((s >> 8) & 0xff);
151     }
152 }
153
Popular Tags