corda/classpath/java/util/zip/InflaterInputStream.java
Joel Dice f0f35a920f return a useful value from InflaterInputStream.available
Note the following excerpt from PNGFileFormat.java in SWT:

     /*
     * InflaterInputStream does not consume all bytes in the stream
     * when it is closed. This may leave unread IDAT chunks. The fix
     * is to read all available bytes before closing it.
     */
     while (stream.available() > 0) stream.read();
     stream.close();

This code relies on the documented behavior of
InflaterInputStream.available, which must return "0 after EOF has been
reached, otherwise always return 1".  This is unlike
InputStream.available, which is documented to return "the number of
bytes that can be read (or skipped over) from this input stream
without blocking by the next caller of a method for this input
stream", and says nothing about how many bytes are left until the end
of stream.

This commit modifies InflaterInputStream.available to behave according
to Sun's documentation.
2010-08-04 18:54:47 -06:00

82 lines
2.0 KiB
Java

/* Copyright (c) 2008, 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.InputStream;
import java.io.IOException;
import java.io.EOFException;
public class InflaterInputStream extends InputStream {
private final InputStream in;
private final Inflater inflater;
private final byte[] buffer;
public InflaterInputStream(InputStream in, Inflater inflater, int bufferSize)
{
this.in = in;
this.inflater = inflater;
this.buffer = new byte[bufferSize];
}
public InflaterInputStream(InputStream in, Inflater inflater) {
this(in, inflater, 4 * 1024);
}
public InflaterInputStream(InputStream in) {
this(in, new Inflater());
}
public int read() throws IOException {
byte[] buffer = new byte[1];
int c = read(buffer);
return (c < 0 ? c : (buffer[0] & 0xFF));
}
public int read(byte[] b, int offset, int length) throws IOException {
if (inflater.finished()) {
return -1;
}
while (true) {
if (inflater.needsInput()) {
int count = in.read(buffer);
if (count > 0) {
inflater.setInput(buffer, 0, count);
} else {
throw new EOFException();
}
}
try {
int count = inflater.inflate(b, offset, length);
if (count > 0) {
return count;
} else if (inflater.needsDictionary()) {
throw new IOException("missing dictionary");
} else if (inflater.finished()) {
return -1;
}
} catch (DataFormatException e) {
throw new IOException(e);
}
}
}
public int available() throws IOException {
return inflater.finished() ? 0 : 1;
}
public void close() throws IOException {
in.close();
inflater.dispose();
}
}