rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java
changeset 772 d382dacfd73f
parent 706 a48961ff3e6b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,175 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + *
    1.30 + * Contributor(s):
    1.31 + *
    1.32 + * Portions Copyrighted 2007 Sun Microsystems, Inc.
    1.33 + */
    1.34 +package org.apidesign.bck2brwsr.emul.zip;
    1.35 +
    1.36 +import java.io.ByteArrayInputStream;
    1.37 +import java.io.IOException;
    1.38 +import java.io.InputStream;
    1.39 +import java.util.zip.ZipEntry;
    1.40 +import java.util.zip.ZipInputStream;
    1.41 +
    1.42 +/**
    1.43 + *
    1.44 + * @author Tomas Zezula
    1.45 + */
    1.46 +public final class FastJar {
    1.47 +    private final byte[] arr;
    1.48 +
    1.49 +    public FastJar(byte[] arr) {
    1.50 +        this.arr = arr;
    1.51 +    }
    1.52 +    
    1.53 +    
    1.54 +    private static final int GIVE_UP = 1<<16;
    1.55 +
    1.56 +    public static final  class Entry {
    1.57 +        
    1.58 +        public final String name;
    1.59 +        final long offset;
    1.60 +        private final long dosTime;
    1.61 +        
    1.62 +        Entry (String name, long offset, long time) {
    1.63 +            assert name != null;
    1.64 +            this.name = name;
    1.65 +            this.offset = offset;
    1.66 +            this.dosTime = time;
    1.67 +        }        
    1.68 +/*        
    1.69 +        public long getTime () {
    1.70 +            Date d = new Date((int)(((dosTime >> 25) & 0x7f) + 80),
    1.71 +                    (int)(((dosTime >> 21) & 0x0f) - 1),
    1.72 +                    (int)((dosTime >> 16) & 0x1f),
    1.73 +                    (int)((dosTime >> 11) & 0x1f),
    1.74 +                    (int)((dosTime >> 5) & 0x3f),
    1.75 +                    (int)((dosTime << 1) & 0x3e));
    1.76 +            return d.getTime();
    1.77 +        }
    1.78 +        */
    1.79 +    }
    1.80 +    
    1.81 +    public InputStream getInputStream (final Entry e) throws IOException {
    1.82 +        return getInputStream(arr, e.offset);
    1.83 +    }
    1.84 +    
    1.85 +    private static InputStream getInputStream (byte[] arr, final long offset) throws IOException {
    1.86 +        ByteArrayInputStream is = new ByteArrayInputStream(arr);
    1.87 +        is.skip(offset);
    1.88 +        ZipInputStream in = new ZipInputStream (is);
    1.89 +        ZipEntry e = in.getNextEntry();
    1.90 +        if (e != null && e.getCrc() == 0L && e.getMethod() == ZipEntry.STORED) {
    1.91 +            int cp = arr.length - is.available();
    1.92 +            return new ByteArrayInputStream(arr, cp, (int)e.getSize());
    1.93 +        }
    1.94 +        return in;
    1.95 +    }
    1.96 +    
    1.97 +    public Entry[] list() throws IOException {
    1.98 +        final int size = arr.length;
    1.99 +
   1.100 +        int at = size - ZipInputStream.ENDHDR;
   1.101 +
   1.102 +        byte[] data = new byte[ZipInputStream.ENDHDR];        
   1.103 +        int giveup = 0;
   1.104 +
   1.105 +        do {
   1.106 +            org.apidesign.bck2brwsr.emul.lang.System.arraycopy(arr, at, data, 0, data.length);
   1.107 +            at--;
   1.108 +            giveup++;
   1.109 +            if (giveup > GIVE_UP) {
   1.110 +                throw new IOException ();
   1.111 +            }
   1.112 +        } while (getsig(data) != ZipInputStream.ENDSIG);
   1.113 +
   1.114 +
   1.115 +        final long censize = endsiz(data);
   1.116 +        final long cenoff  = endoff(data);
   1.117 +        at = (int) cenoff;                                                     
   1.118 +
   1.119 +        Entry[] result = new Entry[0];
   1.120 +        int cenread = 0;
   1.121 +        data = new byte[ZipInputStream.CENHDR];
   1.122 +        while (cenread < censize) {
   1.123 +            org.apidesign.bck2brwsr.emul.lang.System.arraycopy(arr, at, data, 0, data.length);
   1.124 +            at += data.length;
   1.125 +            if (getsig(data) != ZipInputStream.CENSIG) {
   1.126 +                throw new IOException("No central table");          //NOI18N
   1.127 +            }
   1.128 +            int cennam = cennam(data);
   1.129 +            int cenext = cenext(data);
   1.130 +            int cencom = cencom(data);
   1.131 +            long lhoff = cenoff(data);
   1.132 +            long centim = centim(data);
   1.133 +            String name = new String(arr, at, cennam, "UTF-8");
   1.134 +            at += cennam;
   1.135 +            int seekby = cenext+cencom;
   1.136 +            int cendatalen = ZipInputStream.CENHDR + cennam + seekby;
   1.137 +            cenread+=cendatalen;
   1.138 +            result = addEntry(result, new Entry(name,lhoff, centim));
   1.139 +            at += seekby;
   1.140 +        }
   1.141 +        return result;
   1.142 +    }
   1.143 +
   1.144 +    private Entry[] addEntry(Entry[] result, Entry entry) {
   1.145 +        Entry[] e = new Entry[result.length + 1];
   1.146 +        e[result.length] = entry;
   1.147 +        org.apidesign.bck2brwsr.emul.lang.System.arraycopy(result, 0, e, 0, result.length);
   1.148 +        return e;
   1.149 +    }
   1.150 +
   1.151 +    private static final long getsig(final byte[] b) throws IOException {return get32(b,0);}
   1.152 +    private static final long endsiz(final byte[] b) throws IOException {return get32(b,ZipInputStream.ENDSIZ);}
   1.153 +    private static final long endoff(final byte[] b) throws IOException {return get32(b,ZipInputStream.ENDOFF);}
   1.154 +    private static final long  cenlen(final byte[] b) throws IOException {return get32(b,ZipInputStream.CENLEN);}
   1.155 +    private static final long  censiz(final byte[] b) throws IOException {return get32(b,ZipInputStream.CENSIZ);}
   1.156 +    private static final long centim(final byte[] b) throws IOException {return get32(b,ZipInputStream.CENTIM);}
   1.157 +    private static final int  cennam(final byte[] b) throws IOException {return get16(b,ZipInputStream.CENNAM);}
   1.158 +    private static final int  cenext(final byte[] b) throws IOException {return get16(b,ZipInputStream.CENEXT);}
   1.159 +    private static final int  cencom(final byte[] b) throws IOException {return get16(b,ZipInputStream.CENCOM);}
   1.160 +    private static final long cenoff (final byte[] b) throws IOException {return get32(b,ZipInputStream.CENOFF);}
   1.161 +    private static final int lochow(final byte[] b) throws IOException {return get16(b,ZipInputStream.LOCHOW);}
   1.162 +    private static final int locname(final byte[] b) throws IOException {return get16(b,ZipInputStream.LOCNAM);}
   1.163 +    private static final int locext(final byte[] b) throws IOException {return get16(b,ZipInputStream.LOCEXT);}
   1.164 +    private static final long locsiz(final byte[] b) throws IOException {return get32(b,ZipInputStream.LOCSIZ);}
   1.165 +    
   1.166 +    private static final int get16(final byte[] b, int off) throws IOException {        
   1.167 +        final int b1 = b[off];
   1.168 +	final int b2 = b[off+1];
   1.169 +        return (b1 & 0xff) | ((b2 & 0xff) << 8);
   1.170 +    }
   1.171 +
   1.172 +    private static final long get32(final byte[] b, int off) throws IOException {
   1.173 +	final int s1 = get16(b, off);
   1.174 +	final int s2 = get16(b, off+2);
   1.175 +        return s1 | ((long)s2 << 16);
   1.176 +    }
   1.177 +
   1.178 +}