jdk/src/share/classes/java/util/jar/JarOutputStream.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The <code>JarOutputStream</code> class is used to write the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a JAR file to any output stream. It extends the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>java.util.zip.ZipOutputStream</code> with support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * for writing an optional <code>Manifest</code> entry. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>Manifest</code> can be used to specify meta-information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * 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.ZipOutputStream
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 JarOutputStream extends ZipOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final int JAR_MAGIC = 0xCAFE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Creates a new <code>JarOutputStream</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * <code>Manifest</code>. The manifest is written as the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * entry to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param out the actual output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @param man the optional <code>Manifest</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public JarOutputStream(OutputStream out, Manifest man) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (man == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new NullPointerException("man");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        man.write(new BufferedOutputStream(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Creates a new <code>JarOutputStream</code> with no manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param out the actual output stream
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 JarOutputStream(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Begins writing a new JAR file entry and positions the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * to the start of the entry data. This method will also close
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * any previous entry. The default compression method will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * used if no compression method was specified for the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The current time will be used if the entry has no set modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param ze the ZIP/JAR entry to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @exception ZipException if a ZIP error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void putNextEntry(ZipEntry ze) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (firstEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // Make sure that extra field data for first JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // entry includes JAR magic number id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            byte[] edata = ze.getExtra();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (edata == null || !hasMagic(edata)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (edata == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    edata = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    // Prepend magic to existing extra data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    byte[] tmp = new byte[edata.length + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    System.arraycopy(edata, 0, tmp, 4, edata.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    edata = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                set16(edata, 0, JAR_MAGIC); // extra field id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                set16(edata, 2, 0);         // extra field size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ze.setExtra(edata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            firstEntry = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        super.putNextEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private boolean firstEntry = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns true if specified byte array contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * jar magic extra field id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static boolean hasMagic(byte[] edata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            while (i < edata.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (get16(edata, i) == JAR_MAGIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                i += get16(edata, i + 2) + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // Invalid extra field data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Fetches unsigned 16-bit value from byte array at specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * The bytes are assumed to be in Intel (little-endian) byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static int get16(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Sets 16-bit value at specified offset. The bytes are assumed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * be in Intel (little-endian) byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static void set16(byte[] b, int off, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        b[off+0] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        b[off+1] = (byte)(value >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}