jdk/src/share/classes/java/util/jar/JarInputStream.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.util.ManifestEntryVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The <code>JarInputStream</code> class is used to read the contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a JAR file from any input stream. It extends the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <code>java.util.zip.ZipInputStream</code> with support for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * an optional <code>Manifest</code> entry. The <code>Manifest</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * can be used to store meta-information about the JAR file and its entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see     Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see     java.util.zip.ZipInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class JarInputStream extends ZipInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Manifest man;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private JarEntry first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private JarVerifier jv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private ManifestEntryVerifier mev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Creates a new <code>JarInputStream</code> and reads the optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * manifest. If a manifest is present, also attempts to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * the signatures if the JarInputStream is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param in the actual input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public JarInputStream(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this(in, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a new <code>JarInputStream</code> and reads the optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * manifest. If a manifest is present and verify is true, also attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * to verify the signatures if the JarInputStream is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param in the actual input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param verify whether or not to verify the JarInputStream if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * it is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public JarInputStream(InputStream in, boolean verify) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        JarEntry e = (JarEntry)super.getNextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (e != null && e.getName().equalsIgnoreCase("META-INF/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            e = (JarEntry)super.getNextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (e != null && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            man = new Manifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            byte bytes[] = getBytes(new BufferedInputStream(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            man.read(new ByteArrayInputStream(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            //man.read(new BufferedInputStream(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                jv = new JarVerifier(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                mev = new ManifestEntryVerifier(man);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            first = getNextJarEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            first = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private byte[] getBytes(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        while ((n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            baos.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Returns the <code>Manifest</code> for this JAR file, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <code>null</code> if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the <code>Manifest</code> for this JAR file, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *         <code>null</code> if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public Manifest getManifest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return man;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Reads the next ZIP file entry and positions the stream at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * beginning of the entry data. If verification has been enabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * any invalid signature detected while positioning the stream for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * the next entry will result in an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @exception SecurityException if any of the jar file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *         are incorrectly signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public ZipEntry getNextEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        JarEntry e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (first == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            e = (JarEntry)super.getNextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            e = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            first = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (jv != null && e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // At this point, we might have parsed all the meta-inf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // entries and have nothing to verify. If we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // nothing to verify, get rid of the JarVerifier object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (jv.nothingToVerify() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                jv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                mev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                jv.beginEntry(e, mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Reads the next JAR file entry and positions the stream at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * beginning of the entry data. If verification has been enabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * any invalid signature detected while positioning the stream for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the next entry will result in an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the next JAR file entry, or null if there are no more entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @exception SecurityException if any of the jar file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *         are incorrectly signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public JarEntry getNextJarEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return (JarEntry)getNextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Reads from the current JAR file entry into an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * If <code>len</code> is not zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * blocks until some input is available; otherwise, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * bytes are read and <code>0</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * If verification has been enabled, any invalid signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * on the current entry will be reported at some point before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * end of the entry is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param off the start offset in the destination array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param len the maximum number of bytes to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return the actual number of bytes read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *         entry is reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception SecurityException if any of the jar file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *         are incorrectly signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (first == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            n = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            n = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (jv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            jv.update(n, b, off, len, mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Creates a new <code>JarEntry</code> (<code>ZipEntry</code>) for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * specified JAR file entry name. The manifest attributes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * the specified JAR file entry name will be copied to the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <CODE>JarEntry</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param name the name of the JAR/ZIP file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return the <code>JarEntry</code> object just created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    protected ZipEntry createZipEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        JarEntry e = new JarEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            e.attr = man.getAttributes(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}