rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/ManifestInputStream.java
changeset 772 d382dacfd73f
parent 672 add357fd6c5c
child 1943 4b4f9dbc807b
     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/lang/ManifestInputStream.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,228 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package org.apidesign.bck2brwsr.emul.lang;
    1.29 +
    1.30 +import java.io.FilterInputStream;
    1.31 +import java.io.IOException;
    1.32 +import java.io.InputStream;
    1.33 +
    1.34 +/*
    1.35 + * A fast buffered input stream for parsing manifest files.
    1.36 + * 
    1.37 + * Taken from java.util.jar.Manifest.FastInputStream and modified to be
    1.38 + * independent of other Manifest functionality.
    1.39 + */
    1.40 +public abstract class ManifestInputStream extends FilterInputStream {
    1.41 +    private byte[] buf;
    1.42 +    private int count = 0;
    1.43 +    private int pos = 0;
    1.44 +
    1.45 +    protected ManifestInputStream(InputStream in) {
    1.46 +        this(in, 8192);
    1.47 +    }
    1.48 +
    1.49 +    protected ManifestInputStream(InputStream in, int size) {
    1.50 +        super(in);
    1.51 +        buf = new byte[size];
    1.52 +    }
    1.53 +
    1.54 +    public int read() throws IOException {
    1.55 +        if (pos >= count) {
    1.56 +            fill();
    1.57 +            if (pos >= count) {
    1.58 +                return -1;
    1.59 +            }
    1.60 +        }
    1.61 +        return buf[pos++] & 0xff;
    1.62 +    }
    1.63 +
    1.64 +    public int read(byte[] b, int off, int len) throws IOException {
    1.65 +        int avail = count - pos;
    1.66 +        if (avail <= 0) {
    1.67 +            if (len >= buf.length) {
    1.68 +                return in.read(b, off, len);
    1.69 +            }
    1.70 +            fill();
    1.71 +            avail = count - pos;
    1.72 +            if (avail <= 0) {
    1.73 +                return -1;
    1.74 +            }
    1.75 +        }
    1.76 +        if (len > avail) {
    1.77 +            len = avail;
    1.78 +        }
    1.79 +        System.arraycopy(buf, pos, b, off, len);
    1.80 +        pos += len;
    1.81 +        return len;
    1.82 +    }
    1.83 +
    1.84 +    /*
    1.85 +     * Reads 'len' bytes from the input stream, or until an end-of-line
    1.86 +     * is reached. Returns the number of bytes read.
    1.87 +     */
    1.88 +    public int readLine(byte[] b, int off, int len) throws IOException {
    1.89 +        byte[] tbuf = this.buf;
    1.90 +        int total = 0;
    1.91 +        while (total < len) {
    1.92 +            int avail = count - pos;
    1.93 +            if (avail <= 0) {
    1.94 +                fill();
    1.95 +                avail = count - pos;
    1.96 +                if (avail <= 0) {
    1.97 +                    return -1;
    1.98 +                }
    1.99 +            }
   1.100 +            int n = len - total;
   1.101 +            if (n > avail) {
   1.102 +                n = avail;
   1.103 +            }
   1.104 +            int tpos = pos;
   1.105 +            int maxpos = tpos + n;
   1.106 +            while (tpos < maxpos && tbuf[tpos++] != '\n') {
   1.107 +                ;
   1.108 +            }
   1.109 +            n = tpos - pos;
   1.110 +            System.arraycopy(tbuf, pos, b, off, n);
   1.111 +            off += n;
   1.112 +            total += n;
   1.113 +            pos = tpos;
   1.114 +            if (tbuf[tpos - 1] == '\n') {
   1.115 +                break;
   1.116 +            }
   1.117 +        }
   1.118 +        return total;
   1.119 +    }
   1.120 +
   1.121 +    public byte peek() throws IOException {
   1.122 +        if (pos == count) {
   1.123 +            fill();
   1.124 +        }
   1.125 +        if (pos == count) {
   1.126 +            return -1; // nothing left in buffer
   1.127 +        }
   1.128 +        return buf[pos];
   1.129 +    }
   1.130 +
   1.131 +    public int readLine(byte[] b) throws IOException {
   1.132 +        return readLine(b, 0, b.length);
   1.133 +    }
   1.134 +
   1.135 +    public long skip(long n) throws IOException {
   1.136 +        if (n <= 0) {
   1.137 +            return 0;
   1.138 +        }
   1.139 +        long avail = count - pos;
   1.140 +        if (avail <= 0) {
   1.141 +            return in.skip(n);
   1.142 +        }
   1.143 +        if (n > avail) {
   1.144 +            n = avail;
   1.145 +        }
   1.146 +        pos += n;
   1.147 +        return n;
   1.148 +    }
   1.149 +
   1.150 +    public int available() throws IOException {
   1.151 +        return (count - pos) + in.available();
   1.152 +    }
   1.153 +
   1.154 +    public void close() throws IOException {
   1.155 +        if (in != null) {
   1.156 +            in.close();
   1.157 +            in = null;
   1.158 +            buf = null;
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    private void fill() throws IOException {
   1.163 +        count = pos = 0;
   1.164 +        int n = in.read(buf, 0, buf.length);
   1.165 +        if (n > 0) {
   1.166 +            count = n;
   1.167 +        }
   1.168 +    }
   1.169 +    
   1.170 +    protected abstract String putValue(String key, String value);
   1.171 +
   1.172 +    public void readAttributes(byte[] lbuf) throws IOException {
   1.173 +        ManifestInputStream is = this;
   1.174 +
   1.175 +        String name = null;
   1.176 +        String value = null;
   1.177 +        byte[] lastline = null;
   1.178 +        int len;
   1.179 +        while ((len = is.readLine(lbuf)) != -1) {
   1.180 +            boolean lineContinued = false;
   1.181 +            if (lbuf[--len] != '\n') {
   1.182 +                throw new IOException("line too long");
   1.183 +            }
   1.184 +            if (len > 0 && lbuf[len - 1] == '\r') {
   1.185 +                --len;
   1.186 +            }
   1.187 +            if (len == 0) {
   1.188 +                break;
   1.189 +            }
   1.190 +            int i = 0;
   1.191 +            if (lbuf[0] == ' ') {
   1.192 +                if (name == null) {
   1.193 +                    throw new IOException("misplaced continuation line");
   1.194 +                }
   1.195 +                lineContinued = true;
   1.196 +                byte[] buf = new byte[lastline.length + len - 1];
   1.197 +                System.arraycopy(lastline, 0, buf, 0, lastline.length);
   1.198 +                System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
   1.199 +                if (is.peek() == ' ') {
   1.200 +                    lastline = buf;
   1.201 +                    continue;
   1.202 +                }
   1.203 +                value = new String(buf, 0, buf.length, "UTF8");
   1.204 +                lastline = null;
   1.205 +            } else {
   1.206 +                while (lbuf[i++] != ':') {
   1.207 +                    if (i >= len) {
   1.208 +                        throw new IOException("invalid header field");
   1.209 +                    }
   1.210 +                }
   1.211 +                if (lbuf[i++] != ' ') {
   1.212 +                    throw new IOException("invalid header field");
   1.213 +                }
   1.214 +                name = new String(lbuf, 0, 0, i - 2);
   1.215 +                if (is.peek() == ' ') {
   1.216 +                    lastline = new byte[len - i];
   1.217 +                    System.arraycopy(lbuf, i, lastline, 0, len - i);
   1.218 +                    continue;
   1.219 +                }
   1.220 +                value = new String(lbuf, i, len - i, "UTF8");
   1.221 +            }
   1.222 +            try {
   1.223 +                if ((putValue(name, value) != null) && (!lineContinued)) {
   1.224 +                    throw new IOException("Duplicate name in Manifest: " + name + ".\n" + "Ensure that the manifest does not " + "have duplicate entries, and\n" + "that blank lines separate " + "individual sections in both your\n" + "manifest and in the META-INF/MANIFEST.MF " + "entry in the jar file.");
   1.225 +                }
   1.226 +            } catch (IllegalArgumentException e) {
   1.227 +                throw new IOException("invalid header field name: " + name);
   1.228 +            }
   1.229 +        }
   1.230 +    }
   1.231 +}