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