Search in sources :

Example 16 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 17 with JarInputStream

use of java.util.jar.JarInputStream in project MinecraftForge by MinecraftForge.

the class ClassPatchManager method setup.

public void setup(Side side) {
    Pattern binpatchMatcher = Pattern.compile(String.format("binpatch/%s/.*.binpatch", side.toString().toLowerCase(Locale.ENGLISH)));
    JarInputStream jis;
    try {
        InputStream binpatchesCompressed = getClass().getResourceAsStream("/binpatches.pack.lzma");
        if (binpatchesCompressed == null) {
            FMLRelaunchLog.log(Level.ERROR, "The binary patch set is missing. Either you are in a development environment, or things are not going to work!");
            return;
        }
        LzmaInputStream binpatchesDecompressed = new LzmaInputStream(binpatchesCompressed);
        ByteArrayOutputStream jarBytes = new ByteArrayOutputStream();
        JarOutputStream jos = new JarOutputStream(jarBytes);
        Pack200.newUnpacker().unpack(binpatchesDecompressed, jos);
        jis = new JarInputStream(new ByteArrayInputStream(jarBytes.toByteArray()));
    } catch (Exception e) {
        FMLRelaunchLog.log(Level.ERROR, e, "Error occurred reading binary patches. Expect severe problems!");
        throw Throwables.propagate(e);
    }
    patches = ArrayListMultimap.create();
    do {
        try {
            JarEntry entry = jis.getNextJarEntry();
            if (entry == null) {
                break;
            }
            if (binpatchMatcher.matcher(entry.getName()).matches()) {
                ClassPatch cp = readPatch(entry, jis);
                if (cp != null) {
                    patches.put(cp.sourceClassName, cp);
                }
            } else {
                jis.closeEntry();
            }
        } catch (IOException e) {
        }
    } while (true);
    FMLRelaunchLog.fine("Read %d binary patches", patches.size());
    if (DEBUG)
        FMLRelaunchLog.fine("Patch list :\n\t%s", Joiner.on("\t\n").join(patches.asMap().entrySet()));
    patchedClasses.clear();
}
Also used : Pattern(java.util.regex.Pattern) LzmaInputStream(LZMA.LzmaInputStream) JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) LzmaInputStream(LZMA.LzmaInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException)

Example 18 with JarInputStream

use of java.util.jar.JarInputStream in project buck by facebook.

the class HashingDeterministicJarWriterTest method entriesAreWrittenAsTheyAreEncounteredWithManifestLast.

@Test
public void entriesAreWrittenAsTheyAreEncounteredWithManifestLast() throws IOException {
    writer.writeEntry("Z", new ByteArrayInputStream("Z's contents".getBytes(StandardCharsets.UTF_8)));
    writer.writeEntry("A", new ByteArrayInputStream("A's contents".getBytes(StandardCharsets.UTF_8)));
    writer.close();
    try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
        JarEntry entry;
        entry = jar.getNextJarEntry();
        assertEquals("Z", entry.getName());
        entry = jar.getNextJarEntry();
        assertEquals("A", entry.getName());
        entry = jar.getNextJarEntry();
        assertEquals(JarFile.MANIFEST_NAME, entry.getName());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Example 19 with JarInputStream

use of java.util.jar.JarInputStream in project buck by facebook.

the class FakeProjectFilesystem method getJarManifest.

@Override
public Manifest getJarManifest(Path path) throws IOException {
    try (JarInputStream jar = new JarInputStream(newFileInputStream(path))) {
        Manifest result = jar.getManifest();
        if (result != null) {
            return result;
        }
        // JarInputStream will only find the manifest if it's the first entry, but we have code that
        // puts it elsewhere. We must search. Fortunately, this is test code! So we can be slow!
        JarEntry entry;
        while ((entry = jar.getNextJarEntry()) != null) {
            if (JarFile.MANIFEST_NAME.equals(entry.getName())) {
                result = new Manifest();
                result.read(jar);
                return result;
            }
        }
    }
    return null;
}
Also used : JarInputStream(java.util.jar.JarInputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry)

Example 20 with JarInputStream

use of java.util.jar.JarInputStream in project mybatis-3 by mybatis.

the class DefaultVFS method list.

@Override
public List<String> list(URL url, String path) throws IOException {
    InputStream is = null;
    try {
        List<String> resources = new ArrayList<String>();
        // First, try to find the URL of a JAR file containing the requested resource. If a JAR
        // file is found, then we'll list child resources by reading the JAR.
        URL jarUrl = findJarForResource(url);
        if (jarUrl != null) {
            is = jarUrl.openStream();
            if (log.isDebugEnabled()) {
                log.debug("Listing " + url);
            }
            resources = listResources(new JarInputStream(is), path);
        } else {
            List<String> children = new ArrayList<String>();
            try {
                if (isJar(url)) {
                    // Some versions of JBoss VFS might give a JAR stream even if the resource
                    // referenced by the URL isn't actually a JAR
                    is = url.openStream();
                    JarInputStream jarInput = new JarInputStream(is);
                    if (log.isDebugEnabled()) {
                        log.debug("Listing " + url);
                    }
                    for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null; ) {
                        if (log.isDebugEnabled()) {
                            log.debug("Jar entry: " + entry.getName());
                        }
                        children.add(entry.getName());
                    }
                    jarInput.close();
                } else {
                    /*
             * Some servlet containers allow reading from directory resources like a
             * text file, listing the child resources one per line. However, there is no
             * way to differentiate between directory and file resources just by reading
             * them. To work around that, as each line is read, try to look it up via
             * the class loader as a child of the current resource. If any line fails
             * then we assume the current resource is not a directory.
             */
                    is = url.openStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    List<String> lines = new ArrayList<String>();
                    for (String line; (line = reader.readLine()) != null; ) {
                        if (log.isDebugEnabled()) {
                            log.debug("Reader entry: " + line);
                        }
                        lines.add(line);
                        if (getResources(path + "/" + line).isEmpty()) {
                            lines.clear();
                            break;
                        }
                    }
                    if (!lines.isEmpty()) {
                        if (log.isDebugEnabled()) {
                            log.debug("Listing " + url);
                        }
                        children.addAll(lines);
                    }
                }
            } catch (FileNotFoundException e) {
                /*
           * For file URLs the openStream() call might fail, depending on the servlet
           * container, because directories can't be opened for reading. If that happens,
           * then list the directory directly instead.
           */
                if ("file".equals(url.getProtocol())) {
                    File file = new File(url.getFile());
                    if (log.isDebugEnabled()) {
                        log.debug("Listing directory " + file.getAbsolutePath());
                    }
                    if (file.isDirectory()) {
                        if (log.isDebugEnabled()) {
                            log.debug("Listing " + url);
                        }
                        children = Arrays.asList(file.list());
                    }
                } else {
                    // No idea where the exception came from so rethrow it
                    throw e;
                }
            }
            // The URL prefix to use when recursively listing child resources
            String prefix = url.toExternalForm();
            if (!prefix.endsWith("/")) {
                prefix = prefix + "/";
            }
            // Iterate over immediate children, adding files and recursing into directories
            for (String child : children) {
                String resourcePath = path + "/" + child;
                resources.add(resourcePath);
                URL childUrl = new URL(prefix + child);
                resources.addAll(list(childUrl, resourcePath));
            }
        }
        return resources;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            // Ignore
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) JarEntry(java.util.jar.JarEntry) File(java.io.File) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

JarInputStream (java.util.jar.JarInputStream)186 JarEntry (java.util.jar.JarEntry)83 IOException (java.io.IOException)73 FileInputStream (java.io.FileInputStream)67 Manifest (java.util.jar.Manifest)56 File (java.io.File)48 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)34 JarOutputStream (java.util.jar.JarOutputStream)29 Test (org.junit.Test)29 FileOutputStream (java.io.FileOutputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)24 URL (java.net.URL)20 ArrayList (java.util.ArrayList)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 OutputStream (java.io.OutputStream)14 JarFile (java.util.jar.JarFile)14 BufferedInputStream (java.io.BufferedInputStream)11 Attributes (java.util.jar.Attributes)11 HashSet (java.util.HashSet)9