]>
Commit | Line | Data |
---|---|---|
4f21aedb | 1 | /* GZIPOutputStream.java - Create a file in gzip format |
0db58ea4 | 2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
4f21aedb MW |
3 | |
4 | This file is part of GNU Classpath. | |
5 | ||
6 | GNU Classpath is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU Classpath is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Classpath; see the file COPYING. If not, write to the | |
917b04f4 KC |
18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | 02110-1301 USA. | |
4f21aedb | 20 | |
92aaa246 MW |
21 | Linking this library statically or dynamically with other modules is |
22 | making a combined work based on this library. Thus, the terms and | |
23 | conditions of the GNU General Public License cover the whole | |
24 | combination. | |
25 | ||
26 | As a special exception, the copyright holders of this library give you | |
27 | permission to link this library with independent modules to produce an | |
28 | executable, regardless of the license terms of these independent | |
29 | modules, and to copy and distribute the resulting executable under | |
30 | terms of your choice, provided that you also meet, for each linked | |
31 | independent module, the terms and conditions of the license of that | |
32 | module. An independent module is a module which is not derived from | |
33 | or based on this library. If you modify this library, you may extend | |
34 | this exception to your version of the library, but you are not | |
35 | obligated to do so. If you do not wish to do so, delete this | |
36 | exception statement from your version. */ | |
0ffac832 TT |
37 | |
38 | package java.util.zip; | |
39 | ||
40 | import java.io.IOException; | |
41 | import java.io.OutputStream; | |
42 | ||
43 | /** | |
d642571f MK |
44 | * This filter stream is used to compress a stream into a "GZIP" stream. |
45 | * The "GZIP" format is described in RFC 1952. | |
46 | * | |
47 | * @author John Leuner | |
0ffac832 | 48 | * @author Tom Tromey |
d642571f | 49 | * @since JDK 1.1 |
0ffac832 TT |
50 | */ |
51 | ||
52 | /* Written using on-line Java Platform 1.2 API Specification | |
53 | * and JCL book. | |
54 | * Believed complete and correct. | |
55 | */ | |
56 | ||
57 | public class GZIPOutputStream extends DeflaterOutputStream | |
58 | { | |
d642571f MK |
59 | /** |
60 | * CRC-32 value for uncompressed data | |
61 | */ | |
62 | protected CRC32 crc; | |
0ffac832 | 63 | |
d642571f MK |
64 | /** |
65 | * Creates a GZIPOutputStream with the default buffer size | |
66 | * | |
67 | * @param out The stream to read data (to be compressed) from | |
68 | * | |
69 | */ | |
70 | public GZIPOutputStream(OutputStream out) throws IOException | |
0ffac832 | 71 | { |
f370ef24 | 72 | this(out, 4096); |
0ffac832 TT |
73 | } |
74 | ||
d642571f MK |
75 | /** |
76 | * Creates a GZIPOutputStream with the specified buffer size | |
77 | * | |
78 | * @param out The stream to read compressed data from | |
79 | * @param size Size of the buffer to use | |
80 | */ | |
81 | public GZIPOutputStream(OutputStream out, int size) throws IOException | |
0ffac832 | 82 | { |
d642571f MK |
83 | super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size); |
84 | crc = new CRC32(); | |
85 | put2(GZIPInputStream.GZIP_MAGIC); | |
86 | out.write(GZIPInputStream.Z_DEFLATED); | |
0ffac832 | 87 | // No flags for now. |
d642571f | 88 | out.write(0); |
0ffac832 | 89 | // No time either. |
d642571f MK |
90 | put2(0); |
91 | put2(0); | |
0ffac832 | 92 | // No xflags either. |
d642571f | 93 | out.write(0); |
0ffac832 | 94 | // FIXME: unknown OS. |
d642571f | 95 | out.write(255); |
0ffac832 TT |
96 | } |
97 | ||
d642571f | 98 | public synchronized void write(int bval) throws IOException |
0db58ea4 | 99 | { |
d642571f MK |
100 | super.write(bval); |
101 | crc.update(bval); | |
0db58ea4 TT |
102 | } |
103 | ||
d642571f | 104 | public synchronized void write(byte[] buf) throws IOException |
2d40265f | 105 | { |
d642571f | 106 | write(buf, 0, buf.length); |
2d40265f TT |
107 | } |
108 | ||
d642571f | 109 | public synchronized void write(byte[] buf, int off, int len) |
0ffac832 TT |
110 | throws IOException |
111 | { | |
112 | super.write(buf, off, len); | |
113 | crc.update(buf, off, len); | |
114 | } | |
115 | ||
d642571f MK |
116 | /** |
117 | * Writes remaining compressed output data to the output stream | |
118 | * and closes it. | |
119 | */ | |
120 | public void close() throws IOException | |
0ffac832 | 121 | { |
d642571f MK |
122 | finish(); |
123 | out.close(); | |
0ffac832 TT |
124 | } |
125 | ||
d642571f | 126 | public void finish() throws IOException |
0ffac832 | 127 | { |
d642571f MK |
128 | super.finish(); |
129 | put4((int) crc.getValue()); | |
130 | put4(def.getTotalIn()); | |
0ffac832 TT |
131 | } |
132 | ||
d642571f MK |
133 | private final void put2(int i) throws IOException |
134 | { | |
135 | out.write(i); | |
136 | out.write(i >> 8); | |
137 | } | |
138 | ||
139 | private final void put4 (int i) throws IOException | |
140 | { | |
141 | out.write(i); | |
142 | out.write(i >> 8); | |
143 | out.write(i >> 16); | |
144 | out.write(i >> 24); | |
145 | } | |
0ffac832 | 146 | } |