Example usage for java.util.jar JarInputStream JarInputStream

List of usage examples for java.util.jar JarInputStream JarInputStream

Introduction

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

Prototype

public JarInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new JarInputStream and reads the optional manifest.

Usage

From source file:org.hecl.jarhack.JarHack.java

/**
 * The <code>substHecl</code> method takes the filenames of two
 * .jar's - one as input, the second as output, in addition to the
 * name of the application.  Where it counts, the old name (Hecl,
 * usually) is overridden with the new name, and the new .jar file
 * is written to the specified outfile.  Via the iconname argument
 * it is also possible to specify a new icon file to use.
 *
 * @param infile a <code>FileInputStream</code> value
 * @param outfile a <code>String</code> value
 * @param newname a <code>String</code> value
 * @param iconname a <code>String</code> value
 * @exception IOException if an error occurs
 *///from   w ww.j  a  va2  s.  co  m
public static void substHecl(InputStream infile, String outfile, String newname, String iconname,
        String scriptfile) throws IOException {

    JarInputStream jif = new JarInputStream(infile);
    Manifest mf = jif.getManifest();
    Attributes attrs = mf.getMainAttributes();

    Set keys = attrs.keySet();
    Iterator it = keys.iterator();
    while (it.hasNext()) {
        Object key = it.next();
        Object value = attrs.get(key);
        String keyname = key.toString();

        /* These are the three cases that interest us in
         * particular, where we need to make changes. */
        if (keyname.equals("MIDlet-Name")) {
            attrs.putValue(keyname, newname);
        } else if (keyname.equals("MIDlet-1")) {
            String valuestr = value.toString();
            /* FIXME - the stringsplit method is used for older
             * versions of GCJ.  Once newer versions are common,
             * it can go away.  Or not - it works just fine. */
            String properties[] = stringsplit(valuestr, ", ");
            attrs.putValue(keyname, newname + ", " + properties[1] + ", " + properties[2]);
        } else if (keyname.equals("MicroEdition-Configuration")) {
            cldcversion = value.toString();
        } else if (keyname.equals("MicroEdition-Profile")) {
            midpversion = value.toString();
        } else if (keyname.equals("MIDlet-Jar-URL")) {
            attrs.put(key, newname + ".jar");
        }
    }

    JarOutputStream jof = new JarOutputStream(new FileOutputStream(outfile), mf);

    byte[] buf = new byte[4096];

    /* Go through the various entries. */
    JarEntry entry;
    int read;
    while ((entry = jif.getNextJarEntry()) != null) {

        /* Don't copy the manifest file. */
        if ("META-INF/MANIFEST.MF".equals(entry.getName()))
            continue;

        /* Insert our own icon */
        if (iconname != null && "Hecl.png".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("Hecl.png"));
            FileInputStream inf = new FileInputStream(iconname);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        }
        /* Insert our own copy of the script file. */
        else if ("script.hcl".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("script.hcl"));
            FileInputStream inf = new FileInputStream(scriptfile);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        } else {
            /* Otherwise, just copy the entry. */
            jof.putNextEntry(entry);
            while ((read = jif.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
        }

        jof.closeEntry();
    }

    jof.flush();
    jof.close();
    jif.close();
}

From source file:com.googlecode.arit.systest.Application.java

public File getExplodedWAR() {
    if (explodedWAR == null) {
        File explodedDir = new File(tmpDir, "exploded");
        explodedDir.mkdir();/*from  w w w  . j a va 2s.c  o  m*/
        String file = url.getFile();
        explodedWAR = new File(explodedDir, file.substring(file.lastIndexOf('/')));
        try {
            InputStream in = url.openStream();
            try {
                JarInputStream jar = new JarInputStream(in);
                JarEntry jarEntry;
                while ((jarEntry = jar.getNextJarEntry()) != null) {
                    File dest = new File(explodedWAR, jarEntry.getName());
                    if (jarEntry.isDirectory()) {
                        dest.mkdir();
                    } else {
                        dest.getParentFile().mkdirs();
                        OutputStream out = new FileOutputStream(dest);
                        try {
                            IOUtils.copy(jar, out);
                        } finally {
                            out.close();
                        }
                    }
                }
            } finally {
                in.close();
            }
        } catch (IOException ex) {
            throw new SystestException("Failed to explode WAR", ex);
        }
    }
    return explodedWAR;
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.jar.JarUtils.java

/**
 * Dump the entries of a jar and return them as a String. This method can be
 * memory expensive depending on the jar size.
 * //from   w  w  w.ja v a 2  s  . com
 * @param resource
 * @return
 */
public static String dumpJarContent(Resource resource) {
    try {
        return dumpJarContent(new JarInputStream(resource.getInputStream()));
    } catch (IOException ex) {
        return "reading from stream failed" + ex;
    }
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.ManifestUtilsTest.java

public void testEmptyManifest() throws Exception {
    Manifest mf = new Manifest();
    mf.getMainAttributes().putValue("foo", "bar");
    createJar(mf);/*  w w w.j  a  v  a  2  s .co  m*/
    in = new JarInputStream(storage.getInputStream());
    assertEquals(mf, in.getManifest());
}

From source file:org.bimserver.plugins.JarClassLoader.java

public JarClassLoader(ClassLoader parentClassLoader, File jarFile) {
    super(parentClassLoader);
    this.jarFile = jarFile;
    try {//from   w  ww . j  av a  2s  .  com
        JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile));
        JarEntry entry = jarInputStream.getNextJarEntry();
        map = new HashMap<String, byte[]>();
        while (entry != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            IOUtils.copy(jarInputStream, byteArrayOutputStream);
            map.put(entry.getName(), byteArrayOutputStream.toByteArray());
            if (entry.getName().endsWith(".jar")) {
                loadSubJars(byteArrayOutputStream.toByteArray());
            }
            entry = jarInputStream.getNextJarEntry();
        }
        jarInputStream.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

From source file:com.cisco.step.jenkins.plugins.jenkow.test.eclipse.UpdateSiteTest.java

public void testUpdateSite() throws Exception {
    URL u = new URL(getURL(), "plugin/" + Consts.PLUGIN_NAME + "/eclipse.site/content.jar");
    Set<String> names = new HashSet<String>();
    JarInputStream jis = new JarInputStream(u.openStream());
    try {//from  w ww  .j  a  v  a  2 s.  c o  m
        while (true) {
            ZipEntry ze = jis.getNextEntry();
            if (ze == null)
                break;
            names.add(ze.getName());
        }
    } finally {
        IOUtils.closeQuietly(jis);
    }
    assertTrue(names.contains("content.xml"));
}

From source file:com.cisco.surf.jenkins.plugins.jenkow.test.eclipse.UpdateSiteTest.java

public void testUpdateSite() throws Exception {
    URL u = new URL(getURL(), "plugin/" + PLUGIN_NAME + "/eclipse.site/content.jar");
    Set<String> names = new HashSet<String>();
    JarInputStream jis = new JarInputStream(u.openStream());
    try {/* w w w .jav a2s  . c o m*/
        while (true) {
            ZipEntry ze = jis.getNextEntry();
            if (ze == null)
                break;
            names.add(ze.getName());
        }
    } finally {
        IOUtils.closeQuietly(jis);
    }
    assertTrue(names.contains("content.xml"));
}

From source file:org.apache.pluto.util.assemble.ear.EarAssembler.java

public void assembleInternal(AssemblerConfig config) throws UtilityException, IOException {

    File source = config.getSource();
    File dest = config.getDestination();

    JarInputStream earIn = new JarInputStream(new FileInputStream(source));
    JarOutputStream earOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(dest), BUFLEN));

    try {/*w  w w. j av a2 s. c  o  m*/

        JarEntry entry;

        // Iterate over entries in the EAR archive
        while ((entry = earIn.getNextJarEntry()) != null) {

            // If a war file is encountered, assemble it into a
            // ByteArrayOutputStream and write the assembled bytes
            // back to the EAR archive.
            if (entry.getName().toLowerCase().endsWith(".war")) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Assembling war file " + entry.getName());
                }

                // keep a handle to the AssemblySink so we can write out
                // JarEntry metadata and the bytes later.
                AssemblySink warBytesOut = getAssemblySink(config, entry);
                JarOutputStream warOut = new JarOutputStream(warBytesOut);

                JarStreamingAssembly.assembleStream(new JarInputStream(earIn), warOut,
                        config.getDispatchServletClass());

                JarEntry warEntry = new JarEntry(entry);

                // Write out the assembled JarEntry metadata
                warEntry.setSize(warBytesOut.getByteCount());
                warEntry.setCrc(warBytesOut.getCrc());
                warEntry.setCompressedSize(-1);
                earOut.putNextEntry(warEntry);

                // Write out the assembled WAR file to the EAR
                warBytesOut.writeTo(earOut);

                earOut.flush();
                earOut.closeEntry();
                earIn.closeEntry();

            } else {

                earOut.putNextEntry(entry);
                IOUtils.copy(earIn, earOut);

                earOut.flush();
                earOut.closeEntry();
                earIn.closeEntry();

            }
        }

    } finally {

        earOut.close();
        earIn.close();

    }
}

From source file:org.bimserver.plugins.classloaders.JarClassLoader.java

public JarClassLoader(ClassLoader parentClassLoader, File jarFile) throws FileNotFoundException, IOException {
    super(parentClassLoader);
    this.jarFile = jarFile;
    JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile));
    JarEntry entry = jarInputStream.getNextJarEntry();
    while (entry != null) {
        if (entry.getName().endsWith(".jar")) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            IOUtils.copy(jarInputStream, byteArrayOutputStream);

            // Not storing the original JAR, so future code will be unable to read the original
            loadSubJars(byteArrayOutputStream.toByteArray());
        } else {/*from  w  ww. ja v a2s. c om*/
            // Files are being stored deflated in memory because most of the time a lot of files are not being used (or the complete plugin is not being used)
            addDataToMap(jarInputStream, entry);
        }
        entry = jarInputStream.getNextJarEntry();
    }
    jarInputStream.close();
}

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);/*from w ww .  ja  v  a  2s.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();
}