rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/ManifestInputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Apr 2016 08:35:46 +0200
changeset 1943 4b4f9dbc807b
parent 772 d382dacfd73f
permissions -rw-r--r--
emul.zip needs to initialize ClassPath dynamically
jaroslav@672
     1
/*
jaroslav@672
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
jaroslav@672
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@672
     4
 *
jaroslav@672
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@672
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@672
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@672
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@672
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@672
    10
 *
jaroslav@672
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@672
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@672
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@672
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@672
    15
 * accompanied this code).
jaroslav@672
    16
 *
jaroslav@672
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@672
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@672
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@672
    20
 *
jaroslav@672
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@672
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@672
    23
 * questions.
jaroslav@672
    24
 */
jaroslav@672
    25
package org.apidesign.bck2brwsr.emul.lang;
jaroslav@672
    26
jaroslav@672
    27
import java.io.FilterInputStream;
jaroslav@672
    28
import java.io.IOException;
jaroslav@672
    29
import java.io.InputStream;
jaroslav@1943
    30
import org.apidesign.bck2brwsr.core.Exported;
jaroslav@672
    31
jaroslav@672
    32
/*
jaroslav@672
    33
 * A fast buffered input stream for parsing manifest files.
jaroslav@672
    34
 * 
jaroslav@672
    35
 * Taken from java.util.jar.Manifest.FastInputStream and modified to be
jaroslav@672
    36
 * independent of other Manifest functionality.
jaroslav@672
    37
 */
jaroslav@1943
    38
@Exported
jaroslav@672
    39
public abstract class ManifestInputStream extends FilterInputStream {
jaroslav@672
    40
    private byte[] buf;
jaroslav@672
    41
    private int count = 0;
jaroslav@672
    42
    private int pos = 0;
jaroslav@672
    43
jaroslav@672
    44
    protected ManifestInputStream(InputStream in) {
jaroslav@672
    45
        this(in, 8192);
jaroslav@672
    46
    }
jaroslav@672
    47
jaroslav@672
    48
    protected ManifestInputStream(InputStream in, int size) {
jaroslav@672
    49
        super(in);
jaroslav@672
    50
        buf = new byte[size];
jaroslav@672
    51
    }
jaroslav@672
    52
jaroslav@672
    53
    public int read() throws IOException {
jaroslav@672
    54
        if (pos >= count) {
jaroslav@672
    55
            fill();
jaroslav@672
    56
            if (pos >= count) {
jaroslav@672
    57
                return -1;
jaroslav@672
    58
            }
jaroslav@672
    59
        }
jaroslav@672
    60
        return buf[pos++] & 0xff;
jaroslav@672
    61
    }
jaroslav@672
    62
jaroslav@672
    63
    public int read(byte[] b, int off, int len) throws IOException {
jaroslav@672
    64
        int avail = count - pos;
jaroslav@672
    65
        if (avail <= 0) {
jaroslav@672
    66
            if (len >= buf.length) {
jaroslav@672
    67
                return in.read(b, off, len);
jaroslav@672
    68
            }
jaroslav@672
    69
            fill();
jaroslav@672
    70
            avail = count - pos;
jaroslav@672
    71
            if (avail <= 0) {
jaroslav@672
    72
                return -1;
jaroslav@672
    73
            }
jaroslav@672
    74
        }
jaroslav@672
    75
        if (len > avail) {
jaroslav@672
    76
            len = avail;
jaroslav@672
    77
        }
jaroslav@672
    78
        System.arraycopy(buf, pos, b, off, len);
jaroslav@672
    79
        pos += len;
jaroslav@672
    80
        return len;
jaroslav@672
    81
    }
jaroslav@672
    82
jaroslav@672
    83
    /*
jaroslav@672
    84
     * Reads 'len' bytes from the input stream, or until an end-of-line
jaroslav@672
    85
     * is reached. Returns the number of bytes read.
jaroslav@672
    86
     */
jaroslav@672
    87
    public int readLine(byte[] b, int off, int len) throws IOException {
jaroslav@672
    88
        byte[] tbuf = this.buf;
jaroslav@672
    89
        int total = 0;
jaroslav@672
    90
        while (total < len) {
jaroslav@672
    91
            int avail = count - pos;
jaroslav@672
    92
            if (avail <= 0) {
jaroslav@672
    93
                fill();
jaroslav@672
    94
                avail = count - pos;
jaroslav@672
    95
                if (avail <= 0) {
jaroslav@672
    96
                    return -1;
jaroslav@672
    97
                }
jaroslav@672
    98
            }
jaroslav@672
    99
            int n = len - total;
jaroslav@672
   100
            if (n > avail) {
jaroslav@672
   101
                n = avail;
jaroslav@672
   102
            }
jaroslav@672
   103
            int tpos = pos;
jaroslav@672
   104
            int maxpos = tpos + n;
jaroslav@672
   105
            while (tpos < maxpos && tbuf[tpos++] != '\n') {
jaroslav@672
   106
                ;
jaroslav@672
   107
            }
jaroslav@672
   108
            n = tpos - pos;
jaroslav@672
   109
            System.arraycopy(tbuf, pos, b, off, n);
jaroslav@672
   110
            off += n;
jaroslav@672
   111
            total += n;
jaroslav@672
   112
            pos = tpos;
jaroslav@672
   113
            if (tbuf[tpos - 1] == '\n') {
jaroslav@672
   114
                break;
jaroslav@672
   115
            }
jaroslav@672
   116
        }
jaroslav@672
   117
        return total;
jaroslav@672
   118
    }
jaroslav@672
   119
jaroslav@672
   120
    public byte peek() throws IOException {
jaroslav@672
   121
        if (pos == count) {
jaroslav@672
   122
            fill();
jaroslav@672
   123
        }
jaroslav@672
   124
        if (pos == count) {
jaroslav@672
   125
            return -1; // nothing left in buffer
jaroslav@672
   126
        }
jaroslav@672
   127
        return buf[pos];
jaroslav@672
   128
    }
jaroslav@672
   129
jaroslav@672
   130
    public int readLine(byte[] b) throws IOException {
jaroslav@672
   131
        return readLine(b, 0, b.length);
jaroslav@672
   132
    }
jaroslav@672
   133
jaroslav@672
   134
    public long skip(long n) throws IOException {
jaroslav@672
   135
        if (n <= 0) {
jaroslav@672
   136
            return 0;
jaroslav@672
   137
        }
jaroslav@672
   138
        long avail = count - pos;
jaroslav@672
   139
        if (avail <= 0) {
jaroslav@672
   140
            return in.skip(n);
jaroslav@672
   141
        }
jaroslav@672
   142
        if (n > avail) {
jaroslav@672
   143
            n = avail;
jaroslav@672
   144
        }
jaroslav@672
   145
        pos += n;
jaroslav@672
   146
        return n;
jaroslav@672
   147
    }
jaroslav@672
   148
jaroslav@672
   149
    public int available() throws IOException {
jaroslav@672
   150
        return (count - pos) + in.available();
jaroslav@672
   151
    }
jaroslav@672
   152
jaroslav@672
   153
    public void close() throws IOException {
jaroslav@672
   154
        if (in != null) {
jaroslav@672
   155
            in.close();
jaroslav@672
   156
            in = null;
jaroslav@672
   157
            buf = null;
jaroslav@672
   158
        }
jaroslav@672
   159
    }
jaroslav@672
   160
jaroslav@672
   161
    private void fill() throws IOException {
jaroslav@672
   162
        count = pos = 0;
jaroslav@672
   163
        int n = in.read(buf, 0, buf.length);
jaroslav@672
   164
        if (n > 0) {
jaroslav@672
   165
            count = n;
jaroslav@672
   166
        }
jaroslav@672
   167
    }
jaroslav@672
   168
    
jaroslav@672
   169
    protected abstract String putValue(String key, String value);
jaroslav@672
   170
jaroslav@672
   171
    public void readAttributes(byte[] lbuf) throws IOException {
jaroslav@672
   172
        ManifestInputStream is = this;
jaroslav@672
   173
jaroslav@672
   174
        String name = null;
jaroslav@672
   175
        String value = null;
jaroslav@672
   176
        byte[] lastline = null;
jaroslav@672
   177
        int len;
jaroslav@672
   178
        while ((len = is.readLine(lbuf)) != -1) {
jaroslav@672
   179
            boolean lineContinued = false;
jaroslav@672
   180
            if (lbuf[--len] != '\n') {
jaroslav@672
   181
                throw new IOException("line too long");
jaroslav@672
   182
            }
jaroslav@672
   183
            if (len > 0 && lbuf[len - 1] == '\r') {
jaroslav@672
   184
                --len;
jaroslav@672
   185
            }
jaroslav@672
   186
            if (len == 0) {
jaroslav@672
   187
                break;
jaroslav@672
   188
            }
jaroslav@672
   189
            int i = 0;
jaroslav@672
   190
            if (lbuf[0] == ' ') {
jaroslav@672
   191
                if (name == null) {
jaroslav@672
   192
                    throw new IOException("misplaced continuation line");
jaroslav@672
   193
                }
jaroslav@672
   194
                lineContinued = true;
jaroslav@672
   195
                byte[] buf = new byte[lastline.length + len - 1];
jaroslav@672
   196
                System.arraycopy(lastline, 0, buf, 0, lastline.length);
jaroslav@672
   197
                System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
jaroslav@672
   198
                if (is.peek() == ' ') {
jaroslav@672
   199
                    lastline = buf;
jaroslav@672
   200
                    continue;
jaroslav@672
   201
                }
jaroslav@672
   202
                value = new String(buf, 0, buf.length, "UTF8");
jaroslav@672
   203
                lastline = null;
jaroslav@672
   204
            } else {
jaroslav@672
   205
                while (lbuf[i++] != ':') {
jaroslav@672
   206
                    if (i >= len) {
jaroslav@672
   207
                        throw new IOException("invalid header field");
jaroslav@672
   208
                    }
jaroslav@672
   209
                }
jaroslav@672
   210
                if (lbuf[i++] != ' ') {
jaroslav@672
   211
                    throw new IOException("invalid header field");
jaroslav@672
   212
                }
jaroslav@672
   213
                name = new String(lbuf, 0, 0, i - 2);
jaroslav@672
   214
                if (is.peek() == ' ') {
jaroslav@672
   215
                    lastline = new byte[len - i];
jaroslav@672
   216
                    System.arraycopy(lbuf, i, lastline, 0, len - i);
jaroslav@672
   217
                    continue;
jaroslav@672
   218
                }
jaroslav@672
   219
                value = new String(lbuf, i, len - i, "UTF8");
jaroslav@672
   220
            }
jaroslav@672
   221
            try {
jaroslav@672
   222
                if ((putValue(name, value) != null) && (!lineContinued)) {
jaroslav@672
   223
                    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.");
jaroslav@672
   224
                }
jaroslav@672
   225
            } catch (IllegalArgumentException e) {
jaroslav@672
   226
                throw new IOException("invalid header field name: " + name);
jaroslav@672
   227
            }
jaroslav@672
   228
        }
jaroslav@672
   229
    }
jaroslav@672
   230
}