implement Inflater and InflaterInputStream

This commit is contained in:
Joel Dice
2007-09-17 16:15:16 -06:00
parent 8af76b0da6
commit 923c4661e8
4 changed files with 243 additions and 2 deletions

View File

@ -2,19 +2,64 @@ 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 = in;
this(in, new Inflater());
}
public int read() throws IOException {
throw new IOException("not implemented");
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");
}
} catch (DataFormatException e) {
throw new IOException(e);
}
}
}
public void close() throws IOException {
in.close();
inflater.dispose();
}
}