Example usage for java.util.jar Pack200 newPacker

List of usage examples for java.util.jar Pack200 newPacker

Introduction

In this page you can find the example usage for java.util.jar Pack200 newPacker.

Prototype

public static synchronized Packer newPacker() 

Source Link

Document

Obtain new instance of a class that implements Packer.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JarFile f = new JarFile("a.jar");
    Pack200.Packer packer = Pack200.newPacker();
    OutputStream out = new FileOutputStream("a.pack");
    packer.pack(f, out);/*from   w  ww. j a  v a 2s  .  co  m*/
    out.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    OutputStream out = null;//from   w ww  . j a  va 2  s  .  c o  m

    JarFile f = new JarFile(args[0]);
    Pack200.Packer packer = Pack200.newPacker();
    out = new FileOutputStream(args[0] + ".pack");
    packer.pack(f, out);
    out.close();
}

From source file:com.izforge.izpack.installer.unpacker.Pack200FileUnpackerTest.java

private Pack200.Packer createPack200Packer(PackFile packFile) {
    Pack200.Packer packer = Pack200.newPacker();
    Map<String, String> defaultPackerProperties = packer.properties();
    Map<String, String> localPackerProperties = packFile.getPack200Properties();
    if (localPackerProperties != null) {
        defaultPackerProperties.putAll(localPackerProperties);
    }/* w w  w . j av  a 2 s  . c  o  m*/
    return packer;
}

From source file:com.izforge.izpack.compiler.packager.impl.Packager.java

private Pack200.Packer createAgressivePack200Packer() {
    Pack200.Packer packer = Pack200.newPacker();
    Map<String, String> packerProperties = packer.properties();
    packerProperties.put(Pack200.Packer.EFFORT, "9");
    packerProperties.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
    packerProperties.put(Pack200.Packer.KEEP_FILE_ORDER, Pack200.Packer.FALSE);
    packerProperties.put(Pack200.Packer.DEFLATE_HINT, Pack200.Packer.FALSE);
    packerProperties.put(Pack200.Packer.MODIFICATION_TIME, Pack200.Packer.LATEST);
    packerProperties.put(Pack200.Packer.CODE_ATTRIBUTE_PFX + "LineNumberTable", Pack200.Packer.STRIP);
    packerProperties.put(Pack200.Packer.CODE_ATTRIBUTE_PFX + "LocalVariableTable", Pack200.Packer.STRIP);
    packerProperties.put(Pack200.Packer.CODE_ATTRIBUTE_PFX + "SourceFile", Pack200.Packer.STRIP);
    return packer;
}

From source file:org.lamport.tla.toolbox.jcloud.PayloadHelper.java

public static Payload appendModel2Jar(final Path modelPath, String mainClass, Properties properties,
        IProgressMonitor monitor) throws IOException {

    /*// w  w  w .ja  v a2  s.  co m
     * Get the standard tla2tools.jar from the classpath as a blueprint.
     * It's located in the org.lamport.tla.toolbox.jclouds bundle in the
     * files/ directory. It uses OSGi functionality to read files/tla2tools.jar
     * from the .jclouds bundle.
     * The copy of the blueprint will contain the spec & model and 
     * additional metadata (properties, amended manifest).
     */
    final Bundle bundle = FrameworkUtil.getBundle(PayloadHelper.class);
    final URL toolsURL = bundle.getEntry("files/tla2tools.jar");
    if (toolsURL == null) {
        throw new RuntimeException("No tlatools.jar and/or spec to deploy");
    }

    /* 
     * Copy the tla2tools.jar blueprint to a temporary location on
     * disk to append model files below.
     */
    final File tempFile = File.createTempFile("tla2tools", ".jar");
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(toolsURL.openStream(), out);
    }

    /*
     * Create a virtual filesystem in jar format.
     */
    final Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    final URI uri = URI.create("jar:" + tempFile.toURI());

    try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
        /*
         * Copy the spec and model into the jar's model/ folder.
         * Also copy any module override (.class file) into the jar.
         */
        try (DirectoryStream<Path> modelDirectoryStream = Files.newDirectoryStream(modelPath,
                "*.{cfg,tla,class}")) {
            for (final Path file : modelDirectoryStream) {
                final Path to = fs.getPath("/model/" + file.getFileName());
                Files.copy(file, to, StandardCopyOption.REPLACE_EXISTING);
            }
        }

        /*
         * Add given class as Main-Class statement to jar's manifest. This
         * causes Java to launch this class when no other Main class is 
         * given on the command line. Thus, it shortens the command line
         * for us.
         */
        final Path manifestPath = fs.getPath("/META-INF/", "MANIFEST.MF");
        final Manifest manifest = new Manifest(Files.newInputStream(manifestPath));
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass);
        final PipedOutputStream ps = new PipedOutputStream();
        final PipedInputStream is = new PipedInputStream(ps);
        manifest.write(ps);
        ps.close();
        Files.copy(is, manifestPath, StandardCopyOption.REPLACE_EXISTING);

        /*
         * Add properties file to archive. The property file contains the
         * result email address... from where TLC eventually reads it.
         */

        // On Windows 7 and above the file has to be created in the system's
        // temp folder. Otherwise except file creation to fail with a
        // AccessDeniedException
        final File f = File.createTempFile("generated", "properties");
        OutputStream out = new FileOutputStream(f);
        // Append all entries in "properties" to the temp file f
        properties.store(out, "This is an optional header comment string");
        // Copy the temp file f into the jar with path /model/generated.properties.
        final Path to = fs.getPath("/model/generated.properties");
        Files.copy(f.toPath(), to, StandardCopyOption.REPLACE_EXISTING);
    } catch (final IOException e1) {
        throw new RuntimeException("No model directory found to deploy", e1);
    }

    /*
     * Compress archive with pack200 to achieve a much higher compression rate. We
     * are going to send the file on the wire after all:
     * 
     * effort: take more time choosing codings for better compression segment: use
     * largest-possible archive segments (>10% better compression) mod time: smear
     * modification times to a single value deflate: ignore all JAR deflation hints
     * in original archive
     */
    final Packer packer = Pack200.newPacker();
    final Map<String, String> p = packer.properties();
    p.put(Packer.EFFORT, "9");
    p.put(Packer.SEGMENT_LIMIT, "-1");
    p.put(Packer.MODIFICATION_TIME, Packer.LATEST);
    p.put(Packer.DEFLATE_HINT, Packer.FALSE);

    // Do not reorder which changes package names. Pkg name changes e.g. break
    // SimpleFilenameToStream.
    p.put(Packer.KEEP_FILE_ORDER, Packer.TRUE);

    // Throw an error if any of the above attributes is unrecognized.
    p.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);

    final File packTempFile = File.createTempFile("tla2tools", ".pack.gz");
    try (final JarFile jarFile = new JarFile(tempFile);
            final GZIPOutputStream fos = new GZIPOutputStream(new FileOutputStream(packTempFile));) {
        packer.pack(jarFile, fos);
    } catch (IOException ioe) {
        throw new RuntimeException("Failed to pack200 the tla2tools.jar file", ioe);
    }

    /*
     * Convert the customized tla2tools.jar into a jClouds payload object. This is
     * the format it will be transfered on the wire. This is handled by jClouds
     * though.
     */
    Payload jarPayLoad = null;
    try {
        final InputStream openStream = new FileInputStream(packTempFile);
        jarPayLoad = Payloads.newInputStreamPayload(openStream);
        // manually set length of content to prevent a NPE bug
        jarPayLoad.getContentMetadata().setContentLength(Long.valueOf(openStream.available()));
    } catch (final IOException e1) {
        throw new RuntimeException("No tlatools.jar to deploy", e1);
    } finally {
        monitor.worked(5);
    }

    return jarPayLoad;
}

From source file:org.sonar.server.plugins.PluginCompression.java

private static void pack200(Path jarPath, Path toPath, String pluginKey) {
    Profiler profiler = Profiler.create(LOG);
    profiler.startInfo("Compressing with pack200 plugin: " + pluginKey);
    Pack200.Packer packer = Pack200.newPacker();

    try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(jarPath)));
            OutputStream out = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(toPath)))) {
        packer.pack(in, out);//  ww w .  ja  v  a 2  s .c  o m
    } catch (IOException e) {
        throw new IllegalStateException(
                String.format("Fail to pack200 plugin [%s] '%s' to '%s'", pluginKey, jarPath, toPath), e);
    }
    profiler.stopInfo();
}