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