jdk/src/share/classes/com/sun/java/util/jar/pack/Driver.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8803 b3f57bfca459
child 13041 8477cb6992be
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8803
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    28
import java.io.BufferedInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    29
import java.io.BufferedOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    30
import java.io.File;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    31
import java.io.FileInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    32
import java.io.FileOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    33
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    34
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    35
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    36
import java.io.PrintStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.text.MessageFormat;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    38
import java.util.ArrayList;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    39
import java.util.Arrays;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    40
import java.util.HashMap;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    41
import java.util.Iterator;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    42
import java.util.List;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    43
import java.util.ListIterator;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    44
import java.util.Map;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    45
import java.util.Properties;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    46
import java.util.ResourceBundle;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    47
import java.util.SortedMap;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    48
import java.util.TreeMap;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    49
import java.util.jar.JarFile;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    50
import java.util.jar.JarOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    51
import java.util.jar.Pack200;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    52
import java.util.zip.GZIPInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    53
import java.util.zip.GZIPOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/** Command line interface for Pack200.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class Driver {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    58
        private static final ResourceBundle RESOURCE =
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    59
                ResourceBundle.getBundle("com.sun.java.util.jar.pack.DriverResource");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static void main(String[] ava) throws IOException {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    62
        List<String> av = new ArrayList<>(Arrays.asList(ava));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        boolean doPack   = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        boolean doUnpack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        boolean doRepack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        boolean doZip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        String logFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        String verboseProp = Utils.DEBUG_VERBOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // Non-standard, undocumented "--unpack" switch enables unpack mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            String arg0 = av.isEmpty() ? "" : av.get(0);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    74
            switch (arg0) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    75
                case "--pack":
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                av.remove(0);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    77
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    78
                case "--unpack":
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                doPack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                doUnpack = true;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    82
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // Collect engine properties here:
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    87
        Map<String,String> engProps = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        engProps.put(verboseProp, System.getProperty(verboseProp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        String optionMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        String[] propTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (doPack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            optionMap = PACK200_OPTION_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            propTable = PACK200_PROPERTY_TO_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            optionMap = UNPACK200_OPTION_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            propTable = UNPACK200_PROPERTY_TO_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Collect argument properties here:
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   101
        Map<String,String> avProps = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                String state = parseCommandOptions(av, optionMap, avProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // Translate command line options to Pack200 properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            eachOpt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                for (Iterator<String> opti = avProps.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                     opti.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    String opt = opti.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    String prop = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    for (int i = 0; i < propTable.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        if (opt.equals(propTable[1+i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            prop = propTable[0+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                            break;
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 (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        String val = avProps.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        opti.remove();  // remove opt from avProps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        if (!prop.endsWith(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            // Normal string or boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            if (!(opt.equals("--verbose")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                  || opt.endsWith("="))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                // Normal boolean; convert to T/F.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                boolean flag = (val != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                if (opt.startsWith("--no-"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                    flag = !flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                val = flag? "true": "false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                            engProps.put(prop, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        } else if (prop.contains(".attribute.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                            for (String val1 : val.split("\0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                String[] val2 = val1.split("=", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                engProps.put(prop+val2[0], val2[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            // Collection property: pack.pass.file.cli.NNN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                            int idx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                            for (String val1 : val.split("\0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                String prop1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                    prop1 = prop+"cli."+(idx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                } while (engProps.containsKey(prop1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                engProps.put(prop1, val1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // See if there is any other action to take.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   151
                if ("--config-file=".equals(state)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    String propFile = av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    Properties fileProps = new Properties();
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   154
                    try (InputStream propIn = new FileInputStream(propFile)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   155
                        fileProps.load(propIn);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   156
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    if (engProps.get(verboseProp) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        fileProps.list(System.out);
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   159
                    for (Map.Entry<Object,Object> me : fileProps.entrySet()) {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   160
                        engProps.put((String) me.getKey(), (String) me.getValue());
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   161
                    }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   162
                } else if ("--version".equals(state)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.VERSION), Driver.class.getName(), "1.31, 07/05/05"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    return;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   165
                } else if ("--help".equals(state)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    printUsage(doPack, true, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } catch (IllegalArgumentException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_ARGUMENT), ee));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Deal with remaining non-engine properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        for (String opt : avProps.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            String val = avProps.get(opt);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   183
            switch (opt) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   184
                case "--repack":
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   185
                    doRepack = true;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   186
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   187
                case "--no-gzip":
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   188
                    doZip = (val == null);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   189
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   190
                case "--log-file=":
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   191
                    logFile = val;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   192
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   193
                default:
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   194
                    throw new InternalError(MessageFormat.format(
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   195
                            RESOURCE.getString(DriverResource.BAD_OPTION),
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   196
                            opt, avProps.get(opt)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (logFile != null && !logFile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (logFile.equals("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                System.setErr(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                OutputStream log = new FileOutputStream(logFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                //log = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                System.setErr(new PrintStream(log));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        boolean verbose = (engProps.get(verboseProp) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        String packfile = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (!av.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            packfile = av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        String jarfile = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (!av.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            jarfile = av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        String newfile = "";  // output JAR file if --repack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        String bakfile = "";  // temporary backup of input JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        String tmpfile = "";  // temporary file to be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (doRepack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // The first argument is the target JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // (Note:  *.pac is nonstandard, but may be necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // if a host OS truncates file extensions.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (packfile.toLowerCase().endsWith(".pack") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                packfile.toLowerCase().endsWith(".pac") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                packfile.toLowerCase().endsWith(".gz")) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   230
                System.err.println(MessageFormat.format(
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   231
                        RESOURCE.getString(DriverResource.BAD_REPACK_OUTPUT),
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   232
                        packfile));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            newfile = packfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // The optional second argument is the source JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (jarfile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // If only one file is given, it is the only JAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                // It serves as both input and output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                jarfile = newfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            tmpfile = createTempFile(newfile, ".pack").getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            packfile = tmpfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            doZip = false;  // no need to zip the temporary file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (!av.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // Accept jarfiles ending with .jar or .zip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // Accept jarfile of "-" (stdout), but only if unpacking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            || !(jarfile.toLowerCase().endsWith(".jar")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                 || jarfile.toLowerCase().endsWith(".zip")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                 || (jarfile.equals("-") && !doPack))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (doRepack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            doPack = doUnpack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        else if (doPack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            doUnpack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        Pack200.Packer jpack = Pack200.newPacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Pack200.Unpacker junpack = Pack200.newUnpacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        jpack.properties().putAll(engProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        junpack.properties().putAll(engProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (doRepack && newfile.equals(jarfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            String zipc = getZipComment(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (verbose && zipc.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.DETECTED_ZIP_COMMENT), zipc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (zipc.indexOf(Utils.PACK_ZIP_ARCHIVE_MARKER_COMMENT) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_REPACKED), jarfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        doPack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        doUnpack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        doRepack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (doPack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                // Mode = Pack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                JarFile in = new JarFile(new File(jarfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                // Packfile must be -, *.gz, *.pack, or *.pac.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                if (packfile.equals("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    // Send warnings, etc., to stderr instead of stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    System.setOut(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                } else if (doZip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    if (!packfile.endsWith(".gz")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WRITE_PACK_FILE), packfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    out = new FileOutputStream(packfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    out = new GZIPOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    if (!packfile.toLowerCase().endsWith(".pack") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                            !packfile.toLowerCase().endsWith(".pac")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WIRTE_PACKGZ_FILE),packfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    out = new FileOutputStream(packfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                jpack.pack(in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                //in.close();  // p200 closes in but not out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (doRepack && newfile.equals(jarfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                // If the source and destination are the same,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // we will move the input JAR aside while regenerating it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                // This allows us to restore it if something goes wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                File bakf = createTempFile(jarfile, ".bak");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // On Windows target must be deleted see 4017593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                bakf.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                boolean okBackup = new File(jarfile).renameTo(bakf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (!okBackup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        throw new Error(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_MOVE_FAILED),bakfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    // Open jarfile recovery bracket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    bakfile = bakf.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (doUnpack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                // Mode = Unpack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (packfile.equals("-"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    in = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    in = new FileInputStream(new File(packfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                BufferedInputStream inBuf = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                in = inBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (Utils.isGZIPMagic(Utils.readMagic(inBuf))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    in = new GZIPInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                String outfile = newfile.equals("")? jarfile: newfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                OutputStream fileOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (outfile.equals("-"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    fileOut = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    fileOut = new FileOutputStream(outfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                fileOut = new BufferedOutputStream(fileOut);
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   351
                try (JarOutputStream out = new JarOutputStream(fileOut)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   352
                    junpack.unpack(in, out);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   353
                    // p200 closes in but not out
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   354
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                // At this point, we have a good jarfile (or newfile, if -r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (!bakfile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        // On success, abort jarfile recovery bracket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        new File(bakfile).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        bakfile = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // Close jarfile recovery bracket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (!bakfile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                File jarFile = new File(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                jarFile.delete(); // Win32 requires this, see above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                new File(bakfile).renameTo(jarFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // In all cases, delete temporary *.pack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (!tmpfile.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                new File(tmpfile).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    static private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    File createTempFile(String basefile, String suffix) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        File base = new File(basefile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        String prefix = base.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (prefix.length() < 3)  prefix += "tmp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        File where = base.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if ( base.getParentFile() == null && suffix.equals(".bak"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            where = new File(".").getAbsoluteFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        File f = File.createTempFile(prefix, suffix, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    static private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    void printUsage(boolean doPack, boolean full, PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        String prog = doPack ? "pack200" : "unpack200";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        String[] packUsage = (String[])RESOURCE.getObject(DriverResource.PACK_HELP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        String[] unpackUsage = (String[])RESOURCE.getObject(DriverResource.UNPACK_HELP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        String[] usage = doPack? packUsage: unpackUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        for (int i = 0; i < usage.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            out.println(usage[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (!full) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            out.println(MessageFormat.format(RESOURCE.getString(DriverResource.MORE_INFO), prog));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    static private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        String getZipComment(String jarfile) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        byte[] tail = new byte[1000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        long filelen = new File(jarfile).length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (filelen <= 0)  return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        long skiplen = Math.max(0, filelen - tail.length);
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   414
        try (InputStream in = new FileInputStream(new File(jarfile))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            in.skip(skiplen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            in.read(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            for (int i = tail.length-4; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                if (tail[i+0] == 'P' && tail[i+1] == 'K' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    tail[i+2] ==  5  && tail[i+3] ==  6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    // Skip sig4, disks4, entries4, clen4, coff4, cmt2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    i += 4+4+4+4+4+2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    if (i < tail.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        return new String(tail, i, tail.length-i, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private static final String PACK200_OPTION_MAP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        (""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         +"--repack                 $ \n  -r +>- @--repack              $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         +"--no-gzip                $ \n  -g +>- @--no-gzip             $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         +"--strip-debug            $ \n  -G +>- @--strip-debug         $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         +"--no-keep-file-order     $ \n  -O +>- @--no-keep-file-order  $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         +"--segment-limit=      *> = \n  -S +>  @--segment-limit=      = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         +"--effort=             *> = \n  -E +>  @--effort=             = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         +"--deflate-hint=       *> = \n  -H +>  @--deflate-hint=       = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         +"--modification-time=  *> = \n  -m +>  @--modification-time=  = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         +"--pass-file=        *> &\0 \n  -P +>  @--pass-file=        &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         +"--unknown-attribute=  *> = \n  -U +>  @--unknown-attribute=  = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         +"--class-attribute=  *> &\0 \n  -C +>  @--class-attribute=  &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         +"--field-attribute=  *> &\0 \n  -F +>  @--field-attribute=  &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         +"--method-attribute= *> &\0 \n  -M +>  @--method-attribute= &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         +"--code-attribute=   *> &\0 \n  -D +>  @--code-attribute=   &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         +"--config-file=      *>   . \n  -f +>  @--config-file=        . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         // Negative options as required by CLIP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         +"--no-strip-debug  !--strip-debug         \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         +"--gzip            !--no-gzip             \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         +"--keep-file-order !--no-keep-file-order  \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         // Non-Standard Options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         +"--verbose                $ \n  -v +>- @--verbose             $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         +"--quiet        !--verbose  \n  -q +>- !--verbose               \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         +"--log-file=           *> = \n  -l +>  @--log-file=           = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         //+"--java-option=      *> = \n  -J +>  @--java-option=        = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         +"--version                . \n  -V +>  @--version             . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         +"--help               . \n  -? +> @--help . \n  -h +> @--help . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         // Termination:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         +"--           . \n"  // end option sequence here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         +"-   +?    >- . \n"  // report error if -XXX present; else use stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    // Note: Collection options use "\0" as a delimiter between arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // For Java version of unpacker (used for testing only):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    private static final String UNPACK200_OPTION_MAP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        (""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         +"--deflate-hint=       *> = \n  -H +>  @--deflate-hint=       = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         +"--verbose                $ \n  -v +>- @--verbose             $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         +"--quiet        !--verbose  \n  -q +>- !--verbose               \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         +"--remove-pack-file       $ \n  -r +>- @--remove-pack-file    $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         +"--log-file=           *> = \n  -l +>  @--log-file=           = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         +"--config-file=        *> . \n  -f +>  @--config-file=        . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         // Termination:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         +"--           . \n"  // end option sequence here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         +"-   +?    >- . \n"  // report error if -XXX present; else use stdin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         +"--version                . \n  -V +>  @--version             . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         +"--help               . \n  -? +> @--help . \n  -h +> @--help . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private static final String[] PACK200_PROPERTY_TO_OPTION = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        Pack200.Packer.SEGMENT_LIMIT, "--segment-limit=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        Pack200.Packer.KEEP_FILE_ORDER, "--no-keep-file-order",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        Pack200.Packer.EFFORT, "--effort=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        Pack200.Packer.DEFLATE_HINT, "--deflate-hint=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        Pack200.Packer.MODIFICATION_TIME, "--modification-time=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        Pack200.Packer.PASS_FILE_PFX, "--pass-file=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        Pack200.Packer.UNKNOWN_ATTRIBUTE, "--unknown-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        Pack200.Packer.CLASS_ATTRIBUTE_PFX, "--class-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        Pack200.Packer.FIELD_ATTRIBUTE_PFX, "--field-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        Pack200.Packer.METHOD_ATTRIBUTE_PFX, "--method-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        Pack200.Packer.CODE_ATTRIBUTE_PFX, "--code-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        //Pack200.Packer.PROGRESS, "--progress=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        Utils.DEBUG_VERBOSE, "--verbose",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        Utils.COM_PREFIX+"strip.debug", "--strip-debug",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private static final String[] UNPACK200_PROPERTY_TO_OPTION = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        Pack200.Unpacker.DEFLATE_HINT, "--deflate-hint=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        //Pack200.Unpacker.PROGRESS, "--progress=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        Utils.DEBUG_VERBOSE, "--verbose",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        Utils.UNPACK_REMOVE_PACKFILE, "--remove-pack-file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /*-*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Remove a set of command-line options from args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * storing them in the map in a canonicalized form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * The options string is a newline-separated series of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * option processing specifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    String parseCommandOptions(List<String> args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                               String options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                               Map<String,String> properties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        //System.out.println(args+" // "+properties);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        String resultString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // Convert options string into optLines dictionary.
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   525
        TreeMap<String,String[]> optmap = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    loadOptmap:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        for (String optline : options.split("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            String[] words = optline.split("\\p{Space}+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (words.length == 0)    continue loadOptmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            String opt = words[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            words[0] = "";  // initial word is not a spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (opt.length() == 0 && words.length >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                opt = words[1];  // initial "word" is empty due to leading ' '
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                words[1] = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (opt.length() == 0)    continue loadOptmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            String[] prevWords = optmap.put(opt, words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (prevWords != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            throw new RuntimeException(MessageFormat.format(RESOURCE.getString(DriverResource.DUPLICATE_OPTION), optline.trim()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // State machine for parsing a command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        ListIterator<String> argp = args.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        ListIterator<String> pbp = new ArrayList<String>().listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    doArgs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            // One trip through this loop per argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            // Multiple trips per option only if several options per argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            String arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (pbp.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                arg = pbp.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                pbp.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            } else if (argp.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                arg = argp.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // No more arguments at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                break doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        tryOpt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            for (int optlen = arg.length(); ; optlen--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                // One time through this loop for each matching arg prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                String opt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                // Match some prefix of the argument to a key in optmap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            findOpt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    opt = arg.substring(0, optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (optmap.containsKey(opt))  break findOpt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    if (optlen == 0)              break tryOpt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    // Decide on a smaller prefix to search for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    SortedMap<String,String[]> pfxmap = optmap.headMap(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    // pfxmap.lastKey is no shorter than any prefix in optmap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    int len = pfxmap.isEmpty() ? 0 : pfxmap.lastKey().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    optlen = Math.min(len, optlen - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    opt = arg.substring(0, optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    // (Note:  We could cut opt down to its common prefix with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    // pfxmap.lastKey, but that wouldn't save many cycles.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                opt = opt.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                assert(arg.startsWith(opt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                assert(opt.length() == optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                String val = arg.substring(optlen);  // arg == opt+val
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                // Execute the option processing specs for this opt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                // If no actions are taken, then look for a shorter prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                boolean didAction = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                boolean isError = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                int pbpMark = pbp.nextIndex();  // in case of backtracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                String[] specs = optmap.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            eachSpec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                for (String spec : specs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    if (spec.length() == 0)     continue eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    if (spec.startsWith("#"))   break eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    int sidx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    char specop = spec.charAt(sidx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    // Deal with '+'/'*' prefixes (spec conditions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    switch (specop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        // + means we want an non-empty val suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        ok = (val.length() != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                        specop = spec.charAt(sidx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        // * means we accept empty or non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        specop = spec.charAt(sidx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        // No condition prefix means we require an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        // match, as indicated by an empty val suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        ok = (val.length() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    if (!ok)  continue eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    String specarg = spec.substring(sidx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    switch (specop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    case '.':  // terminate the option sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                        resultString = (specarg.length() != 0)? specarg.intern(): opt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                        break doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    case '?':  // abort the option sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        resultString = (specarg.length() != 0)? specarg.intern(): arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        isError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        break eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    case '@':  // change the effective opt name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        opt = specarg.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    case '>':  // shift remaining arg val to next arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        pbp.add(specarg + val);  // push a new argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                        val = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    case '!':  // negation option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                        String negopt = (specarg.length() != 0)? specarg.intern(): opt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        properties.remove(negopt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        properties.put(negopt, null);  // leave placeholder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        didAction = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    case '$':  // normal "boolean" option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        String boolval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        if (specarg.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                            // If there is a given spec token, store it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                            boolval = specarg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                            String old = properties.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            if (old == null || old.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                                boolval = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                // Increment any previous value as a numeral.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                boolval = ""+(1+Integer.parseInt(old));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        properties.put(opt, boolval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        didAction = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    case '=':  // "string" option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    case '&':  // "collection" option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        // Read an option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        boolean append = (specop == '&');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        String strval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        if (pbp.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            strval = pbp.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                            pbp.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        } else if (argp.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                            strval = argp.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                            resultString = arg + " ?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                            isError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                            break eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        if (append) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                            String old = properties.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                            if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                                // Append new val to old with embedded delim.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                String delim = specarg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                if (delim.length() == 0)  delim = " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                strval = old + specarg + strval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        properties.put(opt, strval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        didAction = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        throw new RuntimeException(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_SPEC),opt, spec));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                // Done processing specs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                if (didAction && !isError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    continue doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                // The specs should have done something, but did not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                while (pbp.nextIndex() > pbpMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    // Remove anything pushed during these specs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    pbp.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    pbp.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                if (isError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    throw new IllegalArgumentException(resultString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                if (optlen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    // We cannot try a shorter matching option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    break tryOpt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            // If we come here, there was no matching option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            // So, push back the argument, and return to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            pbp.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            break doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        // Report number of arguments consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        args.subList(0, argp.nextIndex()).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        // Report any unconsumed partial argument.
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   719
        while (pbp.hasPrevious()) {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   720
            args.add(0, pbp.previous());
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   721
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        //System.out.println(args+" // "+properties+" -> "+resultString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return resultString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
}