diff -r 6de8252246b5 -r a48961ff3e6b emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java Sun Feb 10 09:51:22 2013 +0100 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java Sun Feb 10 12:14:40 2013 +0100 @@ -28,17 +28,12 @@ * * Portions Copyrighted 2007 Sun Microsystems, Inc. */ -package org.netbeans.modules.java.source.parsing; +package org.apidesign.bck2brwsr.emul.zip; -import java.io.File; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.RandomAccessFile; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; /** @@ -46,78 +41,28 @@ * @author Tomas Zezula */ public final class FastJar { + private final byte[] arr; - private FastJar() { + public FastJar(byte[] arr) { + this.arr = arr; } private static final int GIVE_UP = 1<<16; - - - private static class RandomAccessFileInputStream extends InputStream { - - private final RandomAccessFile b; - private final long len; - - public RandomAccessFileInputStream (RandomAccessFile b) throws IOException { - assert b != null; - this.b = b; - this.len = b.length(); - } - - public RandomAccessFileInputStream (RandomAccessFile b, long len) throws IOException { - assert b != null; - assert len >=0; - this.b = b; - this.len = b.getFilePointer()+len; - } - - public int read (byte[] data, int offset, int size) throws IOException { - int rem = available(); - if (rem == 0) { - return -1; - } - int rlen; - if (size> 25) & 0x7f) + 80), (int)(((dosTime >> 21) & 0x0f) - 1), @@ -127,120 +72,94 @@ (int)((dosTime << 1) & 0x3e)); return d.getTime(); } + */ } - public static InputStream getInputStream (final File file, final Entry e) throws IOException { - return getInputStream(file, e.offset); + public InputStream getInputStream (final Entry e) throws IOException { + return getInputStream(arr, e.offset); } - static InputStream getInputStream (final File file, final long offset) throws IOException { - RandomAccessFile f = new RandomAccessFile (file, "r"); //NOI18N - f.seek (offset); - ZipInputStream in = new ZipInputStream (new RandomAccessFileInputStream (f)); + private static InputStream getInputStream (byte[] arr, final long offset) throws IOException { + ByteArrayInputStream is = new ByteArrayInputStream(arr); + is.skip(offset); + ZipInputStream in = new ZipInputStream (is); ZipEntry e = in.getNextEntry(); if (e != null && e.getCrc() == 0L && e.getMethod() == ZipEntry.STORED) { - long cp = f.getFilePointer(); - in.close(); - f = new RandomAccessFile (file, "r"); //NOI18N - f.seek (cp); - return new RandomAccessFileInputStream (f, e.getSize()); + int cp = arr.length - is.available(); + return new ByteArrayInputStream(arr, cp, (int)e.getSize()); } return in; } - static ZipEntry getZipEntry (final File file, final long offset) throws IOException { - RandomAccessFile f = new RandomAccessFile (file, "r"); //NOI18N - try { - f.seek (offset); - ZipInputStream in = new ZipInputStream (new RandomAccessFileInputStream (f)); - try { - return in.getNextEntry(); - } finally { - in.close(); + public Entry[] list() throws IOException { + final int size = arr.length; + + int at = size - ZipInputStream.ENDHDR; + + byte[] data = new byte[ZipInputStream.ENDHDR]; + int giveup = 0; + + do { + org.apidesign.bck2brwsr.emul.lang.System.arraycopy(arr, at, data, 0, data.length); + at--; + giveup++; + if (giveup > GIVE_UP) { + throw new IOException (); } - } finally { - f.close (); + } while (getsig(data) != ZipInputStream.ENDSIG); + + + final long censize = endsiz(data); + final long cenoff = endoff(data); + at = (int) cenoff; + + Entry[] result = new Entry[0]; + int cenread = 0; + data = new byte[ZipInputStream.CENHDR]; + while (cenread < censize) { + org.apidesign.bck2brwsr.emul.lang.System.arraycopy(arr, at, data, 0, data.length); + at += data.length; + if (getsig(data) != ZipInputStream.CENSIG) { + throw new IOException("No central table"); //NOI18N + } + int cennam = cennam(data); + int cenext = cenext(data); + int cencom = cencom(data); + long lhoff = cenoff(data); + long centim = centim(data); + String name = new String(arr, at, cennam, "UTF-8"); + at += cennam; + int seekby = cenext+cencom; + int cendatalen = ZipInputStream.CENHDR + cennam + seekby; + cenread+=cendatalen; + result = addEntry(result, new Entry(name,lhoff, centim)); + at += seekby; } + return result; } - public static Iterable list(File f) throws IOException { - RandomAccessFile b = new RandomAccessFile (f,"r"); //NOI18N - try { - final long size = (int) b.length(); - b.seek (size-ZipFile.ENDHDR); - - byte[] data = new byte[ZipFile.ENDHDR]; - int giveup = 0; - - do { - if (b.read(data, 0, ZipFile.ENDHDR)!=ZipFile.ENDHDR) { - throw new IOException (); - } - b.seek(b.getFilePointer()-(ZipFile.ENDHDR+1)); - giveup++; - if (giveup > GIVE_UP) { - throw new IOException (); - } - } while (getsig(data) != ZipFile.ENDSIG); - - - final long censize = endsiz(data); - final long cenoff = endoff(data); - b.seek (cenoff); - - List result = new LinkedList(); - int cenread = 0; - data = new byte[ZipFile.CENHDR]; - while (cenread < censize) { - if (b.read(data, 0, ZipFile.CENHDR)!=ZipFile.CENHDR) { - throw new IOException ("No central table"); //NOI18N - } - if (getsig(data) != ZipFile.CENSIG) { - throw new IOException("No central table"); //NOI18N - } - int cennam = cennam(data); - int cenext = cenext(data); - int cencom = cencom(data); - long lhoff = cenoff(data); - long centim = centim(data); - String name = name(b, cennam); - int seekby = cenext+cencom; - int cendatalen = ZipFile.CENHDR + cennam + seekby; - cenread+=cendatalen; - result.add(new Entry(name,lhoff, centim)); - seekBy(b,seekby); - } - return result; - } finally { - b.close(); - } - } - - private static final String name(final RandomAccessFile b, final int cennam) throws IOException { - byte[] name = new byte[cennam]; - b.read(name, 0, cennam); - return new String(name, "UTF-8"); //NOI18N + private Entry[] addEntry(Entry[] result, Entry entry) { + Entry[] e = new Entry[result.length + 1]; + e[result.length] = entry; + org.apidesign.bck2brwsr.emul.lang.System.arraycopy(result, 0, e, 0, result.length); + return e; } private static final long getsig(final byte[] b) throws IOException {return get32(b,0);} - private static final long endsiz(final byte[] b) throws IOException {return get32(b,ZipFile.ENDSIZ);} - private static final long endoff(final byte[] b) throws IOException {return get32(b,ZipFile.ENDOFF);} - private static final long cenlen(final byte[] b) throws IOException {return get32(b,ZipFile.CENLEN);} - private static final long censiz(final byte[] b) throws IOException {return get32(b,ZipFile.CENSIZ);} - private static final long centim(final byte[] b) throws IOException {return get32(b,ZipFile.CENTIM);} - private static final int cennam(final byte[] b) throws IOException {return get16(b,ZipFile.CENNAM);} - private static final int cenext(final byte[] b) throws IOException {return get16(b,ZipFile.CENEXT);} - private static final int cencom(final byte[] b) throws IOException {return get16(b,ZipFile.CENCOM);} - private static final long cenoff (final byte[] b) throws IOException {return get32(b,ZipFile.CENOFF);} - private static final int lochow(final byte[] b) throws IOException {return get16(b,ZipFile.LOCHOW);} - private static final int locname(final byte[] b) throws IOException {return get16(b,ZipFile.LOCNAM);} - private static final int locext(final byte[] b) throws IOException {return get16(b,ZipFile.LOCEXT);} - private static final long locsiz(final byte[] b) throws IOException {return get32(b,ZipFile.LOCSIZ);} + private static final long endsiz(final byte[] b) throws IOException {return get32(b,ZipInputStream.ENDSIZ);} + private static final long endoff(final byte[] b) throws IOException {return get32(b,ZipInputStream.ENDOFF);} + private static final long cenlen(final byte[] b) throws IOException {return get32(b,ZipInputStream.CENLEN);} + private static final long censiz(final byte[] b) throws IOException {return get32(b,ZipInputStream.CENSIZ);} + private static final long centim(final byte[] b) throws IOException {return get32(b,ZipInputStream.CENTIM);} + private static final int cennam(final byte[] b) throws IOException {return get16(b,ZipInputStream.CENNAM);} + private static final int cenext(final byte[] b) throws IOException {return get16(b,ZipInputStream.CENEXT);} + private static final int cencom(final byte[] b) throws IOException {return get16(b,ZipInputStream.CENCOM);} + private static final long cenoff (final byte[] b) throws IOException {return get32(b,ZipInputStream.CENOFF);} + private static final int lochow(final byte[] b) throws IOException {return get16(b,ZipInputStream.LOCHOW);} + private static final int locname(final byte[] b) throws IOException {return get16(b,ZipInputStream.LOCNAM);} + private static final int locext(final byte[] b) throws IOException {return get16(b,ZipInputStream.LOCEXT);} + private static final long locsiz(final byte[] b) throws IOException {return get32(b,ZipInputStream.LOCSIZ);} - private static final void seekBy(final RandomAccessFile b, int offset) throws IOException { - b.seek(b.getFilePointer() + offset); - } - private static final int get16(final byte[] b, int off) throws IOException { final int b1 = b[off]; final int b2 = b[off+1];