+2004-07-09 Michael Koch <konqueror@gmx.de>
+
+ * java/util/zip/DeflaterOutputStream.java,
+ java/util/zip/GZIPInputStream.java,
+ java/util/zip/GZIPOutputStream.java,
+ java/util/zip/InflaterInputStream.java:
+ Reformatted. Added javadocs. Reordered all stuff.
+ Renamed variables to be more clear.
+
2004-07-09 Michael Koch <konqueror@gmx.de>
* javax/imageio/IIOException.java,
*/
public class DeflaterOutputStream extends FilterOutputStream
{
- public void close () throws IOException
- {
- finish ();
- out.close();
- }
+ /**
+ * This buffer is used temporarily to retrieve the bytes from the
+ * deflater and write them to the underlying output stream.
+ */
+ protected byte[] buf;
+ /**
+ * The deflater which is used to deflate the stream.
+ */
+ protected Deflater def;
+
/**
* Deflates everything in the def's input buffers. This will call
* <code>def.deflate()</code> until all bytes from the input buffers
* are processed.
*/
- protected void deflate () throws IOException
+ protected void deflate() throws IOException
{
do
{
* default buffer size.
* @param out the output stream where deflated output should be written.
*/
- public DeflaterOutputStream (OutputStream out)
+ public DeflaterOutputStream(OutputStream out)
{
- this (out, new Deflater (), 512);
+ this(out, new Deflater(), 512);
}
/**
* @param out the output stream where deflated output should be written.
* @param defl the underlying deflater.
*/
- public DeflaterOutputStream (OutputStream out, Deflater defl)
+ public DeflaterOutputStream(OutputStream out, Deflater defl)
{
- this (out, defl, 512);
+ this(out, defl, 512);
}
/**
*/
public DeflaterOutputStream(OutputStream out, Deflater defl, int bufsize)
{
- super (out);
+ super(out);
if (bufsize <= 0)
throw new IllegalArgumentException("bufsize <= 0");
buf = new byte[bufsize];
* was the only way to ensure that all bytes are flushed in Sun's
* JDK.
*/
- public void finish () throws IOException
+ public void finish() throws IOException
{
inbufWrite();
def.finish();
- while (! def.finished ())
+ while (! def.finished())
{
int len = def.deflate(buf, 0, buf.length);
if (len > 0)
}
}
- public void write (int bval) throws IOException
+ /**
+ * Calls finish() and closes the stream.
+ */
+ public void close() throws IOException
+ {
+ finish();
+ out.close();
+ }
+
+ /**
+ * Writes a single byte to the compressed output stream.
+ * @param bval the byte value.
+ */
+ public void write(int bval) throws IOException
{
if (inbuf == null)
inbuf = new byte[128];
else if (inbufLength == inbuf.length)
- inbufWrite ();
+ inbufWrite();
inbuf[inbufLength++] = (byte) bval;
}
- public void write (byte[] buf, int off, int len) throws IOException
+ /**
+ * Writes a len bytes from an array to the compressed stream.
+ * @param buf the byte array.
+ * @param off the offset into the byte array where to start.
+ * @param len the number of bytes to write.
+ */
+ public void write(byte[] buf, int off, int len) throws IOException
{
- inbufWrite ();
- def.setInput (buf, off, len);
- deflate ();
+ inbufWrite();
+ def.setInput(buf, off, len);
+ deflate();
}
- private void inbufWrite () throws IOException
+ private void inbufWrite() throws IOException
{
if (inbufLength > 0)
{
int size = inbufLength;
inbufLength = 0;
- write (inbuf, 0, size);
+ write(inbuf, 0, size);
}
}
private byte[] inbuf;
// Used length of inbuf.
private int inbufLength;
-
- // The retrieval buffer.
- protected byte[] buf;
-
- // Deflater used to compress data.
- protected Deflater def;
}
*/
public static final int GZIP_MAGIC = 0x8b1f;
+ static final int Z_DEFLATED = 8;
+
+ /**
+ * The mask for bit 1 of the flag byte.
+ */
+ static final int HEAD_CRC = 0x02;
+
+ /**
+ * The mask for bit 2 of the flag byte.
+ */
+ static final int EXTRA_FIELD = 0x04;
+
+ /**
+ * The mask for bit 3 of the flag byte.
+ */
+ static final int ORIG_NAME = 0x08;
+
+ /**
+ * The mask for bit 4 of the flag byte.
+ */
+ static final int COMMENT = 0x10;
+
+ /**
+ * The mask for all reserved bits of the flag byte.
+ */
+ static final int RESERVED = 0xe0;
+
+ /**
+ * The CRC-32 checksum value for uncompressed data.
+ */
+ protected CRC32 crc;
+
+ /**
+ * Indicates whether or not the end of the stream has been reached.
+ */
+ protected boolean eos;
+
/**
* Creates a GZIPInputStream with the default buffer size.
*
return (((buf[offset + 3] & 0xFF) << 24) + ((buf[offset + 2] & 0xFF) << 16)
+ ((buf[offset + 1] & 0xFF) << 8) + (buf[offset] & 0xFF));
}
-
- // Checksum used by this input stream.
- protected CRC32 crc;
-
- // Indicates whether end-of-stream has been reached.
- protected boolean eos;
-
- // Some constants from zlib.
- static final int Z_DEFLATED = 8;
- static final int HEAD_CRC = 0x02;
- static final int EXTRA_FIELD = 0x04;
- static final int ORIG_NAME = 0x08;
- static final int COMMENT = 0x10;
- static final int RESERVED = 0xe0;
}
import java.io.OutputStream;
/**
+ * This filter stream is used to compress a stream into a "GZIP" stream.
+ * The "GZIP" format is described in RFC 1952.
+ *
+ * @author John Leuner
* @author Tom Tromey
- * @date May 17, 1999
+ * @since JDK 1.1
*/
/* Written using on-line Java Platform 1.2 API Specification
public class GZIPOutputStream extends DeflaterOutputStream
{
- public void close () throws IOException
- {
- finish ();
- out.close ();
- }
-
- public void finish () throws IOException
- {
- super.finish();
- put4 ((int) crc.getValue());
- put4 (def.getTotalIn());
- }
+ /**
+ * CRC-32 value for uncompressed data
+ */
+ protected CRC32 crc;
- public GZIPOutputStream (OutputStream out) throws IOException
+ /**
+ * Creates a GZIPOutputStream with the default buffer size
+ *
+ * @param out The stream to read data (to be compressed) from
+ *
+ */
+ public GZIPOutputStream(OutputStream out) throws IOException
{
- this (out, 512);
+ this(out, 512);
}
- public GZIPOutputStream (OutputStream out, int readsize) throws IOException
+ /**
+ * Creates a GZIPOutputStream with the specified buffer size
+ *
+ * @param out The stream to read compressed data from
+ * @param size Size of the buffer to use
+ */
+ public GZIPOutputStream(OutputStream out, int size) throws IOException
{
- super (out, new Deflater (Deflater.DEFAULT_COMPRESSION, true), readsize);
-
- put2 (GZIPInputStream.GZIP_MAGIC);
- out.write (GZIPInputStream.Z_DEFLATED);
+ super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
+ crc = new CRC32();
+ put2(GZIPInputStream.GZIP_MAGIC);
+ out.write(GZIPInputStream.Z_DEFLATED);
// No flags for now.
- out.write (0);
+ out.write(0);
// No time either.
- put2 (0);
- put2 (0);
+ put2(0);
+ put2(0);
// No xflags either.
- out.write (0);
+ out.write(0);
// FIXME: unknown OS.
- out.write (255);
-
- crc = new CRC32 ();
+ out.write(255);
}
- public synchronized void write (int bval) throws IOException
+ public synchronized void write(int bval) throws IOException
{
- super.write (bval);
- crc.update (bval);
+ super.write(bval);
+ crc.update(bval);
}
- public synchronized void write (byte[] buf) throws IOException
+ public synchronized void write(byte[] buf) throws IOException
{
- write (buf, 0, buf.length);
+ write(buf, 0, buf.length);
}
- public synchronized void write (byte[] buf, int off, int len)
+ public synchronized void write(byte[] buf, int off, int len)
throws IOException
{
super.write(buf, off, len);
crc.update(buf, off, len);
}
- private final void put2 (int i) throws IOException
+ /**
+ * Writes remaining compressed output data to the output stream
+ * and closes it.
+ */
+ public void close() throws IOException
{
- out.write (i);
- out.write (i >> 8);
+ finish();
+ out.close();
}
- private final void put4 (int i) throws IOException
+ public void finish() throws IOException
{
- out.write (i);
- out.write (i >> 8);
- out.write (i >> 16);
- out.write (i >> 24);
+ super.finish();
+ put4((int) crc.getValue());
+ put4(def.getTotalIn());
}
- // Checksum used by this stream.
- protected CRC32 crc;
+ private final void put2(int i) throws IOException
+ {
+ out.write(i);
+ out.write(i >> 8);
+ }
+
+ private final void put4 (int i) throws IOException
+ {
+ out.write(i);
+ out.write(i >> 8);
+ out.write(i >> 16);
+ out.write(i >> 24);
+ }
}
*/
protected int len;
- protected void fill () throws IOException
- {
- len = in.read(buf, 0, buf.length);
- if (len != -1)
- inf.setInput(buf, 0, len);
- }
/**
* Create an InflaterInputStream with the default decompresseor
*/
public InflaterInputStream(InputStream in)
{
- this (in, new Inflater (), 512);
+ this(in, new Inflater(), 512);
}
/**
*/
public InflaterInputStream(InputStream in, Inflater inf)
{
- this (in, inf, 512);
+ this(in, inf, 512);
}
/**
super(in);
if (in == null)
- throw new NullPointerException ("in may not be null");
+ throw new NullPointerException("in may not be null");
if (inf == null)
- throw new NullPointerException ("inf may not be null");
+ throw new NullPointerException("inf may not be null");
if (size < 0)
- throw new IllegalArgumentException ("size may not be negative");
+ throw new IllegalArgumentException("size may not be negative");
this.inf = inf;
this.buf = new byte [size];
}
- public int read () throws IOException
+ /**
+ * Returns 0 once the end of the stream (EOF) has been reached.
+ * Otherwise returns 1.
+ */
+ public int available() throws IOException
+ {
+ // According to the JDK 1.2 docs, this should only ever return 0
+ // or 1 and should not be relied upon by Java programs.
+ if (inf == null)
+ throw new IOException("stream closed");
+ return inf.finished() ? 0 : 1;
+ }
+
+ /**
+ * Closes the input stream
+ */
+ public synchronized void close() throws IOException
{
- byte[] buf = new byte[1];
- int r = read (buf, 0, 1);
- if (r != -1)
- r = buf[0] & 0xff;
- return r;
+ inf = null;
+ super.close();
}
+ /**
+ * Fills the buffer with more data to decompress.
+ */
+ protected void fill() throws IOException
+ {
+ if (in == null)
+ throw new ZipException ("InflaterInputStream is closed");
+
+ len = in.read(buf, 0, buf.length);
+
+ if (len != -1)
+ inf.setInput(buf, 0, len);
+ }
/**
* Decompresses data into the byte array
public int read(byte[] b, int off, int len) throws IOException
{
if (inf == null)
- throw new IOException ("stream closed");
+ throw new IOException("stream closed");
if (len == 0)
return 0;
if (inf.finished())
while (count == 0)
{
if (inf.needsInput())
- fill ();
+ fill();
try
{
return -1;
}
if (inf.needsDictionary())
- throw new ZipException ("Inflater needs Dictionary");
+ throw new ZipException("Inflater needs Dictionary");
}
}
catch (DataFormatException dfe)
return count;
}
- public void close () throws IOException
- {
- inf = null;
- super.close ();
- }
-
- public int available () throws IOException
- {
- // According to the JDK 1.2 docs, this should only ever return 0
- // or 1 and should not be relied upon by Java programs.
- if (inf == null)
- throw new IOException ("stream closed");
- return inf.finished () ? 0 : 1;
- }
-
/**
* Skip specified number of bytes of uncompressed data
*
public long skip(long n) throws IOException
{
if (inf == null)
- throw new IOException ("stream closed");
+ throw new IOException("stream closed");
+ if (n < 0)
+ throw new IllegalArgumentException();
if (n == 0)
return 0;
- int min = (int) Math.min(n, 1024);
- byte[] buf = new byte[min];
+ int buflen = (int) Math.min(n, 1024);
+ byte[] tmpbuf = new byte[buflen];
- long s = 0;
- while (n > 0)
+ long skipped = 0L;
+ while (n > 0L)
{
- int r = read (buf, 0, min);
- if (r == -1)
+ int numread = read(tmpbuf, 0, buflen);
+ if (numread == -1)
break;
- n -= r;
- s += r;
- min = (int) Math.min(n, 1024);
+ n -= numread;
+ skipped += numread;
+ buflen = (int) Math.min(n, 1024);
}
- return s;
+ return skipped;
}
}