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