mirror of
https://github.com/corda/corda.git
synced 2025-06-13 04:38:19 +00:00
fix bugs in Deflater and DeflaterOutputStream
Previously, Deflater.deflate would pass Z_SYNC_FLUSH to zlib unconditionally, which caused the output to be enormous when setInput was called repeatedly with very small input buffers. In order to allow zlib to buffer output and thereby maximize compression, we must use Z_NO_FLUSH until Deflater.finish is called, at which point we switch to Z_FINISH. We also modify DeflaterOutputStream.close to call Deflater.finish and write any remaining output to the wrapped stream.
This commit is contained in:
@ -52,23 +52,25 @@ public class DeflaterOutputStream extends OutputStream {
|
||||
} else if (length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < length; i+= buffer.length) {
|
||||
deflater.setInput(b, offset + i, Math.min(buffer.length, length - i));
|
||||
while (deflater.getRemaining() > 0) {
|
||||
try {
|
||||
int len = deflater.deflate(buffer, 0, buffer.length);
|
||||
if (len > 0) {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
} catch (DataFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
deflater.setInput(b, offset, length);
|
||||
while (deflater.getRemaining() > 0) {
|
||||
deflate();
|
||||
}
|
||||
}
|
||||
|
||||
private void deflate() throws IOException {
|
||||
int len = deflater.deflate(buffer, 0, buffer.length);
|
||||
if (len > 0) {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
deflater.finish();
|
||||
while (! deflater.finished()) {
|
||||
deflate();
|
||||
}
|
||||
out.close();
|
||||
deflater.dispose();
|
||||
}
|
||||
|
Reference in New Issue
Block a user