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.apache.maven.plugin.javadoc.JavadocUtil.java

/**
 * @param jarFile not null// ww  w. j a v a 2s.  c om
 * @return all class names from the given jar file.
 * @throws IOException if any or if the jarFile is null or doesn't exist.
 */
private static List<String> getClassNamesFromJar(File jarFile) throws IOException {
    if (jarFile == null || !jarFile.exists() || !jarFile.isFile()) {
        throw new IOException("The jar '" + jarFile + "' doesn't exist or is not a file.");
    }

    List<String> classes = new ArrayList<String>();
    JarInputStream jarStream = null;

    try {
        jarStream = new JarInputStream(new FileInputStream(jarFile));
        JarEntry jarEntry = jarStream.getNextJarEntry();
        while (jarEntry != null) {
            if (jarEntry.getName().toLowerCase(Locale.ENGLISH).endsWith(".class")) {
                String name = jarEntry.getName().substring(0, jarEntry.getName().indexOf("."));

                classes.add(name.replaceAll("/", "\\."));
            }

            jarStream.closeEntry();
            jarEntry = jarStream.getNextJarEntry();
        }
    } finally {
        IOUtil.close(jarStream);
    }

    return classes;
}

From source file:org.schemaspy.Config.java

public static Set<String> getBuiltInDatabaseTypes(String loadedFromJar) {
    Set<String> databaseTypes = new TreeSet<>();
    JarInputStream jar = null;/*from  w w w.jav a  2s .  co m*/

    try {
        jar = new JarInputStream(new FileInputStream(loadedFromJar));
        JarEntry entry;

        while ((entry = jar.getNextJarEntry()) != null) {
            String entryName = entry.getName();
            if (entryName.indexOf("types") != -1) {
                int dotPropsIndex = entryName.indexOf(".properties");
                if (dotPropsIndex != -1)
                    databaseTypes.add(entryName.substring(0, dotPropsIndex));
            }
        }
    } catch (IOException exc) {
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException ignore) {
            }
        }
    }

    return databaseTypes;
}

From source file:com.facebook.buck.jvm.java.DefaultJavaLibraryIntegrationTest.java

@Test
public void shouldIncludeUserSuppliedManifestIfProvided() throws IOException {
    setUpProjectWorkspaceForScenario("manifest");

    Manifest m = new Manifest();
    Attributes attrs = new Attributes();
    attrs.putValue("Data", "cheese");
    m.getEntries().put("Example", attrs);
    m.write(System.out);/*from   ww  w. j  a v a 2  s.  com*/

    Path path = workspace.buildAndReturnOutput("//:library");

    try (InputStream is = Files.newInputStream(path); JarInputStream jis = new JarInputStream(is)) {
        Manifest manifest = jis.getManifest();
        String value = manifest.getEntries().get("Example").getValue("Data");
        assertEquals("cheese", value);
    }
}

From source file:test.BuilderTest.java

public static void testNoManifest() throws Exception {
    Builder b = new Builder();
    try {/*from w  ww.  j  ava2  s  . c  o  m*/
        b.setProperty("-nomanifest", "true");
        b.setProperty(Constants.BUNDLE_CLASSPATH, "WEB-INF/classes");
        b.setProperty("Include-Resource", "WEB-INF/classes=@jar/asm.jar");
        Jar jar = b.build();
        assertTrue(b.check());

        File f = new File("tmp.jar");
        f.deleteOnExit();
        jar.write(f);

        JarInputStream jin = new JarInputStream(new FileInputStream(f));
        Manifest m = jin.getManifest();
        assertNull(m);
    } finally {
        b.close();
    }
}

From source file:org.allcolor.yahp.converter.CClassLoader.java

/**
 * analyse the content of the given jar file
 * // w  w  w.j a v  a2  s. co m
 * @param jarFile
 *            the jar to analise
 */
private final void readDirectories(final URL jarFile) {
    JarInputStream jarIn = null;

    try {
        if (!jarFile.getPath().endsWith(".jar")) {
            return;
        }
        if (CClassLoader.sl(CClassLoader.DEBUG)) {
            CClassLoader.log("opening jar : " + jarFile.toExternalForm(), CClassLoader.DEBUG);
        }
        jarIn = new JarInputStream(jarFile.openStream());
        JarEntry jarEntry = null;

        while ((jarEntry = jarIn.getNextJarEntry()) != null) {
            if (jarEntry.isDirectory()) {
                continue;
            }

            final URL url = new URL("yahpjarloader://"
                    + CBASE64Codec.encode(jarFile.toExternalForm().getBytes("utf-8")).replaceAll("\n", "") + "/"
                    + jarEntry.getName());

            if (CClassLoader.sl(CClassLoader.DEBUG)) {
                CClassLoader.log("found entry : " + url.toString(), CClassLoader.DEBUG);
            }

            if (jarEntry.getName().endsWith(".class")) {
                if (!this.classesMap.containsKey(jarEntry.getName())) {
                    if (!this.booResourceOnly) {
                        this.classesMap.put(jarEntry.getName(), url);
                    }
                }

                if (this.resourcesMap.containsKey(jarEntry.getName())) {
                    final Object to = this.resourcesMap.get(jarEntry.getName());
                    if (to instanceof URL) {
                        final URL uo = (URL) to;
                        final List l = new ArrayList();
                        l.add(uo);
                        l.add(url);
                        this.resourcesMap.put(jarEntry.getName(), l);
                    } else if (to instanceof List) {
                        final List uo = (List) to;
                        uo.add(url);
                        this.resourcesMap.put(jarEntry.getName(), uo);
                    }
                } else {
                    this.resourcesMap.put(jarEntry.getName(), url);
                }
            } else if (jarEntry.getName().startsWith("native/")) {
                String system = jarEntry.getName().substring(7);
                system = system.substring(0, system.indexOf('/'));
                if (!this.dllMap.containsKey(system)) {
                    this.dllMap.put(system, url);
                }
                if (this.resourcesMap.containsKey(jarEntry.getName())) {
                    final Object to = this.resourcesMap.get(jarEntry.getName());
                    if (to instanceof URL) {
                        final URL uo = (URL) to;
                        final List l = new ArrayList();
                        l.add(uo);
                        l.add(url);
                        this.resourcesMap.put(jarEntry.getName(), l);
                    } else if (to instanceof List) {
                        final List uo = (List) to;
                        uo.add(url);
                        this.resourcesMap.put(jarEntry.getName(), uo);
                    }
                } else {
                    this.resourcesMap.put(jarEntry.getName(), url);
                }
            } else {
                if (this.resourcesMap.containsKey(jarEntry.getName())) {
                    final Object to = this.resourcesMap.get(jarEntry.getName());
                    if (to instanceof URL) {
                        final URL uo = (URL) to;
                        final List l = new ArrayList();
                        l.add(uo);
                        l.add(url);
                        this.resourcesMap.put(jarEntry.getName(), l);
                    } else if (to instanceof List) {
                        final List uo = (List) to;
                        uo.add(url);
                        this.resourcesMap.put(jarEntry.getName(), uo);
                    }
                } else {
                    this.resourcesMap.put(jarEntry.getName(), url);
                }
            }
        }

        if (CClassLoader.sl(CClassLoader.DEBUG)) {
            CClassLoader.log("opening jar : " + jarFile.getFile().toString() + " done.", CClassLoader.DEBUG);
        }
    } catch (final MalformedURLException mue) {
        mue.printStackTrace();

        if (CClassLoader.sl(CClassLoader.FATAL)) {
            CClassLoader.log(mue.getMessage(), CClassLoader.FATAL);
        }
    } catch (final IOException ioe) {
        ioe.printStackTrace();

        if (CClassLoader.sl(CClassLoader.FATAL)) {
            CClassLoader.log(ioe.getMessage(), CClassLoader.FATAL);
        }
    } catch (final Exception e) {
        e.printStackTrace();

        if (CClassLoader.sl(CClassLoader.FATAL)) {
            CClassLoader.log(e.getMessage(), CClassLoader.FATAL);
        }
    } finally {
        try {
            jarIn.close();
            jarIn = null;
        } catch (final Exception e) {
        }
    }
}

From source file:com.amalto.workbench.utils.Util.java

public static ByteArrayOutputStream retrieveJarEntryAsBytes(File barFile, String entryName) {
    JarInputStream jarIn = null;/*w ww .j  a  v  a 2  s. c o m*/
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();

    try {
        jarIn = new JarInputStream(new FileInputStream(barFile));
        JarEntry entry;
        byte[] buf = new byte[4096];
        while ((entry = jarIn.getNextJarEntry()) != null) {
            if (entry.toString().equals(entryName)) {
                int read;
                while ((read = jarIn.read(buf, 0, 4096)) != -1) {
                    outBytes.write(buf, 0, read);
                }
                if (outBytes.toByteArray().length > 0) {
                    return outBytes;
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}