Commit d642571f by Michael Koch Committed by Michael Koch

DeflaterOutputStream.java, [...]: Reformatted.

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.

From-SVN: r84380
parent fbc40a17
2004-07-09 Michael Koch <konqueror@gmx.de> 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, * javax/imageio/IIOException.java,
javax/imageio/event/IIOReadProgressListener.java, javax/imageio/event/IIOReadProgressListener.java,
javax/imageio/event/IIOReadUpdateListener.java, javax/imageio/event/IIOReadUpdateListener.java,
......
...@@ -60,18 +60,23 @@ import java.io.IOException; ...@@ -60,18 +60,23 @@ import java.io.IOException;
*/ */
public class DeflaterOutputStream extends FilterOutputStream public class DeflaterOutputStream extends FilterOutputStream
{ {
public void close () throws IOException /**
{ * This buffer is used temporarily to retrieve the bytes from the
finish (); * deflater and write them to the underlying output stream.
out.close(); */
} 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 * Deflates everything in the def's input buffers. This will call
* <code>def.deflate()</code> until all bytes from the input buffers * <code>def.deflate()</code> until all bytes from the input buffers
* are processed. * are processed.
*/ */
protected void deflate () throws IOException protected void deflate() throws IOException
{ {
do do
{ {
...@@ -87,9 +92,9 @@ public class DeflaterOutputStream extends FilterOutputStream ...@@ -87,9 +92,9 @@ public class DeflaterOutputStream extends FilterOutputStream
* default buffer size. * default buffer size.
* @param out the output stream where deflated output should be written. * @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 ...@@ -98,9 +103,9 @@ public class DeflaterOutputStream extends FilterOutputStream
* @param out the output stream where deflated output should be written. * @param out the output stream where deflated output should be written.
* @param defl the underlying deflater. * @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 ...@@ -113,7 +118,7 @@ public class DeflaterOutputStream extends FilterOutputStream
*/ */
public DeflaterOutputStream(OutputStream out, Deflater defl, int bufsize) public DeflaterOutputStream(OutputStream out, Deflater defl, int bufsize)
{ {
super (out); super(out);
if (bufsize <= 0) if (bufsize <= 0)
throw new IllegalArgumentException("bufsize <= 0"); throw new IllegalArgumentException("bufsize <= 0");
buf = new byte[bufsize]; buf = new byte[bufsize];
...@@ -125,11 +130,11 @@ public class DeflaterOutputStream extends FilterOutputStream ...@@ -125,11 +130,11 @@ public class DeflaterOutputStream extends FilterOutputStream
* was the only way to ensure that all bytes are flushed in Sun's * was the only way to ensure that all bytes are flushed in Sun's
* JDK. * JDK.
*/ */
public void finish () throws IOException public void finish() throws IOException
{ {
inbufWrite(); inbufWrite();
def.finish(); def.finish();
while (! def.finished ()) while (! def.finished())
{ {
int len = def.deflate(buf, 0, buf.length); int len = def.deflate(buf, 0, buf.length);
if (len > 0) if (len > 0)
...@@ -137,29 +142,48 @@ public class DeflaterOutputStream extends FilterOutputStream ...@@ -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) if (inbuf == null)
inbuf = new byte[128]; inbuf = new byte[128];
else if (inbufLength == inbuf.length) else if (inbufLength == inbuf.length)
inbufWrite (); inbufWrite();
inbuf[inbufLength++] = (byte) bval; 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 (); inbufWrite();
def.setInput (buf, off, len); def.setInput(buf, off, len);
deflate (); deflate();
} }
private void inbufWrite () throws IOException private void inbufWrite() throws IOException
{ {
if (inbufLength > 0) if (inbufLength > 0)
{ {
int size = inbufLength; int size = inbufLength;
inbufLength = 0; inbufLength = 0;
write (inbuf, 0, size); write(inbuf, 0, size);
} }
} }
...@@ -167,10 +191,4 @@ public class DeflaterOutputStream extends FilterOutputStream ...@@ -167,10 +191,4 @@ public class DeflaterOutputStream extends FilterOutputStream
private byte[] inbuf; private byte[] inbuf;
// Used length of inbuf. // Used length of inbuf.
private int inbufLength; private int inbufLength;
// The retrieval buffer.
protected byte[] buf;
// Deflater used to compress data.
protected Deflater def;
} }
...@@ -56,6 +56,43 @@ public class GZIPInputStream ...@@ -56,6 +56,43 @@ public class GZIPInputStream
*/ */
public static final int GZIP_MAGIC = 0x8b1f; 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. * Creates a GZIPInputStream with the default buffer size.
* *
...@@ -210,18 +247,4 @@ public class GZIPInputStream ...@@ -210,18 +247,4 @@ public class GZIPInputStream
return (((buf[offset + 3] & 0xFF) << 24) + ((buf[offset + 2] & 0xFF) << 16) return (((buf[offset + 3] & 0xFF) << 24) + ((buf[offset + 2] & 0xFF) << 16)
+ ((buf[offset + 1] & 0xFF) << 8) + (buf[offset] & 0xFF)); + ((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;
} }
...@@ -41,8 +41,12 @@ import java.io.IOException; ...@@ -41,8 +41,12 @@ import java.io.IOException;
import java.io.OutputStream; 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 * @author Tom Tromey
* @date May 17, 1999 * @since JDK 1.1
*/ */
/* Written using on-line Java Platform 1.2 API Specification /* Written using on-line Java Platform 1.2 API Specification
...@@ -52,75 +56,91 @@ import java.io.OutputStream; ...@@ -52,75 +56,91 @@ import java.io.OutputStream;
public class GZIPOutputStream extends DeflaterOutputStream public class GZIPOutputStream extends DeflaterOutputStream
{ {
public void close () throws IOException /**
{ * CRC-32 value for uncompressed data
finish (); */
out.close (); protected CRC32 crc;
}
public void finish () throws IOException
{
super.finish();
put4 ((int) crc.getValue());
put4 (def.getTotalIn());
}
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); super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
crc = new CRC32();
put2 (GZIPInputStream.GZIP_MAGIC); put2(GZIPInputStream.GZIP_MAGIC);
out.write (GZIPInputStream.Z_DEFLATED); out.write(GZIPInputStream.Z_DEFLATED);
// No flags for now. // No flags for now.
out.write (0); out.write(0);
// No time either. // No time either.
put2 (0); put2(0);
put2 (0); put2(0);
// No xflags either. // No xflags either.
out.write (0); out.write(0);
// FIXME: unknown OS. // FIXME: unknown OS.
out.write (255); out.write(255);
crc = new CRC32 ();
} }
public synchronized void write (int bval) throws IOException public synchronized void write(int bval) throws IOException
{ {
super.write (bval); super.write(bval);
crc.update (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 throws IOException
{ {
super.write(buf, off, len); super.write(buf, off, len);
crc.update(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); finish();
out.write (i >> 8); out.close();
} }
private final void put4 (int i) throws IOException public void finish() throws IOException
{ {
out.write (i); super.finish();
out.write (i >> 8); put4((int) crc.getValue());
out.write (i >> 16); put4(def.getTotalIn());
out.write (i >> 24);
} }
// Checksum used by this stream. private final void put2(int i) throws IOException
protected CRC32 crc; {
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);
}
} }
...@@ -70,12 +70,6 @@ public class InflaterInputStream extends FilterInputStream ...@@ -70,12 +70,6 @@ public class InflaterInputStream extends FilterInputStream
*/ */
protected int len; 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 * Create an InflaterInputStream with the default decompresseor
...@@ -85,7 +79,7 @@ public class InflaterInputStream extends FilterInputStream ...@@ -85,7 +79,7 @@ public class InflaterInputStream extends FilterInputStream
*/ */
public InflaterInputStream(InputStream in) public InflaterInputStream(InputStream in)
{ {
this (in, new Inflater (), 512); this(in, new Inflater(), 512);
} }
/** /**
...@@ -97,7 +91,7 @@ public class InflaterInputStream extends FilterInputStream ...@@ -97,7 +91,7 @@ public class InflaterInputStream extends FilterInputStream
*/ */
public InflaterInputStream(InputStream in, Inflater inf) public InflaterInputStream(InputStream in, Inflater inf)
{ {
this (in, inf, 512); this(in, inf, 512);
} }
/** /**
...@@ -113,25 +107,51 @@ public class InflaterInputStream extends FilterInputStream ...@@ -113,25 +107,51 @@ public class InflaterInputStream extends FilterInputStream
super(in); super(in);
if (in == null) if (in == null)
throw new NullPointerException ("in may not be null"); throw new NullPointerException("in may not be null");
if (inf == null) if (inf == null)
throw new NullPointerException ("inf may not be null"); throw new NullPointerException("inf may not be null");
if (size < 0) if (size < 0)
throw new IllegalArgumentException ("size may not be negative"); throw new IllegalArgumentException("size may not be negative");
this.inf = inf; this.inf = inf;
this.buf = new byte [size]; 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]; inf = null;
int r = read (buf, 0, 1); super.close();
if (r != -1)
r = buf[0] & 0xff;
return r;
} }
/**
* 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 * Decompresses data into the byte array
...@@ -143,7 +163,7 @@ public class InflaterInputStream extends FilterInputStream ...@@ -143,7 +163,7 @@ public class InflaterInputStream extends FilterInputStream
public int read(byte[] b, int off, int len) throws IOException public int read(byte[] b, int off, int len) throws IOException
{ {
if (inf == null) if (inf == null)
throw new IOException ("stream closed"); throw new IOException("stream closed");
if (len == 0) if (len == 0)
return 0; return 0;
if (inf.finished()) if (inf.finished())
...@@ -153,7 +173,7 @@ public class InflaterInputStream extends FilterInputStream ...@@ -153,7 +173,7 @@ public class InflaterInputStream extends FilterInputStream
while (count == 0) while (count == 0)
{ {
if (inf.needsInput()) if (inf.needsInput())
fill (); fill();
try try
{ {
...@@ -166,7 +186,7 @@ public class InflaterInputStream extends FilterInputStream ...@@ -166,7 +186,7 @@ public class InflaterInputStream extends FilterInputStream
return -1; return -1;
} }
if (inf.needsDictionary()) if (inf.needsDictionary())
throw new ZipException ("Inflater needs Dictionary"); throw new ZipException("Inflater needs Dictionary");
} }
} }
catch (DataFormatException dfe) catch (DataFormatException dfe)
...@@ -177,21 +197,6 @@ public class InflaterInputStream extends FilterInputStream ...@@ -177,21 +197,6 @@ public class InflaterInputStream extends FilterInputStream
return count; 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 * Skip specified number of bytes of uncompressed data
* *
...@@ -200,25 +205,27 @@ public class InflaterInputStream extends FilterInputStream ...@@ -200,25 +205,27 @@ public class InflaterInputStream extends FilterInputStream
public long skip(long n) throws IOException public long skip(long n) throws IOException
{ {
if (inf == null) if (inf == null)
throw new IOException ("stream closed"); throw new IOException("stream closed");
if (n < 0)
throw new IllegalArgumentException();
if (n == 0) if (n == 0)
return 0; return 0;
int min = (int) Math.min(n, 1024); int buflen = (int) Math.min(n, 1024);
byte[] buf = new byte[min]; byte[] tmpbuf = new byte[buflen];
long s = 0; long skipped = 0L;
while (n > 0) while (n > 0L)
{ {
int r = read (buf, 0, min); int numread = read(tmpbuf, 0, buflen);
if (r == -1) if (numread == -1)
break; break;
n -= r; n -= numread;
s += r; skipped += numread;
min = (int) Math.min(n, 1024); buflen = (int) Math.min(n, 1024);
} }
return s; return skipped;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment