Lines 35-92
Link Here
|
35 |
super(os); |
35 |
super(os); |
36 |
} |
36 |
} |
37 |
|
37 |
|
38 |
private static final byte[] EMPTYBYTEARRAY = new byte[0]; |
|
|
39 |
private boolean hasData = false; |
40 |
|
41 |
/** |
38 |
/** |
42 |
* Here we make sure we have received data, so that the header has been for |
39 |
* It is used to reserve one byte of real data so that it can be used when |
43 |
* sure written to the output stream already. |
40 |
* flushing the stream. |
44 |
*/ |
41 |
*/ |
|
|
42 |
private byte[] lastByte = new byte[1]; |
43 |
private boolean hasLastByte = false; |
44 |
|
45 |
@Override |
45 |
@Override |
46 |
public synchronized void write(byte[] bytes, int i, int i1) |
46 |
public void write(byte[] bytes) throws IOException { |
|
|
47 |
write(bytes, 0, bytes.length); |
48 |
} |
49 |
|
50 |
@Override |
51 |
public synchronized void write(byte[] bytes, int offset, int length) |
47 |
throws IOException { |
52 |
throws IOException { |
48 |
super.write(bytes, i, i1); |
53 |
if (length > 0) { |
49 |
hasData = true; |
54 |
flushLastByte(); |
|
|
55 |
if (length > 1) { |
56 |
super.write(bytes, offset, length - 1); |
57 |
} |
58 |
rememberLastByte(bytes[offset + length - 1]); |
59 |
} |
50 |
} |
60 |
} |
51 |
|
61 |
|
52 |
@Override |
62 |
@Override |
53 |
public synchronized void write(int i) throws IOException { |
63 |
public synchronized void write(int i) throws IOException { |
54 |
super.write(i); |
64 |
flushLastByte(); |
55 |
hasData = true; |
65 |
rememberLastByte((byte) i); |
56 |
} |
66 |
} |
57 |
|
67 |
|
58 |
@Override |
68 |
@Override |
59 |
public synchronized void write(byte[] bytes) throws IOException { |
69 |
public synchronized void close() throws IOException { |
60 |
super.write(bytes); |
70 |
flushLastByte(); |
61 |
hasData = true; |
71 |
super.close(); |
62 |
} |
72 |
} |
63 |
|
73 |
|
|
|
74 |
private void rememberLastByte(byte b) { |
75 |
lastByte[0] = b; |
76 |
hasLastByte = true; |
77 |
} |
78 |
|
79 |
private void flushLastByte() throws IOException { |
80 |
if (hasLastByte) { |
81 |
super.write(lastByte, 0, 1); |
82 |
hasLastByte = false; |
83 |
} |
84 |
} |
85 |
|
64 |
@Override |
86 |
@Override |
65 |
public synchronized void flush() throws IOException { |
87 |
public synchronized void flush() throws IOException { |
66 |
if (!hasData) { |
88 |
if (hasLastByte) { |
67 |
return; // do not allow the gzip header to be flushed on its own |
89 |
// - do not allow the gzip header to be flushed on its own |
68 |
} |
90 |
// - do not do anything if there is no data to send |
69 |
|
91 |
|
70 |
// trick the deflater to flush |
92 |
// trick the deflater to flush |
71 |
/** |
93 |
/** |
72 |
* Now this is tricky: We force the Deflater to flush its data by |
94 |
* Now this is tricky: We force the Deflater to flush its data by |
73 |
* switching compression level. As yet, a perplexingly simple workaround |
95 |
* switching compression level. As yet, a perplexingly simple workaround |
74 |
* for |
96 |
* for |
75 |
* http://developer.java.sun.com/developer/bugParade/bugs/4255743.html |
97 |
* http://developer.java.sun.com/developer/bugParade/bugs/4255743.html |
76 |
*/ |
98 |
*/ |
77 |
if (!def.finished()) { |
99 |
if (!def.finished()) { |
78 |
def.setInput(EMPTYBYTEARRAY, 0, 0); |
100 |
def.setLevel(Deflater.NO_COMPRESSION); |
79 |
|
101 |
flushLastByte(); |
80 |
def.setLevel(Deflater.NO_COMPRESSION); |
102 |
def.setLevel(Deflater.DEFAULT_COMPRESSION); |
81 |
deflate(); |
103 |
} |
82 |
|
|
|
83 |
def.setLevel(Deflater.DEFAULT_COMPRESSION); |
84 |
deflate(); |
85 |
|
86 |
out.flush(); |
87 |
} |
104 |
} |
88 |
|
105 |
out.flush(); |
89 |
hasData = false; // no more data to flush |
|
|
90 |
} |
106 |
} |
91 |
|
107 |
|
92 |
/* |
108 |
/* |