This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
ZipFile speed improvement patch
- From: Mark Wielaard <mark at klomp dot org>
- To: java-patches at gcc dot gnu dot org
- Date: 27 Oct 2002 14:34:10 +0100
- Subject: ZipFile speed improvement patch
Hi,
The following makes opening a new ZipFile a bit more efficient.
RandomAccessFile in libgcj is a bit more efficient then the Classpath
version but I still managed to save a couple of hunderd miliseconds with
an application that opened large numbers of zip and jar files. (The same
test with Classpath+Kissme saved multiple seconds!)
2002-10-27 Mark Wielaard <mark@klomp.org>
* java/util/zip/ZipFile.java (readLeShort): Take and use DataInput as
argument.
(readLeShort): Likewise and use byte[].
(readLeInt): Likewise.
(readEntries): Use new versions of methods and use byte[] for reading
a complete zip entry. Add ZipFile name to exceptions.
(entries): Add ZipFile name to exceptions.
(getEntry): Likewise.
(checkLocalHeader): Use new versions of methods and add ZipFile name
to exceptions.
OK to commit?
Cheers,
Mark
Index: java/util/zip/ZipFile.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/util/zip/ZipFile.java,v
retrieving revision 1.17
diff -u -r1.17 ZipFile.java
--- java/util/zip/ZipFile.java 15 Jun 2002 18:31:13 -0000 1.17
+++ java/util/zip/ZipFile.java 27 Oct 2002 13:09:23 -0000
@@ -1,5 +1,5 @@
/* java.util.zip.ZipFile
- Copyright (C) 2001 Free Software Foundation, Inc.
+ Copyright (C) 2001, 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -36,6 +36,10 @@
exception statement from your version. */
package java.util.zip;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
@@ -133,8 +137,10 @@
* @exception IOException if a i/o error occured.
* @exception EOFException if the file ends prematurely
*/
- private final int readLeShort() throws IOException {
- return raf.readUnsignedByte() | raf.readUnsignedByte() << 8;
+ private final int readLeShort(DataInput di) throws IOException {
+ byte[] b = new byte[2];
+ di.readFully(b);
+ return (b[0] & 0xff) | (b[1] & 0xff) << 8;
}
/**
@@ -142,8 +148,11 @@
* @exception IOException if a i/o error occured.
* @exception EOFException if the file ends prematurely
*/
- private final int readLeInt() throws IOException {
- return readLeShort() | readLeShort() << 16;
+ private final int readLeInt(DataInput di) throws IOException {
+ byte[] b = new byte[4];
+ di.readFully(b);
+ return ((b[0] & 0xff) | (b[1] & 0xff) << 8)
+ | ((b[2] & 0xff) | (b[3] & 0xff) << 8) << 16;
}
/**
@@ -164,36 +173,43 @@
{
if (pos < 0)
throw new ZipException
- ("central directory not found, probably not a zip file");
+ ("central directory not found, probably not a zip file: " + name);
raf.seek(pos--);
}
- while (readLeInt() != ENDSIG);
+ while (readLeInt(raf) != ENDSIG);
if (raf.skipBytes(ENDTOT - ENDNRD) != ENDTOT - ENDNRD)
- throw new EOFException();
- int count = readLeShort();
+ throw new EOFException(name);
+ int count = readLeShort(raf);
if (raf.skipBytes(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ)
- throw new EOFException();
- int centralOffset = readLeInt();
+ throw new EOFException(name);
+ int centralOffset = readLeInt(raf);
entries = new ZipEntry[count];
raf.seek(centralOffset);
+ byte[] ebs = new byte[24];
+ ByteArrayInputStream ebais = new ByteArrayInputStream(ebs);
+ DataInputStream edip = new DataInputStream(ebais);
for (int i = 0; i < count; i++)
{
- if (readLeInt() != CENSIG)
- throw new ZipException("Wrong Central Directory signature");
+ if (readLeInt(raf) != CENSIG)
+ throw new ZipException("Wrong Central Directory signature: " + name);
if (raf.skipBytes(CENHOW - CENVEM) != CENHOW - CENVEM)
- throw new EOFException();
- int method = readLeShort();
- int dostime = readLeInt();
- int crc = readLeInt();
- int csize = readLeInt();
- int size = readLeInt();
- int nameLen = readLeShort();
- int extraLen = readLeShort();
- int commentLen = readLeShort();
+ throw new EOFException(name);
+
+ raf.readFully(ebs);
+ ebais.reset();
+ int method = readLeShort(edip);
+ int dostime = readLeInt(edip);
+ int crc = readLeInt(edip);
+ int csize = readLeInt(edip);
+ int size = readLeInt(edip);
+ int nameLen = readLeShort(edip);
+ int extraLen = readLeShort(edip);
+ int commentLen = readLeShort(edip);
+
if (raf.skipBytes(CENOFF - CENDSK) != CENOFF - CENDSK)
- throw new EOFException();
- int offset = readLeInt();
+ throw new EOFException(name);
+ int offset = readLeInt(raf);
byte[] buffer = new byte[Math.max(nameLen, commentLen)];
@@ -244,7 +260,7 @@
public Enumeration entries()
{
if (entries == null)
- throw new IllegalStateException("ZipFile has closed");
+ throw new IllegalStateException("ZipFile has closed: " + name);
return new ZipEntryEnumeration(entries);
}
@@ -265,7 +281,7 @@
public ZipEntry getEntry(String name)
{
if (entries == null)
- throw new IllegalStateException("ZipFile has closed");
+ throw new IllegalStateException("ZipFile has closed: " + name);
int index = getEntryIndex(name);
return index >= 0 ? (ZipEntry) entries[index].clone() : null;
}
@@ -283,24 +299,24 @@
synchronized (raf)
{
raf.seek(entry.offset);
- if (readLeInt() != LOCSIG)
- throw new ZipException("Wrong Local header signature");
+ if (readLeInt(raf) != LOCSIG)
+ throw new ZipException("Wrong Local header signature: " + name);
/* skip version and flags */
if (raf.skipBytes(LOCHOW - LOCVER) != LOCHOW - LOCVER)
- throw new EOFException();
+ throw new EOFException(name);
- if (entry.getMethod() != readLeShort())
- throw new ZipException("Compression method mismatch");
+ if (entry.getMethod() != readLeShort(raf))
+ throw new ZipException("Compression method mismatch: " + name);
/* Skip time, crc, size and csize */
if (raf.skipBytes(LOCNAM - LOCTIM) != LOCNAM - LOCTIM)
- throw new EOFException();
+ throw new EOFException(name);
- if (entry.getName().length() != readLeShort())
- throw new ZipException("file name length mismatch");
+ if (entry.getName().length() != readLeShort(raf))
+ throw new ZipException("file name length mismatch: " + name);
- int extraLen = entry.getName().length() + readLeShort();
+ int extraLen = entry.getName().length() + readLeShort(raf);
return entry.offset + LOCHDR + extraLen;
}
}