Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
*/ |
17 |
|
18 |
package org.apache.coyote.http11.filters; |
19 |
|
20 |
import java.io.IOException; |
21 |
import java.io.OutputStream; |
22 |
import java.util.zip.Deflater; |
23 |
import java.util.zip.GZIPOutputStream; |
24 |
|
25 |
/** |
26 |
* Extension of {@link GZIPOutputStream} to workaround for a couple of long |
27 |
* standing JDK bugs |
28 |
* (<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4255743">Bug |
29 |
* 4255743</a> and |
30 |
* <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4813885">Bug |
31 |
* 4813885</a>) so the GZIP'd output can be flushed. |
32 |
*/ |
33 |
public class FlushableGZIPOutputStream extends GZIPOutputStream { |
34 |
public FlushableGZIPOutputStream(OutputStream os) throws IOException { |
35 |
super(os); |
36 |
} |
37 |
|
38 |
private static final byte[] EMPTYBYTEARRAY = new byte[0]; |
39 |
private boolean hasData = false; |
40 |
|
41 |
/** |
42 |
* Here we make sure we have received data, so that the header has been for |
43 |
* sure written to the output stream already. |
44 |
*/ |
45 |
@Override |
46 |
public synchronized void write(byte[] bytes, int i, int i1) |
47 |
throws IOException { |
48 |
super.write(bytes, i, i1); |
49 |
hasData = true; |
50 |
} |
51 |
|
52 |
@Override |
53 |
public synchronized void write(int i) throws IOException { |
54 |
super.write(i); |
55 |
hasData = true; |
56 |
} |
57 |
|
58 |
@Override |
59 |
public synchronized void write(byte[] bytes) throws IOException { |
60 |
super.write(bytes); |
61 |
hasData = true; |
62 |
} |
63 |
|
64 |
@Override |
65 |
public synchronized void flush() throws IOException { |
66 |
if (!hasData) { |
67 |
return; // do not allow the gzip header to be flushed on its own |
68 |
} |
69 |
|
70 |
// trick the deflater to flush |
71 |
/** |
72 |
* Now this is tricky: We force the Deflater to flush its data by |
73 |
* switching compression level. As yet, a perplexingly simple workaround |
74 |
* for |
75 |
* http://developer.java.sun.com/developer/bugParade/bugs/4255743.html |
76 |
*/ |
77 |
if (!def.finished()) { |
78 |
def.setInput(EMPTYBYTEARRAY, 0, 0); |
79 |
|
80 |
def.setLevel(Deflater.NO_COMPRESSION); |
81 |
deflate(); |
82 |
|
83 |
def.setLevel(Deflater.DEFAULT_COMPRESSION); |
84 |
deflate(); |
85 |
|
86 |
out.flush(); |
87 |
} |
88 |
|
89 |
hasData = false; // no more data to flush |
90 |
} |
91 |
|
92 |
/* |
93 |
* Keep on calling deflate until it runs dry. The default implementation |
94 |
* only does it once and can therefore hold onto data when they need to be |
95 |
* flushed out. |
96 |
*/ |
97 |
@Override |
98 |
protected void deflate() throws IOException { |
99 |
int len; |
100 |
do { |
101 |
len = def.deflate(buf, 0, buf.length); |
102 |
if (len > 0) { |
103 |
out.write(buf, 0, len); |
104 |
} |
105 |
} while (len != 0); |
106 |
} |
107 |
|
108 |
} |
0 |
+ native |
109 |
+ native |