jdk/src/java.base/share/classes/java/util/jar/Manifest.java
author avstepan
Thu, 06 Aug 2015 19:07:35 +0300
changeset 32037 ab4526f4ac10
parent 27804 4659e70271c4
child 45663 4a0cbf8f2474
permissions -rw-r--r--
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22078
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FilterInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The Manifest class is used to maintain Manifest entry names and their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * associated Attributes. There are main Manifest Attributes as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * per-entry Attributes. For information on the Manifest format, please
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <a href="../../../../technotes/guides/jar/jar.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Manifest format specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see     Attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class Manifest implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // manifest main attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Attributes attr = new Attributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // manifest entries
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 5506
diff changeset
    54
    private Map<String, Attributes> entries = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructs a new, empty Manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public Manifest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructs a new Manifest from the specified input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param is the input stream containing manifest data
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 12181
diff changeset
    66
     * @throws IOException if an I/O error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public Manifest(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        read(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a new Manifest that is a copy of the specified Manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param man the Manifest to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public Manifest(Manifest man) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        attr.putAll(man.getMainAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        entries.putAll(man.getEntries());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the main Attributes for the Manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @return the main Attributes for the Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public Attributes getMainAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns a Map of the entries contained in this Manifest. Each entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * is represented by a String name (key) and associated Attributes (value).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The Map permits the {@code null} key, but no entry with a null key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * created by {@link #read}, nor is such an entry written by using {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * #write}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return a Map of the entries contained in this Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public Map<String,Attributes> getEntries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Returns the Attributes for the specified entry name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * This method is defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *      return (Attributes)getEntries().get(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Though {@code null} is a valid {@code name}, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * {@code getAttributes(null)} is invoked on a {@code Manifest}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * obtained from a jar file, {@code null} will be returned.  While jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * files themselves do not allow {@code null}-named attributes, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * possible to invoke {@link #getEntries} on a {@code Manifest}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * on that result, invoke {@code put} with a null key and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * arbitrary value.  Subsequent invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * {@code getAttributes(null)} will return the just-{@code put}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Note that this method does not return the manifest's main attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * see {@link #getMainAttributes}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param name entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the Attributes for the specified entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public Attributes getAttributes(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return getEntries().get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Clears the main Attributes as well as the entries in this Manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        attr.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Writes the Manifest to the specified OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Attributes.Name.MANIFEST_VERSION must be set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * MainAttributes prior to invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see #getMainAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   146
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void write(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        DataOutputStream dos = new DataOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // Write out the main attributes for the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        attr.writeMain(dos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // Now write out the pre-entry attributes
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
   152
        for (Map.Entry<String, Attributes> e : entries.entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            StringBuffer buffer = new StringBuffer("Name: ");
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 5506
diff changeset
   154
            String value = e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                byte[] vb = value.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                value = new String(vb, 0, 0, vb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            buffer.append(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            buffer.append("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            make72Safe(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            dos.writeBytes(buffer.toString());
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 5506
diff changeset
   163
            e.getValue().write(dos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Adds line breaks to enforce a maximum 72 bytes per line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static void make72Safe(StringBuffer line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int length = line.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (length > 72) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int index = 70;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            while (index < length - 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                line.insert(index, "\r\n ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                index += 72;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                length += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Reads the Manifest from the specified InputStream. The entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * names and attributes read will be merged in with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * manifest entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param is the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public void read(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // Buffered input stream for reading manifest data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        FastInputStream fis = new FastInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Line buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        byte[] lbuf = new byte[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // Read the main attributes for the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        attr.read(fis, lbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Total number of entries, attributes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int ecount = 0, acount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // Average size of entry attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int asize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // Now parse the manifest entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        boolean skipEmptyLines = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        byte[] lastline = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        while ((len = fis.readLine(lbuf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (lbuf[--len] != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                throw new IOException("manifest line too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (len > 0 && lbuf[len-1] == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                --len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (len == 0 && skipEmptyLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            skipEmptyLines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                name = parseName(lbuf, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    throw new IOException("invalid manifest format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                if (fis.peek() == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    // name is wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    lastline = new byte[len - 6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    System.arraycopy(lbuf, 6, lastline, 0, len - 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                // continuation line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                byte[] buf = new byte[lastline.length + len - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                System.arraycopy(lastline, 0, buf, 0, lastline.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                if (fis.peek() == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    // name is wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    lastline = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                name = new String(buf, 0, buf.length, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                lastline = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            Attributes attr = getAttributes(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                attr = new Attributes(asize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                entries.put(name, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            attr.read(fis, lbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            ecount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            acount += attr.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            //XXX: Fix for when the average is 0. When it is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // you get an Attributes object with an initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // capacity of 0, which tickles a bug in HashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            asize = Math.max(2, acount / ecount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            skipEmptyLines = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private String parseName(byte[] lbuf, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (toLower(lbuf[0]) == 'n' && toLower(lbuf[1]) == 'a' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            toLower(lbuf[2]) == 'm' && toLower(lbuf[3]) == 'e' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            lbuf[4] == ':' && lbuf[5] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                return new String(lbuf, 6, len - 6, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private int toLower(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return (c >= 'A' && c <= 'Z') ? 'a' + (c - 'A') : c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Returns true if the specified Object is also a Manifest and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * the same main Attributes and entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param o the object to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return true if the specified Object is also a Manifest and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * the same main Attributes and entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (o instanceof Manifest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            Manifest m = (Manifest)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return attr.equals(m.getMainAttributes()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                   entries.equals(m.getEntries());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns the hash code for this Manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return attr.hashCode() + entries.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Returns a shallow copy of this Manifest.  The shallow copy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * implemented as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *     public Object clone() { return new Manifest(this); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return a shallow copy of this Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return new Manifest(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * A fast buffered input stream for parsing manifest files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    static class FastInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        private byte buf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        private int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        private int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        FastInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            this(in, 8192);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        FastInputStream(InputStream in, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            buf = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (pos >= count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (pos >= count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
11828
590711df7828 7143629: JDK jar/zip code should use unsigned library support
darcy
parents: 11274
diff changeset
   341
            return Byte.toUnsignedInt(buf[pos++]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            int avail = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (avail <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                if (len >= buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    return in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                avail = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (avail <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (len > avail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                len = avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            System.arraycopy(buf, pos, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            pos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * Reads 'len' bytes from the input stream, or until an end-of-line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * is reached. Returns the number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        public int readLine(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            byte[] tbuf = this.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            while (total < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                int avail = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                if (avail <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    avail = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    if (avail <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                int n = len - total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                if (n > avail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    n = avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                int tpos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                int maxpos = tpos + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                while (tpos < maxpos && tbuf[tpos++] != '\n') ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                n = tpos - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                System.arraycopy(tbuf, pos, b, off, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                pos = tpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                if (tbuf[tpos-1] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        public byte peek() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (pos == count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                fill();
12181
d7c6dd7a2bd5 7148584: Jar tools fails to generate manifest correctly when boundary condition hit
coffeys
parents: 11828
diff changeset
   402
            if (pos == count)
d7c6dd7a2bd5 7148584: Jar tools fails to generate manifest correctly when boundary condition hit
coffeys
parents: 11828
diff changeset
   403
                return -1; // nothing left in buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return buf[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        public int readLine(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return readLine(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            long avail = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (avail <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return in.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (n > avail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                n = avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return (count - pos) + in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        private void fill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            count = pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            int n = in.read(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                count = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}