jdk/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8803 b3f57bfca459
child 10115 eb08d08c7ef7
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8803
diff changeset
     2
 * Copyright (c) 2003, 2011, 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 com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.beans.PropertyChangeListener;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    29
import java.io.BufferedInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    30
import java.io.ByteArrayOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    31
import java.io.File;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    32
import java.io.FileInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    33
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    34
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    35
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    36
import java.util.HashSet;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    37
import java.util.Set;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    38
import java.util.SortedMap;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    39
import java.util.TimeZone;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    40
import java.util.jar.JarEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    41
import java.util.jar.JarInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    42
import java.util.jar.JarOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    43
import java.util.jar.Pack200;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    44
import java.util.zip.CRC32;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    45
import java.util.zip.CheckedOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    46
import java.util.zip.ZipEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Implementation of the Pack provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Kumar Srinivasan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    56
public class UnpackerImpl extends TLGlobals implements Pack200.Unpacker {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Register a listener for changes to options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param listener  An object to be invoked when a property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void addPropertyChangeListener(PropertyChangeListener listener) {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    64
        props.addListener(listener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
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
     * Remove a listener for the PropertyChange event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param listener  The PropertyChange listener to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void removePropertyChangeListener(PropertyChangeListener listener) {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    73
        props.removeListener(listener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    76
    public UnpackerImpl() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Get the set of options for the pack and unpack engines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @return A sorted association of option key strings to option values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    84
    @SuppressWarnings("unchecked")
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    85
    public SortedMap properties() {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    86
        return props;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Back-pointer to NativeUnpacker, when active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    Object _nunp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return Utils.getVersionString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    //Driver routines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // The unpack worker...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Takes a packed-stream InputStream, and writes to a JarOutputStream. Internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * the entire buffer must be read, it may be more efficient to read the packed-stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * to a file and pass the File object, in the alternate method described below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Closes its input but not its output.  (The output can accumulate more elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param in an InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param out a JarOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @exception IOException if an error is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   110
    public void unpack(InputStream in, JarOutputStream out) throws IOException {
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   111
        if (in == null) {
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   112
            throw new NullPointerException("null input");
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   113
        }
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   114
        if (out == null) {
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   115
            throw new NullPointerException("null output");
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   116
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        assert(Utils.currentInstance.get() == null);
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   118
        TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE))
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   119
                      ? null
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   120
                      : TimeZone.getDefault();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Utils.currentInstance.set(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   125
            final int verbose = props.getInteger(Utils.DEBUG_VERBOSE);
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   126
            BufferedInputStream in0 = new BufferedInputStream(in);
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   127
            if (Utils.isJarMagic(Utils.readMagic(in0))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    Utils.log.info("Copying unpacked JAR file...");
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   130
                Utils.copyJarFile(new JarInputStream(in0), out);
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   131
            } else if (props.getBoolean(Utils.DEBUG_DISABLE_NATIVE)) {
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   132
                (new DoUnpack()).run(in0, out);
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   133
                in0.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                Utils.markJarFile(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   136
                (new NativeUnpack(this)).run(in0, out);
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   137
                in0.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                Utils.markJarFile(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            _nunp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Utils.currentInstance.set(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (tz != null) TimeZone.setDefault(tz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Takes an input File containing the pack file, and generates a JarOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Does not close its output.  (The output can accumulate more elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param in a File.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param out a JarOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @exception IOException if an error is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void unpack(File in, JarOutputStream out) throws IOException {
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   156
        if (in == null) {
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   157
            throw new NullPointerException("null input");
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   158
        }
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   159
        if (out == null) {
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   160
            throw new NullPointerException("null output");
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6313
diff changeset
   161
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // Use the stream-based implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // %%% Reconsider if native unpacker learns to memory-map the file.
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   164
        try (FileInputStream instr = new FileInputStream(in)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   165
            unpack(instr, out);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   166
        }
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   167
        if (props.getBoolean(Utils.UNPACK_REMOVE_PACKFILE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            in.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private class DoUnpack {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   173
        final int verbose = props.getInteger(Utils.DEBUG_VERBOSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   176
            props.setInteger(Pack200.Unpacker.PROGRESS, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Here's where the bits are read from disk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        final Package pkg = new Package();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        final boolean keepModtime
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   183
            = Pack200.Packer.KEEP.equals(
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   184
              props.getProperty(Utils.UNPACK_MODIFICATION_TIME, Pack200.Packer.KEEP));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        final boolean keepDeflateHint
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   186
            = Pack200.Packer.KEEP.equals(
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   187
              props.getProperty(Pack200.Unpacker.DEFLATE_HINT, Pack200.Packer.KEEP));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        final int modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        final boolean deflateHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (!keepModtime) {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   192
                modtime = props.getTime(Utils.UNPACK_MODIFICATION_TIME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                modtime = pkg.default_modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            deflateHint = (keepDeflateHint) ? false :
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   198
                props.getBoolean(java.util.jar.Pack200.Unpacker.DEFLATE_HINT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // Checksum apparatus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        final CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        final ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        final OutputStream crcOut = new CheckedOutputStream(bufOut, crc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        public void run(BufferedInputStream in, JarOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (verbose > 0) {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   208
                props.list(System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            for (int seg = 1; ; seg++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                unpackSegment(in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                // Try to get another segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (!Utils.isPackMagic(Utils.readMagic(in)))  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    Utils.log.info("Finished segment #"+seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   221
            props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // Process the output directory or jar output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            new PackageReader(pkg, in).read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   225
            if (props.getBoolean("unpack.strip.debug"))    pkg.stripAttributeKind("Debug");
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   226
            if (props.getBoolean("unpack.strip.compile"))  pkg.stripAttributeKind("Compile");
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   227
            props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            pkg.ensureAllClassFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            // Now write out the files.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   230
            Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   231
            for (Package.File file : pkg.getFiles()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                String name = file.nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                JarEntry je = new JarEntry(Utils.getJarEntryName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                boolean deflate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   236
                deflate = (keepDeflateHint)
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   237
                          ? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   238
                            ((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   239
                          : deflateHint;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                boolean needCRC = !deflate;  // STORE mode requires CRC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (needCRC)  crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                bufOut.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (file.isClassStub()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    Package.Class cls = file.getStubClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    assert(cls != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    classesToWrite.remove(cls);  // for an error check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    // collect data & maybe CRC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    file.writeTo(needCRC ? crcOut : bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if (needCRC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    je.setMethod(JarEntry.STORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    je.setSize(bufOut.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    je.setCrc(crc.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (keepModtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    je.setTime(file.modtime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    // Convert back to milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    je.setTime((long)file.modtime * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    je.setTime((long)modtime * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                out.putNextEntry(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                bufOut.writeTo(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                out.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            assert(classesToWrite.isEmpty());
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   277
            props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            pkg.reset();  // reset for the next segment, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}