Search in sources :

Example 1 with Packer

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

the class Utils method pack.

// given a jar file foo.jar will write to foo.pack
static void pack(JarFile jarFile, File packFile) throws IOException {
    Pack200.Packer packer = Pack200.newPacker();
    Map<String, String> p = packer.properties();
    // Take the time optimization vs. space
    // CAUTION: do not use 0.
    p.put(packer.EFFORT, "1");
    // Make the memory consumption as effective as possible
    p.put(packer.SEGMENT_LIMIT, "10000");
    // ignore all JAR deflation requests to save time
    p.put(packer.DEFLATE_HINT, packer.FALSE);
    // save the file ordering of the original JAR
    p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
    FileOutputStream fos = null;
    try {
        // Write out to a jtreg scratch area
        fos = new FileOutputStream(packFile);
        // Call the packer
        packer.pack(jarFile, fos);
    } finally {
        close(fos);
    }
}
Also used : Pack200(java.util.jar.Pack200) FileOutputStream(java.io.FileOutputStream)

Example 2 with Packer

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

the class T7007157 method main.

public static void main(String... args) throws IOException {
    File sdkHome = Utils.JavaSDK;
    File testJar = new File(new File(sdkHome, "lib"), "tools.jar");
    JarFile jarFile = new JarFile(testJar);
    File packFile = new File("foo.pack");
    Pack200.Packer packer = Pack200.newPacker();
    Map<String, String> p = packer.properties();
    // Take the time optimization vs. space
    // CAUTION: do not use 0.
    p.put(packer.EFFORT, "1");
    // Make the memory consumption as effective as possible
    p.put(packer.SEGMENT_LIMIT, "10000");
    // ignore all JAR deflation requests to save time
    p.put(packer.DEFLATE_HINT, packer.FALSE);
    // save the file ordering of the original JAR
    p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
    // strip the StackMapTables
    p.put(packer.CODE_ATTRIBUTE_PFX + "StackMapTable", packer.STRIP);
    FileOutputStream fos = null;
    try {
        // Write out to a jtreg scratch area
        fos = new FileOutputStream(packFile);
        // Call the packer
        packer.pack(jarFile, fos);
    } finally {
        Utils.close(fos);
        Utils.close(jarFile);
    }
    Utils.cleanup();
}
Also used : Pack200(java.util.jar.Pack200) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 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(new DevNullOutputStream()));
        jis = new JarInputStream(is);
        packer.pack(jis, os);
    } finally {
        System.setErr(err);
        if (closeIS)
            Util.closeEL(jis);
        if (closeOS)
            Util.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)

Example 4 with Packer

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

the class Pack200Props method verifyDefaults.

private static void verifyDefaults() {
    Map<String, String> expectedDefaults = new HashMap<>();
    Packer p = Pack200.newPacker();
    expectedDefaults.put("com.sun.java.util.jar.pack.default.timezone", p.FALSE);
    expectedDefaults.put("com.sun.java.util.jar.pack.disable.native", p.FALSE);
    expectedDefaults.put("com.sun.java.util.jar.pack.verbose", "0");
    expectedDefaults.put(p.CLASS_ATTRIBUTE_PFX + "CompilationID", "RUH");
    expectedDefaults.put(p.CLASS_ATTRIBUTE_PFX + "SourceID", "RUH");
    expectedDefaults.put(p.CODE_ATTRIBUTE_PFX + "CharacterRangeTable", "NH[PHPOHIIH]");
    expectedDefaults.put(p.CODE_ATTRIBUTE_PFX + "CoverageTable", "NH[PHHII]");
    expectedDefaults.put(p.DEFLATE_HINT, p.KEEP);
    expectedDefaults.put(p.EFFORT, "5");
    expectedDefaults.put(p.KEEP_FILE_ORDER, p.TRUE);
    expectedDefaults.put(p.MODIFICATION_TIME, p.KEEP);
    expectedDefaults.put(p.SEGMENT_LIMIT, "-1");
    expectedDefaults.put(p.UNKNOWN_ATTRIBUTE, p.PASS);
    Map<String, String> props = p.properties();
    int errors = 0;
    for (String key : expectedDefaults.keySet()) {
        String def = expectedDefaults.get(key);
        String x = props.get(key);
        if (x == null) {
            System.out.println("Error: key not found:" + key);
            errors++;
        } else {
            if (!def.equals(x)) {
                System.out.println("Error: key " + key + "\n  value expected: " + def + "\n  value obtained: " + x);
                errors++;
            }
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " error(s) encountered in default properties verification");
    }
}
Also used : HashMap(java.util.HashMap) Packer(java.util.jar.Pack200.Packer)

Example 5 with Packer

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

the class TestExceptions method pack200Test3.

// test the Pack200.pack(JarInputStream, OutputStream);
static void pack200Test3() {
    List<PackTestJarInputStream> tlist = new ArrayList<PackTestJarInputStream>();
    try {
        // setup the test scenarios
        try {
            tlist.add(new PackTestJarInputStream((JarInputStream) null, null));
            tlist.add(new PackTestJarInputStream((JarInputStream) null, new ByteArrayOutputStream()));
            tlist.add(new PackTestJarInputStream(new JarInputStream(new FileInputStream(testJar)), null));
        } catch (Exception e) {
            throw new Error("Initialization error", e);
        }
        for (PackTestJarInputStream ti : tlist) {
            System.out.println(ti);
            try {
                Pack200.Packer p = Pack200.newPacker();
                p.pack(ti.getJarInputStream(), ti.getOutputStream());
            } catch (Exception e) {
                ti.checkException(e);
            }
        }
    } finally {
        // keep jprt happy
        for (PackTestJarInputStream ti : tlist) {
            if (ti != null) {
                ti.close();
            }
        }
    }
}
Also used : Pack200(java.util.jar.Pack200) JarInputStream(java.util.jar.JarInputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

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