Example usage for java.util.jar JarFile close

List of usage examples for java.util.jar JarFile close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP file.

Usage

From source file:org.spoutcraft.launcher.launch.MinecraftClassLoader.java

private void index(File file) throws IOException {
    JarFile jar = null;
    try {//  www.  j a v  a2s .c o  m
        jar = new JarFile(file);
        Enumeration<JarEntry> i = jar.entries();
        while (i.hasMoreElements()) {
            JarEntry entry = i.nextElement();
            if (entry.getName().endsWith(".class")) {
                String name = entry.getName();
                name = name.replace("/", ".").substring(0, name.length() - 6);
                classLocations.put(name, file);
            }
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException ignore) {
            }
        }
    }
}

From source file:hudson.ClassicPluginStrategy.java

@Override
public String getShortName(File archive) throws IOException {
    Manifest manifest;//from  www .  j  av  a 2  s .c  o m
    if (isLinked(archive)) {
        manifest = loadLinkedManifest(archive);
    } else {
        JarFile jf = new JarFile(archive, false);
        try {
            manifest = jf.getManifest();
        } finally {
            jf.close();
        }
    }
    return PluginWrapper.computeShortName(manifest, archive.getName());
}

From source file:org.pepstock.jem.util.ReverseURLClassLoader.java

/**
 * Copies the resource input stream in a byte array. In this way I can close the input stream of the resource.
 * @param jFile jar file to be closed after copying
 * @param resourceIS input stream of the resource inside the jar
 * @param baos byte output stream used to copy the bytes of the resource
 * @return returns a byte array with the resource
 *///  www  . jav  a 2s .c o  m
private ByteArrayInputStream copy(JarFile jFile, InputStream resourceIS, ByteArrayOutputStream baos) {
    try {
        // copies to bytes array
        IOUtils.copy(resourceIS, baos);
        // closes jar input stream
        IOUtils.closeQuietly(resourceIS);
        IOUtils.closeQuietly(baos);
        // creates an input stream of bytes
        return new ByteArrayInputStream(baos.toByteArray());
    } catch (IOException e) {
        // ignore
        LogAppl.getInstance().ignore(e.getMessage(), e);
    } finally {
        // close always the jar file
        try {
            jFile.close();
        } catch (IOException e) {
            LogAppl.getInstance().ignore(e.getMessage(), e);
        }
    }
    return null;
}

From source file:org.hyperic.snmp.MIBTree.java

public boolean parse(File file, String[] accept) throws IOException {
    if (hasParsedFile(file)) {
        return true;
    }/*from  ww w  . java2 s.  c  o m*/

    if (file.isDirectory()) {
        File[] mibs = file.listFiles();

        if ((mibs == null) || (mibs.length == 0)) {
            log.debug("No MIBs in directory: " + file);

            return false;
        }

        AcceptFilter filter = new AcceptFilter(accept);

        log.debug("Loading MIBs in directory: " + file);

        for (int i = 0; i < mibs.length; i++) {
            File mib = mibs[i];

            if (mib.isDirectory()) {
                continue;
            }

            if (!filter.accept(mib.getName())) {
                continue;
            }

            parseFile(mib);
        }

        return true;
    } else if (file.getName().endsWith(".jar")) {
        JarFile jar = new JarFile(file);

        try {
            return parse(jar, accept);
        } finally {
            jar.close();
        }
    } else {
        return parseFile(file);
    }
}

From source file:com.taobao.android.builder.tools.asm.ClazzBasicHandler.java

public void execute() throws IOException {

    logger.info("[ClazzReplacer] rewriteJar from " + jar.getAbsolutePath() + " to " + outJar.getAbsolutePath());

    JarFile jarFile = new JarFile(jar);

    JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(outJar)));

    Enumeration<JarEntry> jarFileEntries = jarFile.entries();

    while (jarFileEntries.hasMoreElements()) {

        JarEntry ze = jarFileEntries.nextElement();

        String pathName = ze.getName();

        logger.info(jar.getAbsolutePath() + "->" + pathName);

        if (!pathName.endsWith(".class")) {
            justCopy(jarFile, jos, ze, pathName);
            continue;
        }/*from w ww .  java 2s  .  c  o  m*/

        handleClazz(jarFile, jos, ze, pathName);

    }

    jarFile.close();
    //        IOUtils.closeQuietly(fileOutputStream);
    IOUtils.closeQuietly(jos);
}

From source file:org.apache.bcel.BCELBenchmark.java

@Benchmark
public void generator(Blackhole bh) throws IOException {
    JarFile jar = getJarFile();

    for (JarEntry entry : getClasses(jar)) {
        byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));

        JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse();

        ClassGen cg = new ClassGen(clazz);

        for (Method m : cg.getMethods()) {
            MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool());
            InstructionList il = mg.getInstructionList();

            if (il != null) {
                mg.getInstructionList().setPositions();
                mg.setMaxLocals();/*from w w  w . ja  v  a 2  s.  c  o m*/
                mg.setMaxStack();
            }
            cg.replaceMethod(m, mg.getMethod());
        }

        bh.consume(cg.getJavaClass().getBytes());
    }

    jar.close();
}

From source file:org.jvnet.hudson.update_center.LocalHPI.java

@Override
public File resolvePOM() throws IOException {
    JarFile jar = null;
    InputStream in = null;/*from  w ww  .  j a va2s.c  o m*/
    OutputStream out = null;
    try {
        jar = new JarFile(jarFile);
        String pomPath = String.format("META-INF/maven/%s/%s/pom.xml", artifact.groupId, artifact.artifactId);
        ZipEntry e = jar.getEntry(pomPath);
        if (e == null) {
            return null;
        }

        File temporaryFile = File.createTempFile(artifact.artifactId, ".xml");
        temporaryFile.deleteOnExit();

        in = jar.getInputStream(e);
        out = new FileOutputStream(temporaryFile);

        IOUtils.copy(in, out);

        return temporaryFile;
    } finally {
        if (out != null) {
            out.close();
        }
        if (in != null) {
            in.close();
        }
        if (jar != null) {
            jar.close();
        }
    }
}

From source file:com.iflytek.edu.cloud.frame.doc.ServiceDocBuilder.java

private void doServiceJavaSource(String jarFileUrl) throws IOException {
    JarFile jarFile = new JarFile(jarFileUrl);
    try {/*from www  .java2 s  .  com*/
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
        }

        for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
            JarEntry entry = entries.nextElement();

            String entryPath = entry.getName();
            if (entryPath.endsWith(".java")) {
                InputStream inputStream = jarFile.getInputStream(entry);
                builder.addSource(new InputStreamReader(inputStream, "UTF-8"));
            }
        }
    } finally {
        jarFile.close();
    }
}

From source file:org.commonjava.web.test.fixture.JarKnockouts.java

public void rewriteJar(final File source, final File targetDir) throws IOException {
    targetDir.mkdirs();/*from w ww.j  a  va  2 s  .co  m*/
    final File target = new File(targetDir, source.getName());

    JarFile in = null;
    JarOutputStream out = null;
    try {
        in = new JarFile(source);

        final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target));
        out = new JarOutputStream(fos, in.getManifest());

        final Enumeration<JarEntry> entries = in.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!knockout(entry.getName())) {
                final InputStream stream = in.getInputStream(entry);
                out.putNextEntry(entry);
                copy(stream, out);
                out.closeEntry();
            }
        }
    } finally {
        closeQuietly(out);
        if (in != null) {
            try {
                in.close();
            } catch (final IOException e) {
            }
        }
    }
}

From source file:rapture.plugin.app.JarBasedSandboxLoader.java

private String[] getResourceFiles(String path, String variant) throws Exception {
    URL dirURL = SelfInstaller.class.getClass().getResource(path);
    String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); // strip out only the JAR file
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
    Enumeration<JarEntry> entries = jar.entries(); // gives ALL entries in
                                                   // jar
    Set<String> result = new HashSet<String>(); // avoid duplicates in case
                                                // it is a subdirectory
    String checkPath = path.substring(1);
    String checkVariant = variant != null ? checkPath.replaceFirst(PluginSandbox.CONTENT, variant).concat("/")
            : null;/*from   ww  w  .  j a v a  2s .  c  om*/
    while (entries.hasMoreElements()) {
        String name = entries.nextElement().getName();
        if (!name.endsWith("/") && // don't return directories
                (name.startsWith(checkPath) || (checkVariant != null && name.startsWith(checkVariant)))) { // filter according to the path
            result.add(name);
        }
    }
    jar.close();
    return result.toArray(new String[result.size()]);

}