DeflaterOutputStreampublic class DeflaterOutputStream extends FilterOutputStream This class provides an implementation of {@code FilterOutputStream} that
compresses data using the DEFLATE algorithm. Basically it wraps the
{@code Deflater} class and takes care of the buffering. |
Fields Summary |
---|
static final int | BUF_SIZE | protected byte[] | bufThe buffer for the data to be written to. | protected Deflater | defThe deflater used. | boolean | done |
Constructors Summary |
---|
public DeflaterOutputStream(OutputStream os, Deflater def)This constructor lets you pass the {@code Deflater} specifying the
compression algorithm.
this(os, def, BUF_SIZE);
| public DeflaterOutputStream(OutputStream os)This is the most basic constructor. You only need to pass the {@code
OutputStream} to which the compressed data shall be written to. The
default settings for the {@code Deflater} and internal buffer are used.
In particular the {@code Deflater} produces a ZLIB header in the output
stream.
this(os, new Deflater());
| public DeflaterOutputStream(OutputStream os, Deflater def, int bsize)This constructor lets you specify both the compression algorithm as well
as the internal buffer size to be used.
super(os);
if (os == null || def == null) {
throw new NullPointerException();
}
if (bsize <= 0) {
throw new IllegalArgumentException();
}
this.def = def;
buf = new byte[bsize];
|
Methods Summary |
---|
public void | close()Writes any unwritten compressed data to the underlying stream, the closes
all underlying streams. This stream can no longer be used after close()
has been called.
if (!def.finished()) {
finish();
}
def.end();
out.close();
| protected void | deflate()Compress the data in the input buffer and write it to the underlying
stream.
int x = 0;
do {
x = def.deflate(buf);
out.write(buf, 0, x);
} while (!def.needsInput());
| public void | finish()Writes any unwritten data to the underlying stream. Does not close the
stream.
if (done) {
return;
}
def.finish();
int x = 0;
while (!def.finished()) {
if (def.needsInput()) {
def.setInput(buf, 0, 0);
}
x = def.deflate(buf);
out.write(buf, 0, x);
}
done = true;
| public void | write(int i)
byte[] b = new byte[1];
b[0] = (byte) i;
write(b, 0, 1);
| public void | write(byte[] buffer, int off, int nbytes)Compresses {@code nbytes} of data from {@code buf} starting at
{@code off} and writes it to the underlying stream.
if (done) {
throw new IOException(Messages.getString("archive.26")); //$NON-NLS-1$
}
// avoid int overflow, check null buf
if (off <= buffer.length && nbytes >= 0 && off >= 0
&& buffer.length - off >= nbytes) {
if (!def.needsInput()) {
throw new IOException();
}
def.setInput(buffer, off, nbytes);
deflate();
} else {
throw new ArrayIndexOutOfBoundsException();
}
|
|