jdk/src/share/classes/com/sun/java/util/jar/pack/Utils.java
author ksrini
Sun, 22 Apr 2012 06:54:38 -0700
changeset 12544 5768f2e096de
parent 10115 eb08d08c7ef7
child 12857 0a5f341c2a28
permissions -rw-r--r--
6981776: Pack200 must support -target 7 bytecodes Summary: pack200 implementation of JSR-200 updated for JSR-292 changes Reviewed-by: jrose, ksrini Contributed-by: john.r.rose@oracle.com, kumar.x.srinivasan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
     2
 * Copyright (c) 2003, 2012, 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: 4919
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: 4919
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: 4919
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4919
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4919
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
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    28
import com.sun.java.util.jar.pack.ConstantPool.ClassEntry;
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    29
import com.sun.java.util.jar.pack.ConstantPool.DescriptorEntry;
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    30
import com.sun.java.util.jar.pack.ConstantPool.LiteralEntry;
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    31
import com.sun.java.util.jar.pack.ConstantPool.MemberEntry;
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    32
import com.sun.java.util.jar.pack.ConstantPool.SignatureEntry;
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    33
import com.sun.java.util.jar.pack.ConstantPool.Utf8Entry;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    34
import java.io.BufferedInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    35
import java.io.BufferedOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    36
import java.io.File;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    37
import java.io.FilterOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    38
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    39
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    40
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    41
import java.util.Date;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    42
import java.util.Enumeration;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    43
import java.util.Map;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    44
import java.util.jar.JarEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    45
import java.util.jar.JarFile;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    46
import java.util.jar.JarInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    47
import java.util.jar.JarOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6900
diff changeset
    48
import java.util.zip.ZipEntry;
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
    49
import sun.util.logging.PlatformLogger;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
    50
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class Utils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final String COM_PREFIX = "com.sun.java.util.jar.pack.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final String METAINF    = "META-INF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Outputs various diagnostic support information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * If >0, print summary comments (e.g., constant pool info).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * If >1, print unit comments (e.g., processing of classes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * If >2, print many comments (e.g., processing of members).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * If >3, print tons of comments (e.g., processing of references).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * (installer only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    63
    static final String DEBUG_VERBOSE = COM_PREFIX+"verbose";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Disables use of native code, prefers the Java-coded implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * (installer only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final String DEBUG_DISABLE_NATIVE = COM_PREFIX+"disable.native";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Use the default working TimeZone instead of UTC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Note: This has installer unpacker implications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * see: zip.cpp which uses gmtime vs. localtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static final String PACK_DEFAULT_TIMEZONE = COM_PREFIX+"default.timezone";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Property indicating that the unpacker should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * ignore the transmitted PACK_MODIFICATION_TIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * replacing it by the given value. The value can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * be a numeric string, representing the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * mSecs since the epoch (UTC), or the special string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * {@link #NOW}, meaning the current time (UTC).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * The default value is the special string {@link #KEEP},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * which asks the unpacker to preserve all transmitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * modification time information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * (installer only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final String UNPACK_MODIFICATION_TIME = COM_PREFIX+"unpack.modification.time";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Property indicating that the unpacker strip the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Debug Attributes, if they are present, in the pack stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The default value is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * (installer only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static final String UNPACK_STRIP_DEBUG = COM_PREFIX+"unpack.strip.debug";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Remove the input file after unpacking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * (installer only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static final String UNPACK_REMOVE_PACKFILE = COM_PREFIX+"unpack.remove.packfile";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * A possible value for MODIFICATION_TIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static final String NOW                             = "now";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // Other debug options:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    //   com...debug.bands=false      add band IDs to pack file, to verify sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    //   com...dump.bands=false       dump band contents to local disk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    //   com...no.vary.codings=false  turn off coding variation heuristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    //   com...no.big.strings=false   turn off "big string" feature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * If this property is set to {@link #TRUE}, the packer will preserve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * the ordering of class files of the original jar in the output archive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The ordering is preserved only for class-files; resource files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * may be reordered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * If the packer is allowed to reorder class files, it can marginally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * decrease the transmitted size of the archive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static final String PACK_KEEP_CLASS_ORDER = COM_PREFIX+"keep.class.order";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * This string PACK200 is given as a zip comment on all JAR files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * produced by this utility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static final String PACK_ZIP_ARCHIVE_MARKER_COMMENT = "PACK200";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   132
    // Keep a TLS point to the global data and environment.
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   133
    // This makes it simpler to supply environmental options
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // to the engine code, especially the native code.
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   135
    static final ThreadLocal<TLGlobals> currentInstance = new ThreadLocal<>();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   136
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   137
    // convenience method to access the TL globals
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   138
    static TLGlobals getTLGlobals() {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   139
        return currentInstance.get();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   140
    }
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   141
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static PropMap currentPropMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Object obj = currentInstance.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (obj instanceof PackerImpl)
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   145
            return ((PackerImpl)obj).props;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (obj instanceof UnpackerImpl)
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   147
            return ((UnpackerImpl)obj).props;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static final boolean nolog
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   152
        = Boolean.getBoolean(COM_PREFIX+"nolog");
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   153
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   154
    static final boolean SORT_MEMBERS_DESCR_MAJOR
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   155
        = Boolean.getBoolean(COM_PREFIX+"sort.members.descr.major");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   157
    static final boolean SORT_HANDLES_KIND_MAJOR
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   158
        = Boolean.getBoolean(COM_PREFIX+"sort.handles.kind.major");
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   159
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   160
    static final boolean SORT_INDY_BSS_MAJOR
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   161
        = Boolean.getBoolean(COM_PREFIX+"sort.indy.bss.major");
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   162
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   163
    static final boolean SORT_BSS_BSM_MAJOR
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   164
        = Boolean.getBoolean(COM_PREFIX+"sort.bss.bsm.major");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   166
    static class Pack200Logger {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   167
        private final String name;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   168
        private PlatformLogger log;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   169
        Pack200Logger(String name) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   170
            this.name = name;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   171
        }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   172
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   173
        private synchronized PlatformLogger getLogger() {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   174
            if (log == null) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   175
                log = PlatformLogger.getLogger(name);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   176
            }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   177
            return log;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   178
        }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   179
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   180
        public void warning(String msg, Object param) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   181
                getLogger().warning(msg, param);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   182
            }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   183
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   184
        public void warning(String msg) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   185
            warning(msg, null);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   186
        }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   187
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   188
        public void info(String msg) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   189
            int verbose = currentPropMap().getInteger(DEBUG_VERBOSE);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   190
            if (verbose > 0) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   191
                if (nolog) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   192
                    System.out.println(msg);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   193
                } else {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   194
                    getLogger().info(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   197
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   199
        public void fine(String msg) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   200
            int verbose = currentPropMap().getInteger(DEBUG_VERBOSE);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   201
            if (verbose > 0) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   202
                    System.out.println(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   204
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   207
    static final Pack200Logger log
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   208
        = new Pack200Logger("java.util.jar.Pack200");
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // Returns the Max Version String of this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static String getVersionString() {
6891
f8a528363fa5 6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents: 6313
diff changeset
   212
        return "Pack200, Vendor: " +
f8a528363fa5 6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents: 6313
diff changeset
   213
            System.getProperty("java.vendor") +
f8a528363fa5 6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents: 6313
diff changeset
   214
            ", Version: " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            Constants.JAVA6_PACKAGE_MAJOR_VERSION + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            Constants.JAVA6_PACKAGE_MINOR_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static void markJarFile(JarOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        out.setComment(PACK_ZIP_ARCHIVE_MARKER_COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // -0 mode helper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (in.getManifest() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            out.putNextEntry(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            in.getManifest().write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            out.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        byte[] buffer = new byte[1 << 14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            out.putNextEntry(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            for (int nr; 0 < (nr = in.read(buffer)); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                out.write(buffer, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        markJarFile(out);  // add PACK200 comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    static void copyJarFile(JarFile in, JarOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        byte[] buffer = new byte[1 << 14];
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 9035
diff changeset
   243
        for (Enumeration<JarEntry> e = in.entries(); e.hasMoreElements(); ) {
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 9035
diff changeset
   244
            JarEntry je = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            out.putNextEntry(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            InputStream ein = in.getInputStream(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            for (int nr; 0 < (nr = ein.read(buffer)); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                out.write(buffer, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        markJarFile(out);  // add PACK200 comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    static void copyJarFile(JarInputStream in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // 4947205 : Peformance is slow when using pack-effort=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        out = new NonCloser(out); // protect from JarOutputStream.close()
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   258
        try (JarOutputStream jout = new JarOutputStream(out)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   259
            copyJarFile(in, jout);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   260
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    static void copyJarFile(JarFile in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // 4947205 : Peformance is slow when using pack-effort=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        out = new NonCloser(out); // protect from JarOutputStream.close()
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   267
        try (JarOutputStream jout = new JarOutputStream(out)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   268
            copyJarFile(in, jout);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   269
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // Wrapper to prevent closing of client-supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    class NonCloser extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        NonCloser(OutputStream out) { super(out); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        public void close() throws IOException { flush(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
   static String getJarEntryName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (name == null)  return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return name.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static String zeString(ZipEntry ze) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int store = (ze.getCompressedSize() > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            : 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // Follow unzip -lv output
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   287
        return ze.getSize() + "\t" + ze.getMethod()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            + "\t" + ze.getCompressedSize() + "\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            + store + "%\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            + new Date(ze.getTime()) + "\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            + Long.toHexString(ze.getCrc()) + "\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            + ze.getName() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    static byte[] readMagic(BufferedInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        in.mark(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        byte[] magic = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        for (int i = 0; i < magic.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // read 1 byte at a time, so we always get 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (1 != in.read(magic, i, 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        in.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return magic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    // magic number recognizers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    static boolean isJarMagic(byte[] magic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return (magic[0] == (byte)'P' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                magic[1] == (byte)'K' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                magic[2] >= 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                magic[2] <  8 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                magic[3] == magic[2] + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    static boolean isPackMagic(byte[] magic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return (magic[0] == (byte)0xCA &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                magic[1] == (byte)0xFE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                magic[2] == (byte)0xD0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                magic[3] == (byte)0x0D);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static boolean isGZIPMagic(byte[] magic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return (magic[0] == (byte)0x1F &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                magic[1] == (byte)0x8B &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                magic[2] == (byte)0x08);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // fourth byte is variable "flg" field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private Utils() { } // do not instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}