From: <chr...@us...> - 2009-03-27 23:38:04
|
Revision: 5162 http://jnode.svn.sourceforge.net/jnode/?rev=5162&view=rev Author: chrisboertien Date: 2009-03-27 23:37:52 +0000 (Fri, 27 Mar 2009) Log Message: ----------- More openjdk java.util.zip, implemented gzip commands, fixed a bug in FileHandleImpl Modified Paths: -------------- trunk/fs/src/fs/org/jnode/fs/service/def/FileHandleImpl.java trunk/shell/descriptors/org.jnode.shell.command.xml Added Paths: ----------- trunk/core/src/openjdk/java/java/util/zip/CheckedInputStream.java trunk/core/src/openjdk/java/java/util/zip/CheckedOutputStream.java trunk/core/src/openjdk/java/java/util/zip/DeflaterOutputStream.java trunk/core/src/openjdk/java/java/util/zip/GZIPInputStream.java trunk/core/src/openjdk/java/java/util/zip/GZIPOutputStream.java trunk/core/src/openjdk/java/java/util/zip/InflaterInputStream.java trunk/shell/src/shell/org/jnode/shell/command/GUNZIPCommand.java trunk/shell/src/shell/org/jnode/shell/command/GZIP.java trunk/shell/src/shell/org/jnode/shell/command/GZIPCommand.java trunk/shell/src/shell/org/jnode/shell/command/ZCATCommand.java Removed Paths: ------------- trunk/core/src/classpath/java/java/util/zip/CheckedInputStream.java trunk/core/src/classpath/java/java/util/zip/CheckedOutputStream.java trunk/core/src/classpath/java/java/util/zip/DeflaterOutputStream.java trunk/core/src/classpath/java/java/util/zip/GZIPInputStream.java trunk/core/src/classpath/java/java/util/zip/GZIPOutputStream.java trunk/core/src/classpath/java/java/util/zip/InflaterInputStream.java Deleted: trunk/core/src/classpath/java/java/util/zip/CheckedInputStream.java =================================================================== --- trunk/core/src/classpath/java/java/util/zip/CheckedInputStream.java 2009-03-27 10:15:56 UTC (rev 5161) +++ trunk/core/src/classpath/java/java/util/zip/CheckedInputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -1,135 +0,0 @@ -/* CheckedInputStream.java - Compute checksum of data being read - Copyright (C) 1999, 2000, 2004 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package java.util.zip; - -import java.io.FilterInputStream; -import java.io.IOException; -import java.io.InputStream; - -/* Written using on-line Java Platform 1.2 API Specification - * and JCL book. - * Believed complete and correct. - */ - -/** - * InputStream that computes a checksum of the data being read using a - * supplied Checksum object. - * - * @see Checksum - * - * @author Tom Tromey - * @date May 17, 1999 - */ -public class CheckedInputStream extends FilterInputStream -{ - /** - * Creates a new CheckInputStream on top of the supplied OutputStream - * using the supplied Checksum. - */ - public CheckedInputStream (InputStream in, Checksum sum) - { - super (in); - this.sum = sum; - } - - /** - * Returns the Checksum object used. To get the data checksum computed so - * far call <code>getChecksum.getValue()</code>. - */ - public Checksum getChecksum () - { - return sum; - } - - /** - * Reads one byte, updates the checksum and returns the read byte - * (or -1 when the end of file was reached). - */ - public int read () throws IOException - { - int x = in.read(); - if (x != -1) - sum.update(x); - return x; - } - - /** - * Reads at most len bytes in the supplied buffer and updates the checksum - * with it. Returns the number of bytes actually read or -1 when the end - * of file was reached. - */ - public int read (byte[] buf, int off, int len) throws IOException - { - int r = in.read(buf, off, len); - if (r != -1) - sum.update(buf, off, r); - return r; - } - - /** - * Skips n bytes by reading them in a temporary buffer and updating the - * the checksum with that buffer. Returns the actual number of bytes skiped - * which can be less then requested when the end of file is reached. - */ - public long skip (long n) throws IOException - { - if (n == 0) - return 0; - - int min = (int) Math.min(n, 1024); - byte[] buf = new byte[min]; - - long s = 0; - while (n > 0) - { - int r = in.read(buf, 0, min); - if (r == -1) - break; - n -= r; - s += r; - min = (int) Math.min(n, 1024); - sum.update(buf, 0, r); - } - - return s; - } - - /** The checksum object. */ - private Checksum sum; -} Deleted: trunk/core/src/classpath/java/java/util/zip/CheckedOutputStream.java =================================================================== --- trunk/core/src/classpath/java/java/util/zip/CheckedOutputStream.java 2009-03-27 10:15:56 UTC (rev 5161) +++ trunk/core/src/classpath/java/java/util/zip/CheckedOutputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -1,100 +0,0 @@ -/* CheckedOutputStream.java - Compute checksum of data being written. - Copyright (C) 1999, 2000 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package java.util.zip; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -/* Written using on-line Java Platform 1.2 API Specification - * and JCL book. - * Believed complete and correct. - */ - -/** - * OutputStream that computes a checksum of data being written using a - * supplied Checksum object. - * - * @see Checksum - * - * @author Tom Tromey - * @date May 17, 1999 - */ -public class CheckedOutputStream extends FilterOutputStream -{ - /** - * Creates a new CheckInputStream on top of the supplied OutputStream - * using the supplied Checksum. - */ - public CheckedOutputStream (OutputStream out, Checksum cksum) - { - super (out); - this.sum = cksum; - } - - /** - * Returns the Checksum object used. To get the data checksum computed so - * far call <code>getChecksum.getValue()</code>. - */ - public Checksum getChecksum () - { - return sum; - } - - /** - * Writes one byte to the OutputStream and updates the Checksum. - */ - public void write (int bval) throws IOException - { - out.write(bval); - sum.update(bval); - } - - /** - * Writes the byte array to the OutputStream and updates the Checksum. - */ - public void write (byte[] buf, int off, int len) throws IOException - { - out.write(buf, off, len); - sum.update(buf, off, len); - } - - /** The checksum object. */ - private Checksum sum; -} Deleted: trunk/core/src/classpath/java/java/util/zip/DeflaterOutputStream.java =================================================================== --- trunk/core/src/classpath/java/java/util/zip/DeflaterOutputStream.java 2009-03-27 10:15:56 UTC (rev 5161) +++ trunk/core/src/classpath/java/java/util/zip/DeflaterOutputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -1,198 +0,0 @@ -/* DeflaterOutputStream.java - Output filter for compressing. - Copyright (C) 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package java.util.zip; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -/* Written using on-line Java Platform 1.2 API Specification - * and JCL book. - * Believed complete and correct. - */ - -/** - * This is a special FilterOutputStream deflating the bytes that are - * written through it. It uses the Deflater for deflating. - * - * A special thing to be noted is that flush() doesn't flush - * everything in Sun's JDK, but it does so in jazzlib. This is because - * Sun's Deflater doesn't have a way to flush() everything, without - * finishing the stream. - * - * @author Tom Tromey, Jochen Hoenicke - * @date Jan 11, 2001 - */ -public class DeflaterOutputStream extends FilterOutputStream -{ - /** - * 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 - { - while (! def.needsInput()) - { - int len = def.deflate(buf, 0, buf.length); - - // System.err.println("DOS deflated " + len + " out of " + buf.length); - if (len <= 0) - break; - out.write(buf, 0, len); - } - - if (! def.needsInput()) - throw new InternalError("Can't deflate all input?"); - } - - /** - * Creates a new DeflaterOutputStream with a default Deflater and - * default buffer size. - * @param out the output stream where deflated output should be written. - */ - public DeflaterOutputStream(OutputStream out) - { - this(out, new Deflater(), 4096); - } - - /** - * Creates a new DeflaterOutputStream with the given Deflater and - * default buffer size. - * @param out the output stream where deflated output should be written. - * @param defl the underlying deflater. - */ - public DeflaterOutputStream(OutputStream out, Deflater defl) - { - this(out, defl, 4096); - } - - /** - * Creates a new DeflaterOutputStream with the given Deflater and - * buffer size. - * @param out the output stream where deflated output should be written. - * @param defl the underlying deflater. - * @param bufsize the buffer size. - * @exception IllegalArgumentException if bufsize isn't positive. - */ - public DeflaterOutputStream(OutputStream out, Deflater defl, int bufsize) - { - super(out); - if (bufsize <= 0) - throw new IllegalArgumentException("bufsize <= 0"); - buf = new byte[bufsize]; - def = defl; - } - - /** - * Flushes the stream by calling flush() on the deflater and then - * on the underlying stream. This ensures that all bytes are - * flushed. This function doesn't work in Sun's JDK, but only in - * jazzlib. - */ - public void flush() throws IOException - { - def.flush(); - deflate(); - out.flush(); - } - - /** - * Finishes the stream by calling finish() on the deflater. This - * was the only way to ensure that all bytes are flushed in Sun's - * JDK. - */ - public void finish() throws IOException - { - def.finish(); - while (! def.finished()) - { - int len = def.deflate(buf, 0, buf.length); - if (len <= 0) - break; - out.write(buf, 0, len); - } - if (! def.finished()) - throw new InternalError("Can't deflate all input?"); - out.flush(); - } - - /** - * 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 - { - byte[] b = new byte[1]; - b[0] = (byte) bval; - write(b, 0, 1); - } - - /** - * 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 - { - def.setInput(buf, off, len); - deflate(); - } -} Deleted: trunk/core/src/classpath/java/java/util/zip/GZIPInputStream.java =================================================================== --- trunk/core/src/classpath/java/java/util/zip/GZIPInputStream.java 2009-03-27 10:15:56 UTC (rev 5161) +++ trunk/core/src/classpath/java/java/util/zip/GZIPInputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -1,355 +0,0 @@ -/* GZIPInputStream.java - Input filter for reading gzip file - Copyright (C) 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package java.util.zip; - -import java.io.EOFException; -import java.io.IOException; -import java.io.InputStream; - -/** - * This filter stream is used to decompress a "GZIP" format stream. - * The "GZIP" format is described in RFC 1952. - * - * @author John Leuner - * @author Tom Tromey - * @since JDK 1.1 - */ -public class GZIPInputStream - extends InflaterInputStream -{ - /** - * The magic number found at the start of a GZIP stream. - */ - public static final int GZIP_MAGIC = 0x8b1f; - - /** - * The mask for bit 0 of the flag byte. - */ - static final int FTEXT = 0x1; - - /** - * The mask for bit 1 of the flag byte. - */ - static final int FHCRC = 0x2; - - /** - * The mask for bit 2 of the flag byte. - */ - static final int FEXTRA = 0x4; - - /** - * The mask for bit 3 of the flag byte. - */ - static final int FNAME = 0x8; - - /** - * The mask for bit 4 of the flag byte. - */ - static final int FCOMMENT = 0x10; - - /** - * 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; - - /** - * Indicates whether or not the GZIP header has been read in. - */ - private boolean readGZIPHeader; - - /** - * Creates a GZIPInputStream with the default buffer size. - * - * @param in The stream to read compressed data from - * (in GZIP format). - * - * @throws IOException if an error occurs during an I/O operation. - */ - public GZIPInputStream(InputStream in) - throws IOException - { - this(in, 4096); - } - - /** - * Creates a GZIPInputStream with the specified buffer size. - * - * @param in The stream to read compressed data from - * (in GZIP format). - * @param size The size of the buffer to use. - * - * @throws IOException if an error occurs during an I/O operation. - * @throws IllegalArgumentException if <code>size</code> - * is less than or equal to 0. - */ - public GZIPInputStream(InputStream in, int size) - throws IOException - { - super(in, new Inflater(true), size); - crc = new CRC32(); - readHeader(); - } - - /** - * Closes the input stream. - * - * @throws IOException if an error occurs during an I/O operation. - */ - public void close() - throws IOException - { - // Nothing to do here. - super.close(); - } - - /** - * Reads in GZIP-compressed data and stores it in uncompressed form - * into an array of bytes. The method will block until either - * enough input data becomes available or the compressed stream - * reaches its end. - * - * @param buf the buffer into which the uncompressed data will - * be stored. - * @param offset the offset indicating where in <code>buf</code> - * the uncompressed data should be placed. - * @param len the number of uncompressed bytes to be read. - */ - public int read(byte[] buf, int offset, int len) throws IOException - { - // We first have to slurp in the GZIP header, then we feed all the - // rest of the data to the superclass. - // - // As we do that we continually update the CRC32. Once the data is - // finished, we check the CRC32. - // - // This means we don't need our own buffer, as everything is done - // in the superclass. - if (!readGZIPHeader) - readHeader(); - - if (eos) - return -1; - - // System.err.println("GZIPIS.read(byte[], off, len ... " + offset + " and len " + len); - - /* We don't have to read the header, - * so we just grab data from the superclass. - */ - int numRead = super.read(buf, offset, len); - if (numRead > 0) - crc.update(buf, offset, numRead); - - if (inf.finished()) - readFooter(); - return numRead; - } - - - /** - * Reads in the GZIP header. - */ - private void readHeader() throws IOException - { - /* 1. Check the two magic bytes */ - CRC32 headCRC = new CRC32(); - int magic = in.read(); - if (magic < 0) - { - eos = true; - return; - } - int magic2 = in.read(); - if ((magic + (magic2 << 8)) != GZIP_MAGIC) - throw new IOException("Error in GZIP header, bad magic code"); - headCRC.update(magic); - headCRC.update(magic2); - - /* 2. Check the compression type (must be 8) */ - int CM = in.read(); - if (CM != Deflater.DEFLATED) - throw new IOException("Error in GZIP header, data not in deflate format"); - headCRC.update(CM); - - /* 3. Check the flags */ - int flags = in.read(); - if (flags < 0) - throw new EOFException("Early EOF in GZIP header"); - headCRC.update(flags); - - /* This flag byte is divided into individual bits as follows: - - bit 0 FTEXT - bit 1 FHCRC - bit 2 FEXTRA - bit 3 FNAME - bit 4 FCOMMENT - bit 5 reserved - bit 6 reserved - bit 7 reserved - */ - - /* 3.1 Check the reserved bits are zero */ - if ((flags & 0xd0) != 0) - throw new IOException("Reserved flag bits in GZIP header != 0"); - - /* 4.-6. Skip the modification time, extra flags, and OS type */ - for (int i=0; i< 6; i++) - { - int readByte = in.read(); - if (readByte < 0) - throw new EOFException("Early EOF in GZIP header"); - headCRC.update(readByte); - } - - /* 7. Read extra field */ - if ((flags & FEXTRA) != 0) - { - /* Skip subfield id */ - for (int i=0; i< 2; i++) - { - int readByte = in.read(); - if (readByte < 0) - throw new EOFException("Early EOF in GZIP header"); - headCRC.update(readByte); - } - if (in.read() < 0 || in.read() < 0) - throw new EOFException("Early EOF in GZIP header"); - - int len1, len2, extraLen; - len1 = in.read(); - len2 = in.read(); - if ((len1 < 0) || (len2 < 0)) - throw new EOFException("Early EOF in GZIP header"); - headCRC.update(len1); - headCRC.update(len2); - - extraLen = (len1 << 8) | len2; - for (int i = 0; i < extraLen;i++) - { - int readByte = in.read(); - if (readByte < 0) - throw new EOFException("Early EOF in GZIP header"); - headCRC.update(readByte); - } - } - - /* 8. Read file name */ - if ((flags & FNAME) != 0) - { - int readByte; - while ( (readByte = in.read()) > 0) - headCRC.update(readByte); - if (readByte < 0) - throw new EOFException("Early EOF in GZIP file name"); - headCRC.update(readByte); - } - - /* 9. Read comment */ - if ((flags & FCOMMENT) != 0) - { - int readByte; - while ( (readByte = in.read()) > 0) - headCRC.update(readByte); - - if (readByte < 0) - throw new EOFException("Early EOF in GZIP comment"); - headCRC.update(readByte); - } - - /* 10. Read header CRC */ - if ((flags & FHCRC) != 0) - { - int tempByte; - int crcval = in.read(); - if (crcval < 0) - throw new EOFException("Early EOF in GZIP header"); - - tempByte = in.read(); - if (tempByte < 0) - throw new EOFException("Early EOF in GZIP header"); - - crcval = (crcval << 8) | tempByte; - if (crcval != ((int) headCRC.getValue() & 0xffff)) - throw new IOException("Header CRC value mismatch"); - } - - readGZIPHeader = true; - //System.err.println("Read GZIP header"); - } - - private void readFooter() throws IOException - { - byte[] footer = new byte[8]; - int avail = inf.getRemaining(); - if (avail > 8) - avail = 8; - System.arraycopy(buf, len - inf.getRemaining(), footer, 0, avail); - int needed = 8 - avail; - while (needed > 0) - { - int count = in.read(footer, 8-needed, needed); - if (count <= 0) - throw new EOFException("Early EOF in GZIP footer"); - needed -= count; //Jewel Jan 16 - } - - int crcval = (footer[0] & 0xff) | ((footer[1] & 0xff) << 8) - | ((footer[2] & 0xff) << 16) | (footer[3] << 24); - if (crcval != (int) crc.getValue()) - throw new IOException("GZIP crc sum mismatch, theirs \"" - + Integer.toHexString(crcval) - + "\" and ours \"" - + Integer.toHexString( (int) crc.getValue())); - - int total = (footer[4] & 0xff) | ((footer[5] & 0xff) << 8) - | ((footer[6] & 0xff) << 16) | (footer[7] << 24); - if (total != inf.getTotalOut()) - throw new IOException("Number of bytes mismatch"); - - /* FIXME" XXX Should we support multiple members. - * Difficult, since there may be some bytes still in buf - */ - eos = true; - } -} Deleted: trunk/core/src/classpath/java/java/util/zip/GZIPOutputStream.java =================================================================== --- trunk/core/src/classpath/java/java/util/zip/GZIPOutputStream.java 2009-03-27 10:15:56 UTC (rev 5161) +++ trunk/core/src/classpath/java/java/util/zip/GZIPOutputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -1,151 +0,0 @@ -/* GZIPOutputStream.java - Create a file in gzip format - Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - -package java.util.zip; - -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 - * @since JDK 1.1 - */ - -/* Written using on-line Java Platform 1.2 API Specification - * and JCL book. - * Believed complete and correct. - */ - -public class GZIPOutputStream extends DeflaterOutputStream -{ - /** - * CRC-32 value for uncompressed data - */ - protected CRC32 crc; - - /** - * 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, 4096); - } - - /** - * 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), size); - crc = new CRC32(); - int mod_time = (int) (System.currentTimeMillis() / 1000L); - byte[] gzipHeader = - { - /* The two magic bytes */ - (byte) GZIPInputStream.GZIP_MAGIC, - (byte) (GZIPInputStream.GZIP_MAGIC >> 8), - - /* The compression type */ - (byte) Deflater.DEFLATED, - - /* The flags (not set) */ - 0, - - /* The modification time */ - (byte) mod_time, (byte) (mod_time >> 8), - (byte) (mod_time >> 16), (byte) (mod_time >> 24), - - /* The extra flags */ - 0, - - /* The OS type (unknown) */ - (byte) 255 - }; - - out.write(gzipHeader); - // System.err.println("wrote GZIP header (" + gzipHeader.length + " bytes )"); - } - - public synchronized void write(byte[] buf, int off, int len) - throws IOException - { - super.write(buf, off, len); - crc.update(buf, off, len); - } - - /** - * Writes remaining compressed output data to the output stream - * and closes it. - */ - public void close() throws IOException - { - finish(); - out.close(); - } - - public void finish() throws IOException - { - super.finish(); - - int totalin = def.getTotalIn(); - int crcval = (int) (crc.getValue() & 0xffffffff); - - // System.err.println("CRC val is " + Integer.toHexString( crcval ) + " and length " + Integer.toHexString(totalin)); - - byte[] gzipFooter = - { - (byte) crcval, (byte) (crcval >> 8), - (byte) (crcval >> 16), (byte) (crcval >> 24), - - (byte) totalin, (byte) (totalin >> 8), - (byte) (totalin >> 16), (byte) (totalin >> 24) - }; - - out.write(gzipFooter); - // System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )"); - } -} Deleted: trunk/core/src/classpath/java/java/util/zip/InflaterInputStream.java =================================================================== --- trunk/core/src/classpath/java/java/util/zip/InflaterInputStream.java 2009-03-27 10:15:56 UTC (rev 5161) +++ trunk/core/src/classpath/java/java/util/zip/InflaterInputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -1,261 +0,0 @@ -/* InflaterInputStream.java - Input stream filter for decompressing - Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 - Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package java.util.zip; - -import java.io.FilterInputStream; -import java.io.IOException; -import java.io.InputStream; - -/** - * This filter stream is used to decompress data compressed in the "deflate" - * format. The "deflate" format is described in RFC 1951. - * - * This stream may form the basis for other decompression filters, such - * as the <code>GZIPInputStream</code>. - * - * @author John Leuner - * @author Tom Tromey - * @since 1.1 - */ -public class InflaterInputStream extends FilterInputStream -{ - /** - * Decompressor for this filter - */ - protected Inflater inf; - - /** - * Byte array used as a buffer - */ - protected byte[] buf; - - /** - * Size of buffer - */ - protected int len; - - // We just use this if we are decoding one byte at a time with the - // read() call. - private byte[] onebytebuffer = new byte[1]; - - /** - * Create an InflaterInputStream with the default decompresseor - * and a default buffer size. - * - * @param in the InputStream to read bytes from - */ - public InflaterInputStream(InputStream in) - { - this(in, new Inflater(), 4096); - } - - /** - * Create an InflaterInputStream with the specified decompresseor - * and a default buffer size. - * - * @param in the InputStream to read bytes from - * @param inf the decompressor used to decompress data read from in - */ - public InflaterInputStream(InputStream in, Inflater inf) - { - this(in, inf, 4096); - } - - /** - * Create an InflaterInputStream with the specified decompresseor - * and a specified buffer size. - * - * @param in the InputStream to read bytes from - * @param inf the decompressor used to decompress data read from in - * @param size size of the buffer to use - */ - public InflaterInputStream(InputStream in, Inflater inf, int size) - { - super(in); - - if (in == null) - throw new NullPointerException("in may not be null"); - if (inf == null) - throw new NullPointerException("inf may not be null"); - if (size < 0) - throw new IllegalArgumentException("size may not be negative"); - - this.inf = inf; - this.buf = new byte [size]; - } - - /** - * 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 - { - if (in != null) - in.close(); - in = null; - } - - /** - * 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 < 0) - throw new ZipException("Deflated stream ends early."); - - inf.setInput(buf, 0, len); - } - - /** - * Reads one byte of decompressed data. - * - * The byte is in the lower 8 bits of the int. - */ - public int read() throws IOException - { - int nread = read(onebytebuffer, 0, 1); - if (nread > 0) - return onebytebuffer[0] & 0xff; - return -1; - } - - /** - * Decompresses data into the byte array - * - * @param b the array to read and decompress data into - * @param off the offset indicating where the data should be placed - * @param len the number of bytes to decompress - */ - public int read(byte[] b, int off, int len) throws IOException - { - if (inf == null) - throw new IOException("stream closed"); - if (len == 0) - return 0; - - int count = 0; - for (;;) - { - - try - { - count = inf.inflate(b, off, len); - } - catch (DataFormatException dfe) - { - throw new ZipException(dfe.getMessage()); - } - - if (count > 0) - return count; - - if (inf.needsDictionary() - | inf.finished()) - return -1; - else if (inf.needsInput()) - fill(); - else - throw new InternalError("Don't know what to do"); - } - } - - /** - * Skip specified number of bytes of uncompressed data - * - * @param n number of bytes to skip - */ - public long skip(long n) throws IOException - { - if (inf == null) - throw new IOException("stream closed"); - if (n < 0) - throw new IllegalArgumentException(); - - if (n == 0) - return 0; - - int buflen = (int) Math.min(n, 2048); - byte[] tmpbuf = new byte[buflen]; - - long skipped = 0L; - while (n > 0L) - { - int numread = read(tmpbuf, 0, buflen); - if (numread <= 0) - break; - n -= numread; - skipped += numread; - buflen = (int) Math.min(n, 2048); - } - - return skipped; - } - - public boolean markSupported() - { - return false; - } - - public void mark(int readLimit) - { - } - - public void reset() throws IOException - { - throw new IOException("reset not supported"); - } -} Added: trunk/core/src/openjdk/java/java/util/zip/CheckedInputStream.java =================================================================== --- trunk/core/src/openjdk/java/java/util/zip/CheckedInputStream.java (rev 0) +++ trunk/core/src/openjdk/java/java/util/zip/CheckedInputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -0,0 +1,116 @@ +/* + * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.util.zip; + +import java.io.FilterInputStream; +import java.io.InputStream; +import java.io.IOException; + +/** + * An input stream that also maintains a checksum of the data being read. + * The checksum can then be used to verify the integrity of the input data. + * + * @see Checksum + * @author David Connelly + */ +public +class CheckedInputStream extends FilterInputStream { + private Checksum cksum; + + /** + * Creates an input stream using the specified Checksum. + * @param in the input stream + * @param cksum the Checksum + */ + public CheckedInputStream(InputStream in, Checksum cksum) { + super(in); + this.cksum = cksum; + } + + /** + * Reads a byte. Will block if no input is available. + * @return the byte read, or -1 if the end of the stream is reached. + * @exception IOException if an I/O error has occurred + */ + public int read() throws IOException { + int b = in.read(); + if (b != -1) { + cksum.update(b); + } + return b; + } + + /** + * Reads into an array of bytes. If <code>len</code> is not zero, the method + * blocks until some input is available; otherwise, no + * bytes are read and <code>0</code> is returned. + * @param buf the buffer into which the data is read + * @param off the start offset in the destination array <code>b</code> + * @param len the maximum number of bytes read + * @return the actual number of bytes read, or -1 if the end + * of the stream is reached. + * @exception NullPointerException If <code>buf</code> is <code>null</code>. + * @exception IndexOutOfBoundsException If <code>off</code> is negative, + * <code>len</code> is negative, or <code>len</code> is greater than + * <code>buf.length - off</code> + * @exception IOException if an I/O error has occurred + */ + public int read(byte[] buf, int off, int len) throws IOException { + len = in.read(buf, off, len); + if (len != -1) { + cksum.update(buf, off, len); + } + return len; + } + + /** + * Skips specified number of bytes of input. + * @param n the number of bytes to skip + * @return the actual number of bytes skipped + * @exception IOException if an I/O error has occurred + */ + public long skip(long n) throws IOException { + byte[] buf = new byte[512]; + long total = 0; + while (total < n) { + long len = n - total; + len = read(buf, 0, len < buf.length ? (int)len : buf.length); + if (len == -1) { + return total; + } + total += len; + } + return total; + } + + /** + * Returns the Checksum for this input stream. + * @return the Checksum value + */ + public Checksum getChecksum() { + return cksum; + } +} Added: trunk/core/src/openjdk/java/java/util/zip/CheckedOutputStream.java =================================================================== --- trunk/core/src/openjdk/java/java/util/zip/CheckedOutputStream.java (rev 0) +++ trunk/core/src/openjdk/java/java/util/zip/CheckedOutputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -0,0 +1,84 @@ +/* + * Copyright 1996-1999 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.util.zip; + +import java.io.FilterOutputStream; +import java.io.OutputStream; +import java.io.IOException; + +/** + * An output stream that also maintains a checksum of the data being + * written. The checksum can then be used to verify the integrity of + * the output data. + * + * @see Checksum + * @author David Connelly + */ +public +class CheckedOutputStream extends FilterOutputStream { + private Checksum cksum; + + /** + * Creates an output stream with the specified Checksum. + * @param out the output stream + * @param cksum the checksum + */ + public CheckedOutputStream(OutputStream out, Checksum cksum) { + super(out); + this.cksum = cksum; + } + + /** + * Writes a byte. Will block until the byte is actually written. + * @param b the byte to be written + * @exception IOException if an I/O error has occurred + */ + public void write(int b) throws IOException { + out.write(b); + cksum.update(b); + } + + /** + * Writes an array of bytes. Will block until the bytes are + * actually written. + * @param b the data to be written + * @param off the start offset of the data + * @param len the number of bytes to be written + * @exception IOException if an I/O error has occurred + */ + public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); + cksum.update(b, off, len); + } + + /** + * Returns the Checksum for this output stream. + * @return the Checksum + */ + public Checksum getChecksum() { + return cksum; + } +} Added: trunk/core/src/openjdk/java/java/util/zip/DeflaterOutputStream.java =================================================================== --- trunk/core/src/openjdk/java/java/util/zip/DeflaterOutputStream.java (rev 0) +++ trunk/core/src/openjdk/java/java/util/zip/DeflaterOutputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -0,0 +1,181 @@ +/* + * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.util.zip; + +import java.io.FilterOutputStream; +import java.io.OutputStream; +import java.io.InputStream; +import java.io.IOException; + +/** + * This class implements an output stream filter for compressing data in + * the "deflate" compression format. It is also used as the basis for other + * types of compression filters, such as GZIPOutputStream. + * + * @see Deflater + * @author David Connelly + */ +public +class DeflaterOutputStream extends FilterOutputStream { + /** + * Compressor for this stream. + */ + protected Deflater def; + + /** + * Output buffer for writing compressed data. + */ + protected byte[] buf; + + /** + * Indicates that the stream has been closed. + */ + + private boolean closed = false; + + /** + * Creates a new output stream with the specified compressor and + * buffer size. + * @param out the output stream + * @param def the compressor ("deflater") + * @param size the output buffer size + * @exception IllegalArgumentException if size is <= 0 + */ + public DeflaterOutputStream(OutputStream out, Deflater def, int size) { + super(out); + if (out == null || def == null) { + throw new NullPointerException(); + } else if (size <= 0) { + throw new IllegalArgumentException("buffer size <= 0"); + } + this.def = def; + buf = new byte[size]; + } + + /** + * Creates a new output stream with the specified compressor and + * a default buffer size. + * @param out the output stream + * @param def the compressor ("deflater") + */ + public DeflaterOutputStream(OutputStream out, Deflater def) { + this(out, def, 512); + } + + boolean usesDefaultDeflater = false; + + /** + * Creates a new output stream with a default compressor and buffer size. + * @param out the output stream + */ + public DeflaterOutputStream(OutputStream out) { + this(out, new Deflater()); + usesDefaultDeflater = true; + } + + /** + * Writes a byte to the compressed output stream. This method will + * block until the byte can be written. + * @param b the byte to be written + * @exception IOException if an I/O error has occurred + */ + public void write(int b) throws IOException { + byte[] buf = new byte[1]; + buf[0] = (byte)(b & 0xff); + write(buf, 0, 1); + } + + /** + * Writes an array of bytes to the compressed output stream. This + * method will block until all the bytes are written. + * @param b the data to be written + * @param off the start offset of the data + * @param len the length of the data + * @exception IOException if an I/O error has occurred + */ + public void write(byte[] b, int off, int len) throws IOException { + if (def.finished()) { + throw new IOException("write beyond end of stream"); + } + if ((off | len | (off + len) | (b.length - (off + len))) < 0) { + throw new IndexOutOfBoundsException(); + } else if (len == 0) { + return; + } + if (!def.finished()) { + // Deflate no more than stride bytes at a time. This avoids + // excess copying in deflateBytes (see Deflater.c) + int stride = buf.length; + for (int i = 0; i < len; i+= stride) { + def.setInput(b, off + i, Math.min(stride, len - i)); + while (!def.needsInput()) { + deflate(); + } + } + } + } + + /** + * Finishes writing compressed data to the output stream without closing + * the underlying stream. Use this method when applying multiple filters + * in succession to the same output stream. + * @exception IOException if an I/O error has occurred + */ + public void finish() throws IOException { + if (!def.finished()) { + def.finish(); + while (!def.finished()) { + deflate(); + } + } + } + + /** + * Writes remaining compressed data to the output stream and closes the + * underlying stream. + * @exception IOException if an I/O error has occurred + */ + public void close() throws IOException { + if (!closed) { + finish(); + if (usesDefaultDeflater) + def.end(); + out.close(); + closed = true; + } + } + + /** + * Writes next block of compressed data to the output stream. + * @throws IOException if an I/O error has occurred + */ + protected void deflate() throws IOException { + int len = def.deflate(buf, 0, buf.length); + if (len > 0) { + out.write(buf, 0, len); + } + } +} Added: trunk/core/src/openjdk/java/java/util/zip/GZIPInputStream.java =================================================================== --- trunk/core/src/openjdk/java/java/util/zip/GZIPInputStream.java (rev 0) +++ trunk/core/src/openjdk/java/java/util/zip/GZIPInputStream.java 2009-03-27 23:37:52 UTC (rev 5162) @@ -0,0 +1,250 @@ +/* + * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.util.zip; + +import java.io.SequenceInputStream; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.IOException; +import java.io.EOFException; + +/** + * This class implements a stream filter for reading compressed data in + * the GZIP file format. + * + * @see InflaterInputStream + * @author David Connelly + * + */ +public +class GZIPInputStream extends InflaterInputStream { + /** + * CRC-32 for uncompressed data. + */ + protected CRC32 crc = new CRC32(); + + /** + * Indicates end of input stream. + */ + protected boolean eos; + + private boolean closed = false; + + /** + * Check to make sure that this stream has not been closed + */ + private void ensureOpen() throws IOException { + if (closed) { + throw new IOException("Stream closed"); + } + } + + /** + * Creates a new input stream with the specified buffer size. + * @param in the input stream + * @param size the input buffer size + * @exception IOException if an I/O error has occurred + * @exception IllegalArgumentException if size is <= 0 + */ + public GZIPInputStream(InputStream in, int size) throws IOException { + super(in, new Inflater(true), size); + //usesDefaultInflater = true; + readHeader(); + crc.reset(); + } + + /** + * Creates a new input stream with a default buffer size. + * @param in the input stream + * @exception IOException if an I/O error has occurred + */ + public GZIPInputStream(InputStream in) throws IOException { + this(in, 512); + } + + /** + * Reads uncompressed data into an array of bytes. If <code>len</code> is not + * zero, the method will block until some input can be decompressed; otherwise, + * no bytes are read and <code>0</code> is returned. + * @param buf the buffer into which the data is read + * @param off the start offset in the destination array <code>b</code> + * @param len the maximum number of bytes read + * @return the actual number of bytes read, or -1 if the end of the + * compressed input stream is reached + * @exception NullPointerException If <code>buf</code> is <code>null</code>. + * @exception IndexOutOfBoundsException If <code>off</code> is negative, + * <code>len</code> is negative, or <code>len</code> is greater than + * <code>buf.length - off</code> + * @exception IOException if an I/O error has occurred or the compressed + * input data is corrupt + */ + public int read(byte[] buf, int off, int len) throws IOException { + ensureOpen(); + if (eos) { + return -1; + } + len = super.read(buf, off, len); + if (len == -1) { + readTrailer(); + eos = true; + } else { + crc.update(buf, off, len); + } + return len; + } + + /** + * Closes this input stream and releases any system resources associated + * with the stream. + * @exception IOException if an I/O error has occurred + */ + public void close() throws IOException { + if (!closed) { + super.close(); + eos = true; + closed = true; + } + } + + /** + * GZIP header magic number. + */ + public final static int GZIP_MAGIC = 0x8b1f; + + /* + * File header flags. + */ + private final static int FTEXT = 1; // Extra text + private final static int FHCRC = 2; // Header CRC + private final static int FEXTRA = 4; // Extra field + private final static int FNAME = 8; // File name + private final static int FCOMMENT = 16; // File comment + + /* + * Reads GZIP member header. + */ + private void readHeader() throws IOException { + CheckedInputStream in = new CheckedInputStream(this.in, crc); + crc.reset(); + // Check header magic + if (readUShort(in) != GZIP_MAGIC) { + throw new IOException("Not in GZIP format"); + } + // Check compression method + if (readUByte(in) != 8) { + throw new IOException("Unsupported compression method"); + } + // Read flags + int flg = readUByte(in); + // Skip MTIME, XFL, and OS fields + skipBytes(in, 6); + // Skip optional extra field + if ((flg & FEXTRA) == FEXTRA) { + skipBytes(in, readUShort(in)); + } + // Skip optional file name + if ((flg & FNAME) == FNAME) { + while (readUByte(in) != 0) ; + } + // Skip optional file comment + if ((flg & FCOMMENT) == FCOMMENT) { + while (readUByte(in) != 0) ; + } + // Check optional header CRC + if ((flg & FHCRC) == FHCRC) { + int v = (int)crc.getValue() & 0xffff; + if (readUShort(in) != v) { + throw new IOException("Corrupt GZIP header"); + } + } + } + + /* + * Reads GZIP member trailer. + */ + private void readTrailer() throws IOException { + InputStream in = this.in; + int n = inf.getRemaining(); + if (n > 0) { + in = new SequenceInputStream( + new ByteArray... [truncated message content] |