Search in sources :

Example 1 with JarInputStream

use of java.util.jar.JarInputStream in project camel by apache.

the class DefaultPackageScanClassResolver method doLoadJarClassEntries.

/**
     * Loads all the class entries from the JAR.
     *
     * @param stream  the inputstream of the jar file to be examined for classes
     * @param urlPath the url of the jar file to be examined for classes
     * @return all the .class entries from the JAR
     */
protected List<String> doLoadJarClassEntries(InputStream stream, String urlPath) {
    List<String> entries = new ArrayList<String>();
    JarInputStream jarStream = null;
    try {
        jarStream = new JarInputStream(stream);
        JarEntry entry;
        while ((entry = jarStream.getNextJarEntry()) != null) {
            String name = entry.getName();
            if (name != null) {
                name = name.trim();
                if (!entry.isDirectory() && name.endsWith(".class")) {
                    entries.add(name);
                }
            }
        }
    } catch (IOException ioe) {
        log.warn("Cannot search jar file '" + urlPath + " due to an IOException: " + ioe.getMessage(), ioe);
    } finally {
        IOHelper.close(jarStream, urlPath, log);
    }
    return entries;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry)

Example 2 with JarInputStream

use of java.util.jar.JarInputStream in project hbase by apache.

the class ClassFinder method findClassesFromJar.

private Set<Class<?>> findClassesFromJar(String jarFileName, String packageName, boolean proceedOnExceptions) throws IOException, ClassNotFoundException, LinkageError {
    JarInputStream jarFile = null;
    try {
        jarFile = new JarInputStream(new FileInputStream(jarFileName));
    } catch (IOException ioEx) {
        LOG.warn("Failed to look for classes in " + jarFileName + ": " + ioEx);
        throw ioEx;
    }
    Set<Class<?>> classes = new HashSet<>();
    JarEntry entry = null;
    try {
        while (true) {
            try {
                entry = jarFile.getNextJarEntry();
            } catch (IOException ioEx) {
                if (!proceedOnExceptions) {
                    throw ioEx;
                }
                LOG.warn("Failed to get next entry from " + jarFileName + ": " + ioEx);
                break;
            }
            if (entry == null) {
                // loop termination condition
                break;
            }
            String className = entry.getName();
            if (!className.endsWith(CLASS_EXT)) {
                continue;
            }
            int ix = className.lastIndexOf('/');
            String fileName = (ix >= 0) ? className.substring(ix + 1) : className;
            if (null != this.fileNameFilter && !this.fileNameFilter.isCandidateFile(fileName, className)) {
                continue;
            }
            className = className.substring(0, className.length() - CLASS_EXT.length()).replace('/', '.');
            if (!className.startsWith(packageName)) {
                continue;
            }
            Class<?> c = makeClass(className, proceedOnExceptions);
            if (c != null) {
                if (!classes.add(c)) {
                    LOG.warn("Ignoring duplicate class " + className);
                }
            }
        }
        return classes;
    } finally {
        jarFile.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Example 3 with JarInputStream

use of java.util.jar.JarInputStream in project tomcat by apache.

the class JarWarResource method getJarInputStreamWrapper.

@Override
protected JarInputStreamWrapper getJarInputStreamWrapper() {
    JarFile warFile = null;
    JarInputStream jarIs = null;
    JarEntry entry = null;
    try {
        warFile = getArchiveResourceSet().openJarFile();
        JarEntry jarFileInWar = warFile.getJarEntry(archivePath);
        InputStream isInWar = warFile.getInputStream(jarFileInWar);
        jarIs = new JarInputStream(isInWar);
        entry = jarIs.getNextJarEntry();
        while (entry != null && !entry.getName().equals(getResource().getName())) {
            entry = jarIs.getNextJarEntry();
        }
        if (entry == null) {
            return null;
        }
        return new JarInputStreamWrapper(entry, jarIs);
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("jarResource.getInputStreamFail", getResource().getName(), getBaseUrl()), e);
        }
        return null;
    } finally {
        if (entry == null) {
            if (jarIs != null) {
                try {
                    jarIs.close();
                } catch (IOException ioe) {
                // Ignore
                }
            }
            if (warFile != null) {
                getArchiveResourceSet().closeJarFile();
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 4 with JarInputStream

use of java.util.jar.JarInputStream in project weave by continuuity.

the class WeaveLauncher method unJar.

private static void unJar(File jarFile, File targetDir) throws IOException {
    JarInputStream jarInput = new JarInputStream(new FileInputStream(jarFile));
    try {
        JarEntry jarEntry = jarInput.getNextJarEntry();
        while (jarEntry != null) {
            File target = new File(targetDir, jarEntry.getName());
            if (jarEntry.isDirectory()) {
                target.mkdirs();
            } else {
                target.getParentFile().mkdirs();
                copy(jarInput, target);
            }
            jarEntry = jarInput.getNextJarEntry();
        }
    } finally {
        jarInput.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with JarInputStream

use of java.util.jar.JarInputStream in project weave by continuuity.

the class ApplicationBundlerTest method unjar.

private void unjar(File jarFile, File targetDir) throws IOException {
    JarInputStream jarInput = new JarInputStream(new FileInputStream(jarFile));
    try {
        JarEntry jarEntry = jarInput.getNextJarEntry();
        while (jarEntry != null) {
            File target = new File(targetDir, jarEntry.getName());
            if (jarEntry.isDirectory()) {
                target.mkdirs();
            } else {
                target.getParentFile().mkdirs();
                ByteStreams.copy(jarInput, Files.newOutputStreamSupplier(target));
            }
            jarEntry = jarInput.getNextJarEntry();
        }
    } finally {
        jarInput.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

JarInputStream (java.util.jar.JarInputStream)154 JarEntry (java.util.jar.JarEntry)72 IOException (java.io.IOException)66 FileInputStream (java.io.FileInputStream)63 Manifest (java.util.jar.Manifest)50 File (java.io.File)46 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)29 JarOutputStream (java.util.jar.JarOutputStream)27 FileOutputStream (java.io.FileOutputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)22 URL (java.net.URL)20 Test (org.junit.Test)20 OutputStream (java.io.OutputStream)13 JarFile (java.util.jar.JarFile)13 BufferedInputStream (java.io.BufferedInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ArrayList (java.util.ArrayList)10 Attributes (java.util.jar.Attributes)10 HashSet (java.util.HashSet)8