rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/ManifestInputStream.java
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.
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.
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).
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.
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
25 package org.apidesign.bck2brwsr.emul.lang;
27 import java.io.FilterInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import org.apidesign.bck2brwsr.core.Exported;
33 * A fast buffered input stream for parsing manifest files.
35 * Taken from java.util.jar.Manifest.FastInputStream and modified to be
36 * independent of other Manifest functionality.
39 public abstract class ManifestInputStream extends FilterInputStream {
41 private int count = 0;
44 protected ManifestInputStream(InputStream in) {
48 protected ManifestInputStream(InputStream in, int size) {
53 public int read() throws IOException {
60 return buf[pos++] & 0xff;
63 public int read(byte[] b, int off, int len) throws IOException {
64 int avail = count - pos;
66 if (len >= buf.length) {
67 return in.read(b, off, len);
78 System.arraycopy(buf, pos, b, off, len);
84 * Reads 'len' bytes from the input stream, or until an end-of-line
85 * is reached. Returns the number of bytes read.
87 public int readLine(byte[] b, int off, int len) throws IOException {
88 byte[] tbuf = this.buf;
91 int avail = count - pos;
104 int maxpos = tpos + n;
105 while (tpos < maxpos && tbuf[tpos++] != '\n') {
109 System.arraycopy(tbuf, pos, b, off, n);
113 if (tbuf[tpos - 1] == '\n') {
120 public byte peek() throws IOException {
125 return -1; // nothing left in buffer
130 public int readLine(byte[] b) throws IOException {
131 return readLine(b, 0, b.length);
134 public long skip(long n) throws IOException {
138 long avail = count - pos;
149 public int available() throws IOException {
150 return (count - pos) + in.available();
153 public void close() throws IOException {
161 private void fill() throws IOException {
163 int n = in.read(buf, 0, buf.length);
169 protected abstract String putValue(String key, String value);
171 public void readAttributes(byte[] lbuf) throws IOException {
172 ManifestInputStream is = this;
176 byte[] lastline = null;
178 while ((len = is.readLine(lbuf)) != -1) {
179 boolean lineContinued = false;
180 if (lbuf[--len] != '\n') {
181 throw new IOException("line too long");
183 if (len > 0 && lbuf[len - 1] == '\r') {
190 if (lbuf[0] == ' ') {
192 throw new IOException("misplaced continuation line");
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() == ' ') {
202 value = new String(buf, 0, buf.length, "UTF8");
205 while (lbuf[i++] != ':') {
207 throw new IOException("invalid header field");
210 if (lbuf[i++] != ' ') {
211 throw new IOException("invalid header field");
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);
219 value = new String(lbuf, i, len - i, "UTF8");
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.");
225 } catch (IllegalArgumentException e) {
226 throw new IOException("invalid header field name: " + name);