Files to add Deflater and DeflaterOutputStream to java.util.zip for

avian
This commit is contained in:
jent
2009-12-04 12:01:31 -07:00
parent 60333c88f5
commit 99a1c12682
5 changed files with 300 additions and 0 deletions

View File

@ -0,0 +1,75 @@
/* 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.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;
}
for (int i = 0; i < length; i+= buffer.length) {
deflater.setInput(b, offset + i, Math.min(buffer.length, length - i));
while (deflater.getRemaining() > 0) {
try {
int len = deflater.deflate(buffer, 0, buffer.length);
if (len > 0) {
out.write(buffer, 0, len);
}
} catch (DataFormatException e) {
e.printStackTrace();
}
}
}
}
public void close() throws IOException {
out.close();
deflater.dispose();
}
}