src/java.base/share/classes/java/util/jar/JarOutputStream.java
author jboes
Tue, 24 Sep 2019 09:43:43 +0100
changeset 58288 48e480e56aad
parent 58242 94bb65cb37d3
child 58679 9c3209ff7550
child 59201 b24f4caa1411
permissions -rw-r--r--
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: bchristi, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2019, 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.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
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    32
 * The {@code JarOutputStream} class is used to write the contents
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a JAR file to any output stream. It extends the class
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    34
 * {@code java.util.zip.ZipOutputStream} with support
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    35
 * for writing an optional {@code Manifest} entry. The
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    36
 * {@code Manifest} can be used to specify meta-information about
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    49
     * Creates a new {@code JarOutputStream} with the specified
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    50
     * {@code Manifest}. The manifest is written as the first
2
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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    54
     * @param man the optional {@code Manifest}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    55
     * @throws    IOException if an I/O error has occurred
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    69
     * Creates a new {@code JarOutputStream} with no manifest.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param out the actual output stream
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    71
     * @throws    IOException if an I/O error has occurred
2
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
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    86
     * @throws    ZipException if a ZIP error has occurred
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    87
     * @throws    IOException if an I/O error has occurred
2
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) {
11828
590711df7828 7143629: JDK jar/zip code should use unsigned library support
darcy
parents: 5506
diff changeset
   138
        return Byte.toUnsignedInt(b[off]) | ( Byte.toUnsignedInt(b[off+1]) << 8);
2
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
}