mirror of
https://github.com/corda/corda.git
synced 2025-01-09 14:33:30 +00:00
d4da92d300
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.
78 lines
2.1 KiB
Java
78 lines
2.1 KiB
Java
/* Copyright (c) 2009, Avian Contributors
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
for any purpose with or without fee is hereby granted, provided
|
|
that the above copyright notice and this permission notice appear
|
|
in all copies.
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
details. */
|
|
|
|
package java.util.zip;
|
|
|
|
import java.io.OutputStream;
|
|
import java.io.IOException;
|
|
|
|
public class DeflaterOutputStream extends OutputStream {
|
|
private final OutputStream out;
|
|
private final Deflater deflater;
|
|
private final byte[] buffer;
|
|
|
|
public DeflaterOutputStream(OutputStream out, Deflater deflater, int bufferSize)
|
|
{
|
|
this.out = out;
|
|
this.deflater = deflater;
|
|
this.buffer = new byte[bufferSize];
|
|
}
|
|
|
|
public DeflaterOutputStream(OutputStream out, Deflater deflater) {
|
|
this(out, deflater, 4 * 1024);
|
|
}
|
|
|
|
public DeflaterOutputStream(OutputStream out) {
|
|
this(out, new Deflater());
|
|
}
|
|
|
|
public void write(int b) throws IOException {
|
|
byte[] buffer = new byte[1];
|
|
buffer[0] = (byte)(b & 0xff);
|
|
write(buffer, 0, 1);
|
|
}
|
|
|
|
public void write(byte[] b, int offset, int length) throws IOException {
|
|
// error condition checking
|
|
if (deflater.finished()) {
|
|
throw new IOException("Already at end of stream");
|
|
} else if (offset < 0) {
|
|
throw new IndexOutOfBoundsException("Offset can't be less than zero");
|
|
} else if (length < 0) {
|
|
throw new IndexOutOfBoundsException("Length can't be less than zero");
|
|
} else if (b.length - (offset + length) < 0) {
|
|
throw new IndexOutOfBoundsException("Offset + Length is larger than the input byte array");
|
|
} else if (length == 0) {
|
|
return;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|