Example usage for java.util.jar JarFile getInputStream

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

Introduction

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

Prototype

public synchronized InputStream getInputStream(ZipEntry ze) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:de.micromata.tpsb.doc.sources.JarSourceFileRepository.java

private String getContents(JarFile jarFile, JarEntry jarEntry) {
    try {/*www .j  a va 2s. c om*/

        InputStream is = jarFile.getInputStream(jarEntry);
        BufferedInputStream stream = new BufferedInputStream(is);
        int bytesRead = 0;
        StringBuilder sb = new StringBuilder();
        while ((bytesRead = stream.read(buffer)) != -1) {
            String chunk = new String(buffer, 0, bytesRead, Charset.forName(CharEncoding.UTF_8));
            sb.append(chunk);
        }
        return sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.katsu.dwm.web.Loader.java

private void copyContent(File f, JarEntry je) {
    File rootPath = JarUtils.getRootApplicationContentPath();
    File temp = new File(rootPath, je.getName());
    if (je.isDirectory()) {
        logger.debug("Create path: " + temp.mkdirs() + " " + temp.getPath());
    } else {/*  w w w. j av  a  2  s  .com*/
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            JarFile jf = new JarFile(f);
            is = jf.getInputStream(je);
            if (!temp.exists()) {
                temp.createNewFile();
            }
            fos = new FileOutputStream(temp);
            byte[] array = new byte[1024];
            int readed;
            while ((readed = is.read(array)) != -1) {
                fos.write(array, 0, readed);
            }
        } catch (Exception e) {
            logger.error(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException ex) {
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException ex) {
            }
        }
    }
}

From source file:org.jahia.utils.PomUtils.java

public static File extractPomFromJar(JarFile jar, String groupId, String artifactId) throws IOException {
    // deploy artifacts to Maven distribution server
    Enumeration<JarEntry> jarEntries = jar.entries();
    JarEntry jarEntry = null;//  w w w  .j  av  a2 s  .  c  om
    boolean found = false;
    while (jarEntries.hasMoreElements()) {
        jarEntry = jarEntries.nextElement();
        String name = jarEntry.getName();
        if (StringUtils.startsWith(name,
                groupId != null ? ("META-INF/maven/" + groupId + "/") : "META-INF/maven/")
                && StringUtils.endsWith(name, artifactId + "/pom.xml")) {
            found = true;
            break;
        }
    }
    if (!found) {
        throw new IOException("unable to find pom.xml file within while looking for " + artifactId);
    }
    InputStream is = jar.getInputStream(jarEntry);
    File pomFile = File.createTempFile("pom", ".xml");
    FileUtils.copyInputStreamToFile(is, pomFile);
    return pomFile;
}

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

/**
 * Baseline benchmark. Read the classes but don't parse them.
 *//*ww  w  .  j  av  a  2 s  . c o m*/
@Benchmark
public void baseline(Blackhole bh) throws IOException {
    JarFile jar = getJarFile();

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

    jar.close();
}

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

@Benchmark
public void parser(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();
        bh.consume(clazz);/* w  ww  .  j  ava2s  .  c om*/
    }

    jar.close();
}

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();/* ww  w. j av  a 2 s  .c om*/
                mg.setMaxStack();
            }
            cg.replaceMethod(m, mg.getMethod());
        }

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

    jar.close();
}

From source file:org.springframework.extensions.config.source.JarConfigSource.java

@Override
protected InputStream getInputStream(String sourceString) {
    InputStream in = null;/*from  w ww  .ja  v a2  s.  c o  m*/

    try {
        // make sure the source string is valid
        if (sourceString.startsWith(JAR_PROTOCOL) == false) {
            throw new IllegalArgumentException("sourceString must start with \"" + JAR_PROTOCOL + ":\"");
        }

        int indexSep = sourceString.indexOf(JAR_PATH_SEPARATOR);
        if (indexSep == -1) {
            throw new IllegalArgumentException(
                    "sourceString must contain an entry within the JAR file i.e. jar:file:/[file]!/[entry]");
        }

        // extract the entry part from source string
        String entryStr = sourceString.substring(indexSep + JAR_PATH_SEPARATOR.length());

        // open the JAR file
        URL url = new URL(sourceString);
        URLConnection conn = url.openConnection();
        if (conn instanceof JarURLConnection) {
            // open the jar file, if it does not contain the entry requested a FileNotFound exception
            // is thrown and reported below
            JarFile jar = ((JarURLConnection) conn).getJarFile();
            ZipEntry entry = jar.getEntry(entryStr);
            if (entry != null) {
                in = jar.getInputStream(entry);
            }
        }
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.debug("Failed to obtain input stream to URL: " + sourceString, e);
    }

    return in;
}

From source file:rubah.runtime.classloader.VersionLoader.java

@Override
public byte[] getResource(String resourceName) throws IOException {
    if (this.versionJar == null) {
        return super.getResource(resourceName);
    }// w w w .  jav  a  2  s  .  c om

    JarFile jarFile = this.versionJar;

    ZipEntry ze = jarFile.getEntry(resourceName);
    if (ze == null)
        throw new FileNotFoundException(resourceName);
    return IOUtils.toByteArray(jarFile.getInputStream(ze));
}

From source file:com.smartitengineering.util.simple.reflection.DefaultClassScannerImpl.java

private ClassReader getClassReader(JarFile jarFile, JarEntry entry) {
    InputStream is = null;//from   w  w w  .j av  a 2 s .  c om
    try {
        is = jarFile.getInputStream(entry);
        ClassReader cr = new ClassReader(is);
        return cr;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ex) {
        }
    }
}

From source file:framework.JarResourceStore.java

public synchronized byte[] read(String pResourceName) {
    JarFile jar = null;
    try {//w  w w  .j  a  v  a 2s. c om
        jar = new JarFile(URLDecoder.decode(filename, "utf-8"));
        JarEntry jarEntry = jar.getJarEntry(pResourceName);
        if (jarEntry != null) {

            InputStream inputStream = jar.getInputStream(jarEntry);
            try {
                return IOUtils.toByteArray(inputStream);
            } finally {
                inputStream.close();
            }
        }
    } catch (IOException e) {
        Loggers.RELOADER.error(e.getMessage(), e);
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException e2) {
                Loggers.RELOADER.error(e2.getMessage(), e2);
            }
        }
    }
    return null;
}