From d642571f32e52d00e0a287c6238dd5d9faeea82d Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Fri, 9 Jul 2004 15:22:19 +0000 Subject: [PATCH] DeflaterOutputStream.java, [...]: Reformatted. 2004-07-09 Michael Koch * 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. From-SVN: r84380 --- libjava/ChangeLog | 9 ++ .../java/util/zip/DeflaterOutputStream.java | 72 +++++++----- libjava/java/util/zip/GZIPInputStream.java | 51 ++++++--- libjava/java/util/zip/GZIPOutputStream.java | 106 +++++++++++------- .../java/util/zip/InflaterInputStream.java | 99 ++++++++-------- 5 files changed, 207 insertions(+), 130 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index d11e67e176f..d78d2734db7 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,12 @@ +2004-07-09 Michael Koch + + * 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 * javax/imageio/IIOException.java, diff --git a/libjava/java/util/zip/DeflaterOutputStream.java b/libjava/java/util/zip/DeflaterOutputStream.java index 6cc3fbf9c23..7ae7d193acd 100644 --- a/libjava/java/util/zip/DeflaterOutputStream.java +++ b/libjava/java/util/zip/DeflaterOutputStream.java @@ -60,18 +60,23 @@ import java.io.IOException; */ 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 * def.deflate() until all bytes from the input buffers * are processed. */ - protected void deflate () throws IOException + protected void deflate() throws IOException { do { @@ -87,9 +92,9 @@ public class DeflaterOutputStream extends FilterOutputStream * 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); } /** @@ -98,9 +103,9 @@ public class DeflaterOutputStream extends FilterOutputStream * @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); } /** @@ -113,7 +118,7 @@ public class DeflaterOutputStream extends FilterOutputStream */ public DeflaterOutputStream(OutputStream out, Deflater defl, int bufsize) { - super (out); + super(out); if (bufsize <= 0) throw new IllegalArgumentException("bufsize <= 0"); buf = new byte[bufsize]; @@ -125,11 +130,11 @@ public class DeflaterOutputStream extends FilterOutputStream * 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) @@ -137,29 +142,48 @@ public class DeflaterOutputStream extends FilterOutputStream } } - 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); } } @@ -167,10 +191,4 @@ public class DeflaterOutputStream extends FilterOutputStream private byte[] inbuf; // Used length of inbuf. private int inbufLength; - - // The retrieval buffer. - protected byte[] buf; - - // Deflater used to compress data. - protected Deflater def; } diff --git a/libjava/java/util/zip/GZIPInputStream.java b/libjava/java/util/zip/GZIPInputStream.java index 6699e56ee69..ec4613f153c 100644 --- a/libjava/java/util/zip/GZIPInputStream.java +++ b/libjava/java/util/zip/GZIPInputStream.java @@ -56,6 +56,43 @@ public class GZIPInputStream */ 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. * @@ -210,18 +247,4 @@ public class GZIPInputStream 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; } diff --git a/libjava/java/util/zip/GZIPOutputStream.java b/libjava/java/util/zip/GZIPOutputStream.java index 939097ed7bb..70339a022d0 100644 --- a/libjava/java/util/zip/GZIPOutputStream.java +++ b/libjava/java/util/zip/GZIPOutputStream.java @@ -41,8 +41,12 @@ import java.io.IOException; 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 @@ -52,75 +56,91 @@ import java.io.OutputStream; 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); + } } diff --git a/libjava/java/util/zip/InflaterInputStream.java b/libjava/java/util/zip/InflaterInputStream.java index 9fac83fc6dc..60442e474af 100644 --- a/libjava/java/util/zip/InflaterInputStream.java +++ b/libjava/java/util/zip/InflaterInputStream.java @@ -70,12 +70,6 @@ public class InflaterInputStream extends FilterInputStream */ 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 @@ -85,7 +79,7 @@ public class InflaterInputStream extends FilterInputStream */ public InflaterInputStream(InputStream in) { - this (in, new Inflater (), 512); + this(in, new Inflater(), 512); } /** @@ -97,7 +91,7 @@ public class InflaterInputStream extends FilterInputStream */ public InflaterInputStream(InputStream in, Inflater inf) { - this (in, inf, 512); + this(in, inf, 512); } /** @@ -113,25 +107,51 @@ public class InflaterInputStream extends FilterInputStream 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 @@ -143,7 +163,7 @@ public class InflaterInputStream extends FilterInputStream 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()) @@ -153,7 +173,7 @@ public class InflaterInputStream extends FilterInputStream while (count == 0) { if (inf.needsInput()) - fill (); + fill(); try { @@ -166,7 +186,7 @@ public class InflaterInputStream extends FilterInputStream return -1; } if (inf.needsDictionary()) - throw new ZipException ("Inflater needs Dictionary"); + throw new ZipException("Inflater needs Dictionary"); } } catch (DataFormatException dfe) @@ -177,21 +197,6 @@ public class InflaterInputStream extends FilterInputStream 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 * @@ -200,25 +205,27 @@ public class InflaterInputStream extends FilterInputStream 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; } } -- 2.30.2