Search in sources :

Example 6 with Packer

use of java.util.jar.Pack200.Packer in project jdk8u_jdk by JetBrains.

the class PackageVersionTest method verifyPack.

static void verifyPack(String filename, int expected_major, int expected_minor) {
    File jarFileName = new File("test.jar");
    jarFileName.delete();
    String[] jargs = { "cvf", jarFileName.getName(), filename };
    Utils.jar(jargs);
    JarFile jfin = null;
    try {
        jfin = new JarFile(jarFileName);
        Packer packer = Pack200.newPacker();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        packer.pack(jfin, baos);
        baos.flush();
        baos.close();
        byte[] buf = baos.toByteArray();
        int minor = buf[4] & 0x000000ff;
        int major = buf[5] & 0x000000ff;
        if (major != expected_major || minor != expected_minor) {
            String msg = String.format("test fails: expected:%d.%d but got %d.%d\n", expected_major, expected_minor, major, minor);
            throw new Error(msg);
        }
        System.out.println(filename + ": OK");
    } catch (IOException ioe) {
        throw new RuntimeException(ioe.getMessage());
    } finally {
        Utils.close((Closeable) jfin);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Packer(java.util.jar.Pack200.Packer)

Example 7 with Packer

use of java.util.jar.Pack200.Packer in project jdk8u_jdk by JetBrains.

the class Driver method main.

public static void main(String[] ava) throws IOException {
    List<String> av = new ArrayList<>(Arrays.asList(ava));
    boolean doPack = true;
    boolean doUnpack = false;
    boolean doRepack = false;
    boolean doZip = true;
    String logFile = null;
    String verboseProp = Utils.DEBUG_VERBOSE;
    {
        // Non-standard, undocumented "--unpack" switch enables unpack mode.
        String arg0 = av.isEmpty() ? "" : av.get(0);
        switch(arg0) {
            case "--pack":
                av.remove(0);
                break;
            case "--unpack":
                av.remove(0);
                doPack = false;
                doUnpack = true;
                break;
        }
    }
    // Collect engine properties here:
    Map<String, String> engProps = new HashMap<>();
    engProps.put(verboseProp, System.getProperty(verboseProp));
    String optionMap;
    String[] propTable;
    if (doPack) {
        optionMap = PACK200_OPTION_MAP;
        propTable = PACK200_PROPERTY_TO_OPTION;
    } else {
        optionMap = UNPACK200_OPTION_MAP;
        propTable = UNPACK200_PROPERTY_TO_OPTION;
    }
    // Collect argument properties here:
    Map<String, String> avProps = new HashMap<>();
    try {
        for (; ; ) {
            String state = parseCommandOptions(av, optionMap, avProps);
            // Translate command line options to Pack200 properties:
            eachOpt: for (Iterator<String> opti = avProps.keySet().iterator(); opti.hasNext(); ) {
                String opt = opti.next();
                String prop = null;
                for (int i = 0; i < propTable.length; i += 2) {
                    if (opt.equals(propTable[1 + i])) {
                        prop = propTable[0 + i];
                        break;
                    }
                }
                if (prop != null) {
                    String val = avProps.get(opt);
                    // remove opt from avProps
                    opti.remove();
                    if (!prop.endsWith(".")) {
                        // Normal string or boolean.
                        if (!(opt.equals("--verbose") || opt.endsWith("="))) {
                            // Normal boolean; convert to T/F.
                            boolean flag = (val != null);
                            if (opt.startsWith("--no-"))
                                flag = !flag;
                            val = flag ? "true" : "false";
                        }
                        engProps.put(prop, val);
                    } else if (prop.contains(".attribute.")) {
                        for (String val1 : val.split("\0")) {
                            String[] val2 = val1.split("=", 2);
                            engProps.put(prop + val2[0], val2[1]);
                        }
                    } else {
                        // Collection property: pack.pass.file.cli.NNN
                        int idx = 1;
                        for (String val1 : val.split("\0")) {
                            String prop1;
                            do {
                                prop1 = prop + "cli." + (idx++);
                            } while (engProps.containsKey(prop1));
                            engProps.put(prop1, val1);
                        }
                    }
                }
            }
            // See if there is any other action to take.
            if ("--config-file=".equals(state)) {
                String propFile = av.remove(0);
                Properties fileProps = new Properties();
                try (InputStream propIn = new FileInputStream(propFile)) {
                    fileProps.load(propIn);
                }
                if (engProps.get(verboseProp) != null)
                    fileProps.list(System.out);
                for (Map.Entry<Object, Object> me : fileProps.entrySet()) {
                    engProps.put((String) me.getKey(), (String) me.getValue());
                }
            } else if ("--version".equals(state)) {
                System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.VERSION), Driver.class.getName(), "1.31, 07/05/05"));
                return;
            } else if ("--help".equals(state)) {
                printUsage(doPack, true, System.out);
                System.exit(1);
                return;
            } else {
                break;
            }
        }
    } catch (IllegalArgumentException ee) {
        System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_ARGUMENT), ee));
        printUsage(doPack, false, System.err);
        System.exit(2);
        return;
    }
    // Deal with remaining non-engine properties:
    for (String opt : avProps.keySet()) {
        String val = avProps.get(opt);
        switch(opt) {
            case "--repack":
                doRepack = true;
                break;
            case "--no-gzip":
                doZip = (val == null);
                break;
            case "--log-file=":
                logFile = val;
                break;
            default:
                throw new InternalError(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_OPTION), opt, avProps.get(opt)));
        }
    }
    if (logFile != null && !logFile.equals("")) {
        if (logFile.equals("-")) {
            System.setErr(System.out);
        } else {
            OutputStream log = new FileOutputStream(logFile);
            //log = new BufferedOutputStream(out);
            System.setErr(new PrintStream(log));
        }
    }
    boolean verbose = (engProps.get(verboseProp) != null);
    String packfile = "";
    if (!av.isEmpty())
        packfile = av.remove(0);
    String jarfile = "";
    if (!av.isEmpty())
        jarfile = av.remove(0);
    // output JAR file if --repack
    String newfile = "";
    // temporary backup of input JAR
    String bakfile = "";
    // temporary file to be deleted
    String tmpfile = "";
    if (doRepack) {
        // if a host OS truncates file extensions.)
        if (packfile.toLowerCase().endsWith(".pack") || packfile.toLowerCase().endsWith(".pac") || packfile.toLowerCase().endsWith(".gz")) {
            System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_REPACK_OUTPUT), packfile));
            printUsage(doPack, false, System.err);
            System.exit(2);
        }
        newfile = packfile;
        // The optional second argument is the source JAR file.
        if (jarfile.equals("")) {
            // If only one file is given, it is the only JAR.
            // It serves as both input and output.
            jarfile = newfile;
        }
        tmpfile = createTempFile(newfile, ".pack").getPath();
        packfile = tmpfile;
        // no need to zip the temporary file
        doZip = false;
    }
    if (!av.isEmpty() || // Accept jarfile of "-" (stdout), but only if unpacking.
    !(jarfile.toLowerCase().endsWith(".jar") || jarfile.toLowerCase().endsWith(".zip") || (jarfile.equals("-") && !doPack))) {
        printUsage(doPack, false, System.err);
        System.exit(2);
        return;
    }
    if (doRepack)
        doPack = doUnpack = true;
    else if (doPack)
        doUnpack = false;
    Pack200.Packer jpack = Pack200.newPacker();
    Pack200.Unpacker junpack = Pack200.newUnpacker();
    jpack.properties().putAll(engProps);
    junpack.properties().putAll(engProps);
    if (doRepack && newfile.equals(jarfile)) {
        String zipc = getZipComment(jarfile);
        if (verbose && zipc.length() > 0)
            System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.DETECTED_ZIP_COMMENT), zipc));
        if (zipc.indexOf(Utils.PACK_ZIP_ARCHIVE_MARKER_COMMENT) >= 0) {
            System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_REPACKED), jarfile));
            doPack = false;
            doUnpack = false;
            doRepack = false;
        }
    }
    try {
        if (doPack) {
            // Mode = Pack.
            JarFile in = new JarFile(new File(jarfile));
            OutputStream out;
            // Packfile must be -, *.gz, *.pack, or *.pac.
            if (packfile.equals("-")) {
                out = System.out;
                // Send warnings, etc., to stderr instead of stdout.
                System.setOut(System.err);
            } else if (doZip) {
                if (!packfile.endsWith(".gz")) {
                    System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WRITE_PACK_FILE), packfile));
                    printUsage(doPack, false, System.err);
                    System.exit(2);
                }
                out = new FileOutputStream(packfile);
                out = new BufferedOutputStream(out);
                out = new GZIPOutputStream(out);
            } else {
                if (!packfile.toLowerCase().endsWith(".pack") && !packfile.toLowerCase().endsWith(".pac")) {
                    System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WRITE_PACKGZ_FILE), packfile));
                    printUsage(doPack, false, System.err);
                    System.exit(2);
                }
                out = new FileOutputStream(packfile);
                out = new BufferedOutputStream(out);
            }
            jpack.pack(in, out);
            //in.close();  // p200 closes in but not out
            out.close();
        }
        if (doRepack && newfile.equals(jarfile)) {
            // If the source and destination are the same,
            // we will move the input JAR aside while regenerating it.
            // This allows us to restore it if something goes wrong.
            File bakf = createTempFile(jarfile, ".bak");
            // On Windows target must be deleted see 4017593
            bakf.delete();
            boolean okBackup = new File(jarfile).renameTo(bakf);
            if (!okBackup) {
                throw new Error(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_MOVE_FAILED), bakfile));
            } else {
                // Open jarfile recovery bracket.
                bakfile = bakf.getPath();
            }
        }
        if (doUnpack) {
            // Mode = Unpack.
            InputStream in;
            if (packfile.equals("-"))
                in = System.in;
            else
                in = new FileInputStream(new File(packfile));
            BufferedInputStream inBuf = new BufferedInputStream(in);
            in = inBuf;
            if (Utils.isGZIPMagic(Utils.readMagic(inBuf))) {
                in = new GZIPInputStream(in);
            }
            String outfile = newfile.equals("") ? jarfile : newfile;
            OutputStream fileOut;
            if (outfile.equals("-"))
                fileOut = System.out;
            else
                fileOut = new FileOutputStream(outfile);
            fileOut = new BufferedOutputStream(fileOut);
            try (JarOutputStream out = new JarOutputStream(fileOut)) {
                junpack.unpack(in, out);
            // p200 closes in but not out
            }
        // At this point, we have a good jarfile (or newfile, if -r)
        }
        if (!bakfile.equals("")) {
            // On success, abort jarfile recovery bracket.
            new File(bakfile).delete();
            bakfile = "";
        }
    } finally {
        // Close jarfile recovery bracket.
        if (!bakfile.equals("")) {
            File jarFile = new File(jarfile);
            // Win32 requires this, see above
            jarFile.delete();
            new File(bakfile).renameTo(jarFile);
        }
        // In all cases, delete temporary *.pack.
        if (!tmpfile.equals(""))
            new File(tmpfile).delete();
    }
}
Also used : Pack200(java.util.jar.Pack200) HashMap(java.util.HashMap) BufferedOutputStream(java.io.BufferedOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ArrayList(java.util.ArrayList) Properties(java.util.Properties) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedInputStream(java.io.BufferedInputStream) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) BufferedOutputStream(java.io.BufferedOutputStream) PrintStream(java.io.PrintStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) JarFile(java.util.jar.JarFile) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 8 with Packer

use of java.util.jar.Pack200.Packer in project Lucee by lucee.

the class Pack200Util method jar2pack.

public static void jar2pack(InputStream is, OutputStream os, boolean closeIS, boolean closeOS) throws IOException {
    // Create the Packer object
    Packer packer = Pack200.newPacker();
    // Initialize the state by setting the desired properties
    Map p = packer.properties();
    // take more time choosing codings for better compression
    // default is "5"
    p.put(Packer.EFFORT, "7");
    // use largest-possible archive segments (>10% better compression).
    p.put(Packer.SEGMENT_LIMIT, "-1");
    // reorder files for better compression.
    p.put(Packer.KEEP_FILE_ORDER, Packer.FALSE);
    // smear modification times to a single value.
    p.put(Packer.MODIFICATION_TIME, Packer.LATEST);
    // ignore all JAR deflation requests,
    // transmitting a single request to use "store" mode.
    p.put(Packer.DEFLATE_HINT, Packer.FALSE);
    // discard debug attributes
    p.put(Packer.CODE_ATTRIBUTE_PFX + "LineNumberTable", Packer.STRIP);
    // throw an error if an attribute is unrecognized
    p.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);
    JarInputStream jis = null;
    os = new GZIPOutputStream(os);
    PrintStream err = System.err;
    try {
        System.setErr(new PrintStream(DevNullOutputStream.DEV_NULL_OUTPUT_STREAM));
        jis = new JarInputStream(is);
        packer.pack(jis, os);
    } finally {
        System.setErr(err);
        if (closeIS)
            IOUtil.closeEL(jis);
        if (closeOS)
            IOUtil.closeEL(os);
    }
}
Also used : PrintStream(java.io.PrintStream) JarInputStream(java.util.jar.JarInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Map(java.util.Map) SortedMap(java.util.SortedMap) Packer(java.util.jar.Pack200.Packer)

Aggregations

Pack200 (java.util.jar.Pack200)4 Packer (java.util.jar.Pack200.Packer)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 PrintStream (java.io.PrintStream)3 Map (java.util.Map)3 SortedMap (java.util.SortedMap)3 JarFile (java.util.jar.JarFile)3 JarInputStream (java.util.jar.JarInputStream)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Iterator (java.util.Iterator)1