author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45434 | jdk/src/java.base/share/classes/java/util/zip/GZIPOutputStream.java@4582657c7260 |
child 58242 | 94bb65cb37d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18156
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
27 |
||
28 |
import java.io.OutputStream; |
|
29 |
import java.io.IOException; |
|
30 |
||
31 |
/** |
|
32 |
* This class implements a stream filter for writing compressed data in |
|
33 |
* the GZIP file format. |
|
34 |
* @author David Connelly |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
32649
diff
changeset
|
35 |
* @since 1.1 |
2 | 36 |
* |
37 |
*/ |
|
38 |
public |
|
39 |
class GZIPOutputStream extends DeflaterOutputStream { |
|
40 |
/** |
|
41 |
* CRC-32 of uncompressed data. |
|
42 |
*/ |
|
43 |
protected CRC32 crc = new CRC32(); |
|
44 |
||
45 |
/* |
|
46 |
* GZIP header magic number. |
|
47 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
48 |
private static final int GZIP_MAGIC = 0x8b1f; |
2 | 49 |
|
50 |
/* |
|
51 |
* Trailer size in bytes. |
|
52 |
* |
|
53 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
54 |
private static final int TRAILER_SIZE = 8; |
2 | 55 |
|
56 |
/** |
|
57 |
* Creates a new output stream with the specified buffer size. |
|
5607
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
58 |
* |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
59 |
* <p>The new output stream instance is created as if by invoking |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
60 |
* the 3-argument constructor GZIPOutputStream(out, size, false). |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
61 |
* |
2 | 62 |
* @param out the output stream |
63 |
* @param size the output buffer size |
|
64 |
* @exception IOException If an I/O error has occurred. |
|
18156 | 65 |
* @exception IllegalArgumentException if {@code size <= 0} |
2 | 66 |
*/ |
67 |
public GZIPOutputStream(OutputStream out, int size) throws IOException { |
|
5607
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
68 |
this(out, size, false); |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
69 |
} |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
70 |
|
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
71 |
/** |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
72 |
* Creates a new output stream with the specified buffer size and |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
73 |
* flush mode. |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
74 |
* |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
75 |
* @param out the output stream |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
76 |
* @param size the output buffer size |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
77 |
* @param syncFlush |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
78 |
* if {@code true} invocation of the inherited |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
79 |
* {@link DeflaterOutputStream#flush() flush()} method of |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
80 |
* this instance flushes the compressor with flush mode |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
81 |
* {@link Deflater#SYNC_FLUSH} before flushing the output |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
82 |
* stream, otherwise only flushes the output stream |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
83 |
* @exception IOException If an I/O error has occurred. |
18156 | 84 |
* @exception IllegalArgumentException if {@code size <= 0} |
5607
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
85 |
* |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
86 |
* @since 1.7 |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
87 |
*/ |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
88 |
public GZIPOutputStream(OutputStream out, int size, boolean syncFlush) |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
89 |
throws IOException |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
90 |
{ |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
91 |
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
92 |
size, |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
93 |
syncFlush); |
2 | 94 |
usesDefaultDeflater = true; |
95 |
writeHeader(); |
|
96 |
crc.reset(); |
|
97 |
} |
|
98 |
||
5607
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
99 |
|
2 | 100 |
/** |
101 |
* Creates a new output stream with a default buffer size. |
|
5607
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
102 |
* |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
103 |
* <p>The new output stream instance is created as if by invoking |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
104 |
* the 2-argument constructor GZIPOutputStream(out, false). |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
105 |
* |
2 | 106 |
* @param out the output stream |
107 |
* @exception IOException If an I/O error has occurred. |
|
108 |
*/ |
|
109 |
public GZIPOutputStream(OutputStream out) throws IOException { |
|
5607
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
110 |
this(out, 512, false); |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
111 |
} |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
112 |
|
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
113 |
/** |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
114 |
* Creates a new output stream with a default buffer size and |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
115 |
* the specified flush mode. |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
116 |
* |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
117 |
* @param out the output stream |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
118 |
* @param syncFlush |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
119 |
* if {@code true} invocation of the inherited |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
120 |
* {@link DeflaterOutputStream#flush() flush()} method of |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
121 |
* this instance flushes the compressor with flush mode |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
122 |
* {@link Deflater#SYNC_FLUSH} before flushing the output |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
123 |
* stream, otherwise only flushes the output stream |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
124 |
* |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
125 |
* @exception IOException If an I/O error has occurred. |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
126 |
* |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
127 |
* @since 1.7 |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
128 |
*/ |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
129 |
public GZIPOutputStream(OutputStream out, boolean syncFlush) |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
130 |
throws IOException |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
131 |
{ |
f01eda72e178
4813885: RFE: GZIPOutputStream should implement flush using Z_SYNC_FLUSH
sherman
parents:
2
diff
changeset
|
132 |
this(out, 512, syncFlush); |
2 | 133 |
} |
134 |
||
135 |
/** |
|
136 |
* Writes array of bytes to the compressed output stream. This method |
|
137 |
* will block until all the bytes are written. |
|
138 |
* @param buf the data to be written |
|
139 |
* @param off the start offset of the data |
|
140 |
* @param len the length of the data |
|
141 |
* @exception IOException If an I/O error has occurred. |
|
142 |
*/ |
|
143 |
public synchronized void write(byte[] buf, int off, int len) |
|
144 |
throws IOException |
|
145 |
{ |
|
146 |
super.write(buf, off, len); |
|
147 |
crc.update(buf, off, len); |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Finishes writing compressed data to the output stream without closing |
|
152 |
* the underlying stream. Use this method when applying multiple filters |
|
153 |
* in succession to the same output stream. |
|
154 |
* @exception IOException if an I/O error has occurred |
|
155 |
*/ |
|
156 |
public void finish() throws IOException { |
|
157 |
if (!def.finished()) { |
|
158 |
def.finish(); |
|
159 |
while (!def.finished()) { |
|
160 |
int len = def.deflate(buf, 0, buf.length); |
|
161 |
if (def.finished() && len <= buf.length - TRAILER_SIZE) { |
|
162 |
// last deflater buffer. Fit trailer at the end |
|
163 |
writeTrailer(buf, len); |
|
164 |
len = len + TRAILER_SIZE; |
|
165 |
out.write(buf, 0, len); |
|
166 |
return; |
|
167 |
} |
|
168 |
if (len > 0) |
|
169 |
out.write(buf, 0, len); |
|
170 |
} |
|
171 |
// if we can't fit the trailer at the end of the last |
|
172 |
// deflater buffer, we write it separately |
|
173 |
byte[] trailer = new byte[TRAILER_SIZE]; |
|
174 |
writeTrailer(trailer, 0); |
|
175 |
out.write(trailer); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
/* |
|
180 |
* Writes GZIP member header. |
|
181 |
*/ |
|
182 |
private void writeHeader() throws IOException { |
|
5608
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
183 |
out.write(new byte[] { |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
184 |
(byte) GZIP_MAGIC, // Magic number (short) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
185 |
(byte)(GZIP_MAGIC >> 8), // Magic number (short) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
186 |
Deflater.DEFLATED, // Compression method (CM) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
187 |
0, // Flags (FLG) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
188 |
0, // Modification time MTIME (int) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
189 |
0, // Modification time MTIME (int) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
190 |
0, // Modification time MTIME (int) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
191 |
0, // Modification time MTIME (int) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
192 |
0, // Extra flags (XFLG) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
193 |
0 // Operating system (OS) |
6771207f52b4
4853493: GZIPOutputStream passes a reference to a private array into an untrusted method
sherman
parents:
5607
diff
changeset
|
194 |
}); |
2 | 195 |
} |
196 |
||
197 |
/* |
|
198 |
* Writes GZIP member trailer to a byte array, starting at a given |
|
199 |
* offset. |
|
200 |
*/ |
|
201 |
private void writeTrailer(byte[] buf, int offset) throws IOException { |
|
202 |
writeInt((int)crc.getValue(), buf, offset); // CRC-32 of uncompr. data |
|
203 |
writeInt(def.getTotalIn(), buf, offset + 4); // Number of uncompr. bytes |
|
204 |
} |
|
205 |
||
206 |
/* |
|
207 |
* Writes integer in Intel byte order to a byte array, starting at a |
|
208 |
* given offset. |
|
209 |
*/ |
|
210 |
private void writeInt(int i, byte[] buf, int offset) throws IOException { |
|
211 |
writeShort(i & 0xffff, buf, offset); |
|
212 |
writeShort((i >> 16) & 0xffff, buf, offset + 2); |
|
213 |
} |
|
214 |
||
215 |
/* |
|
216 |
* Writes short integer in Intel byte order to a byte array, starting |
|
217 |
* at a given offset |
|
218 |
*/ |
|
219 |
private void writeShort(int s, byte[] buf, int offset) throws IOException { |
|
220 |
buf[offset] = (byte)(s & 0xff); |
|
221 |
buf[offset + 1] = (byte)((s >> 8) & 0xff); |
|
222 |
} |
|
223 |
} |