jdk/src/share/classes/com/sun/java/util/jar/pack/Utils.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: 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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final String DEBUG_VERBOSE = Utils.COM_PREFIX+"verbose";
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
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   137
    // convenience methods to access the TL globals
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
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   142
    static Map<String, Utf8Entry> getUtf8Entries() {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   143
        return getTLGlobals().getUtf8Entries();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   144
    }
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   145
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   146
    static Map<String, ClassEntry> getClassEntries() {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   147
        return getTLGlobals().getClassEntries();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   148
    }
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   149
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   150
    static Map<Object, LiteralEntry> getLiteralEntries() {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   151
        return getTLGlobals().getLiteralEntries();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   152
    }
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   153
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   154
    static Map<String, DescriptorEntry> getDescriptorEntries() {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   155
         return getTLGlobals().getDescriptorEntries();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   156
    }
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   157
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   158
    static Map<String, SignatureEntry> getSignatureEntries() {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   159
        return getTLGlobals().getSignatureEntries();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   160
    }
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   161
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   162
    static Map<String, MemberEntry> getMemberEntries() {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   163
        return getTLGlobals().getMemberEntries();
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   164
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static PropMap currentPropMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        Object obj = currentInstance.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (obj instanceof PackerImpl)
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   169
            return ((PackerImpl)obj).props;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (obj instanceof UnpackerImpl)
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   171
            return ((UnpackerImpl)obj).props;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static final boolean nolog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        = Boolean.getBoolean(Utils.COM_PREFIX+"nolog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   179
    static class Pack200Logger {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   180
        private final String name;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   181
        private PlatformLogger log;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   182
        Pack200Logger(String name) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   183
            this.name = name;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   184
        }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   185
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   186
        private synchronized PlatformLogger getLogger() {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   187
            if (log == null) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   188
                log = PlatformLogger.getLogger(name);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   189
            }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   190
            return log;
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   191
        }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   192
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   193
        public void warning(String msg, Object param) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   194
                getLogger().warning(msg, param);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   195
            }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   196
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   197
        public void warning(String msg) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   198
            warning(msg, null);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   199
        }
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   200
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   201
        public void info(String msg) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   202
            int verbose = currentPropMap().getInteger(DEBUG_VERBOSE);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   203
            if (verbose > 0) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   204
                if (nolog) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   205
                    System.out.println(msg);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   206
                } else {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   207
                    getLogger().info(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   210
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   212
        public void fine(String msg) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   213
            int verbose = currentPropMap().getInteger(DEBUG_VERBOSE);
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   214
            if (verbose > 0) {
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   215
                    System.out.println(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   217
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   220
    static final Pack200Logger log
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   221
        = new Pack200Logger("java.util.jar.Pack200");
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   222
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // Returns the Max Version String of this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static String getVersionString() {
6891
f8a528363fa5 6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents: 6313
diff changeset
   225
        return "Pack200, Vendor: " +
f8a528363fa5 6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents: 6313
diff changeset
   226
            System.getProperty("java.vendor") +
f8a528363fa5 6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents: 6313
diff changeset
   227
            ", Version: " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            Constants.JAVA6_PACKAGE_MAJOR_VERSION + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            Constants.JAVA6_PACKAGE_MINOR_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    static void markJarFile(JarOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        out.setComment(PACK_ZIP_ARCHIVE_MARKER_COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // -0 mode helper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (in.getManifest() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            out.putNextEntry(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            in.getManifest().write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            out.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte[] buffer = new byte[1 << 14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            out.putNextEntry(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            for (int nr; 0 < (nr = in.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(JarFile in, JarOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        byte[] buffer = new byte[1 << 14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        for (Enumeration e = in.entries(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            JarEntry je = (JarEntry) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            out.putNextEntry(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            InputStream ein = in.getInputStream(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for (int nr; 0 < (nr = ein.read(buffer)); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                out.write(buffer, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        markJarFile(out);  // add PACK200 comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    static void copyJarFile(JarInputStream in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // 4947205 : Peformance is slow when using pack-effort=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        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
   271
        try (JarOutputStream jout = new JarOutputStream(out)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   272
            copyJarFile(in, jout);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   273
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    static void copyJarFile(JarFile in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // 4947205 : Peformance is slow when using pack-effort=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        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
   280
        try (JarOutputStream jout = new JarOutputStream(out)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   281
            copyJarFile(in, jout);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   282
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // Wrapper to prevent closing of client-supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    class NonCloser extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        NonCloser(OutputStream out) { super(out); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        public void close() throws IOException { flush(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
   static String getJarEntryName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (name == null)  return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return name.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    static String zeString(ZipEntry ze) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int store = (ze.getCompressedSize() > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            : 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // 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
   300
        return ze.getSize() + "\t" + ze.getMethod()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            + "\t" + ze.getCompressedSize() + "\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            + store + "%\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            + new Date(ze.getTime()) + "\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            + Long.toHexString(ze.getCrc()) + "\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            + ze.getName() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    static byte[] readMagic(BufferedInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        in.mark(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        byte[] magic = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        for (int i = 0; i < magic.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // read 1 byte at a time, so we always get 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (1 != in.read(magic, i, 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        in.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return magic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // magic number recognizers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static boolean isJarMagic(byte[] magic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return (magic[0] == (byte)'P' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                magic[1] == (byte)'K' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                magic[2] >= 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                magic[2] <  8 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                magic[3] == magic[2] + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    static boolean isPackMagic(byte[] magic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return (magic[0] == (byte)0xCA &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                magic[1] == (byte)0xFE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                magic[2] == (byte)0xD0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                magic[3] == (byte)0x0D);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    static boolean isGZIPMagic(byte[] magic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return (magic[0] == (byte)0x1F &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                magic[1] == (byte)0x8B &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                magic[2] == (byte)0x08);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // fourth byte is variable "flg" field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private Utils() { } // do not instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}