InflaterOutputStreampublic class InflaterOutputStream extends FilterOutputStream Implements an output stream filter for uncompressing data stored in the
"deflate" compression format. |
Fields Summary |
---|
protected final Inflater | infDecompressor for this stream. | protected final byte[] | bufOutput buffer for writing uncompressed data. | private final byte[] | wbufTemporary write buffer. | private boolean | usesDefaultInflaterDefault decompressor is used. | private boolean | closedtrue iff {@link #close()} has been called. |
Constructors Summary |
---|
public InflaterOutputStream(OutputStream out)Creates a new output stream with a default decompressor and buffer
size.
this(out, new Inflater());
usesDefaultInflater = true;
| public InflaterOutputStream(OutputStream out, Inflater infl)Creates a new output stream with the specified decompressor and a
default buffer size.
this(out, infl, 512);
| public InflaterOutputStream(OutputStream out, Inflater infl, int bufLen)Creates a new output stream with the specified decompressor and
buffer size.
super(out);
// Sanity checks
if (out == null)
throw new NullPointerException("Null output");
if (infl == null)
throw new NullPointerException("Null inflater");
if (bufLen <= 0)
throw new IllegalArgumentException("Buffer size < 1");
// Initialize
inf = infl;
buf = new byte[bufLen];
|
Methods Summary |
---|
public void | close()Writes any remaining uncompressed data to the output stream and closes
the underlying output stream.
if (!closed) {
// Complete the uncompressed output
try {
finish();
} finally {
out.close();
closed = true;
}
}
| private void | ensureOpen()Checks to make sure that this stream has not been closed.
if (closed) {
throw new IOException("Stream closed");
}
| public void | finish()Finishes writing uncompressed data to the output stream without closing
the underlying stream. Use this method when applying multiple filters in
succession to the same output stream.
ensureOpen();
// Finish decompressing and writing pending output data
flush();
if (usesDefaultInflater) {
inf.end();
}
| public void | flush()Flushes this output stream, forcing any pending buffered output bytes to be
written.
ensureOpen();
// Finish decompressing and writing pending output data
if (!inf.finished()) {
try {
while (!inf.finished() && !inf.needsInput()) {
int n;
// Decompress pending output data
n = inf.inflate(buf, 0, buf.length);
if (n < 1) {
break;
}
// Write the uncompressed output data block
out.write(buf, 0, n);
}
super.flush();
} catch (DataFormatException ex) {
// Improperly formatted compressed (ZIP) data
String msg = ex.getMessage();
if (msg == null) {
msg = "Invalid ZLIB data format";
}
throw new ZipException(msg);
}
}
| public void | write(int b)Writes a byte to the uncompressed output stream.
// Write a single byte of data
wbuf[0] = (byte) b;
write(wbuf, 0, 1);
| public void | write(byte[] b, int off, int len)Writes an array of bytes to the uncompressed output stream.
// Sanity checks
ensureOpen();
if (b == null) {
throw new NullPointerException("Null buffer for read");
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
// Write uncompressed data to the output stream
try {
for (;;) {
int n;
// Fill the decompressor buffer with output data
if (inf.needsInput()) {
int part;
if (len < 1) {
break;
}
part = (len < 512 ? len : 512);
inf.setInput(b, off, part);
off += part;
len -= part;
}
// Decompress and write blocks of output data
do {
n = inf.inflate(buf, 0, buf.length);
if (n > 0) {
out.write(buf, 0, n);
}
} while (n > 0);
// Check the decompressor
if (inf.finished()) {
break;
}
if (inf.needsDictionary()) {
throw new ZipException("ZLIB dictionary missing");
}
}
} catch (DataFormatException ex) {
// Improperly formatted compressed (ZIP) data
String msg = ex.getMessage();
if (msg == null) {
msg = "Invalid ZLIB data format";
}
throw new ZipException(msg);
}
|
|