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.bimserver.plugins.classloaders.MemoryJarClassLoader.java

public MemoryJarClassLoader(ClassLoader parentClassLoader, File jarFile)
        throws FileNotFoundException, IOException {
    super(parentClassLoader);
    this.jarFile = jarFile;
    JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile));
    JarEntry entry = jarInputStream.getNextJarEntry();
    while (entry != null) {
        if (entry.getName().endsWith(".jar")) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            IOUtils.copy(jarInputStream, byteArrayOutputStream);

            // Not storing the original JAR, so future code will be unable to read the original
            loadSubJars(byteArrayOutputStream.toByteArray());
        } else {//  ww  w  .  j  a v a 2 s  .  c  o  m
            // Files are being stored deflated in memory because most of the time a lot of files are not being used (or the complete plugin is not being used)
            addDataToMap(jarInputStream, entry);
        }
        entry = jarInputStream.getNextJarEntry();
    }
    jarInputStream.close();
}

From source file:gemlite.core.internal.support.hotdeploy.scanner.ScannerIterator.java

public ScannerIterator(URL url) throws IOException, URISyntaxException {
    if (url.getProtocol().equals("http") || url.getFile().endsWith(".jar")) {
        jarFile = true;//from   www  .j a  v a2s. c  om
        jarInputStream = new JarInputStream(url.openStream());
    } else {
        Collection<File> colls = FileUtils.listFiles(new File(url.toURI()),
                new String[] { class_suffix.substring(1) }, true);
        classFileIterator = colls.iterator();
        classpathLen = url.getFile().length();
    }

}

From source file:org.pentaho.webpackage.deployer.WebPackageURLConnectionTest.java

@Test
public void testClosingStream() throws IOException {
    WebPackageURLConnection connection = new WebPackageURLConnection(
            new File("src/test/resources/my-simple-module-1.4.0.zip").toURI().toURL());
    connection.connect();// w ww  . j a v  a2  s  . c  o  m

    InputStream inputStream = connection.getInputStream();
    JarInputStream jar = new JarInputStream(inputStream);
    jar.getManifest();
    jar.close();

    try {
        connection.transform_thread.get();
    } catch (Exception exception) {
        fail("Thread failed to execute transform() method: " + exception.getMessage());
    }
}

From source file:com.ikon.util.cl.BinaryClassLoader.java

/**
 * Create internal classes and resources cache
 *//*  w w  w .ja  v a2  s  . c  o m*/
private void createCache(byte[] buf) throws IOException {
    ByteArrayInputStream bais = null;
    JarInputStream jis = null;
    byte[] buffer = new byte[1024 * 4];

    try {
        bais = new ByteArrayInputStream(buf);
        jis = new JarInputStream(bais);
        Attributes attr = jis.getManifest().getMainAttributes();
        mainClassName = attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;

        for (JarEntry entry = null; (entry = jis.getNextJarEntry()) != null;) {
            String name = entry.getName();

            if (!entry.isDirectory()) {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

                for (int n = 0; -1 != (n = jis.read(buffer));) {
                    byteStream.write(buffer, 0, n);
                }

                if (name.endsWith(".class")) {
                    String className = name.substring(0, name.indexOf('.')).replace('/', '.');
                    resources.put(className, byteStream.toByteArray());
                } else {
                    resources.put(name, byteStream.toByteArray());
                }

                byteStream.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(bais);
    }
}

From source file:org.jahia.utils.maven.plugin.support.MavenAetherHelperUtils.java

public static boolean doesJarHavePackageName(File jarFile, String packageName, Log log) {
    JarInputStream jarInputStream = null;
    if (jarFile == null) {
        log.warn("File is null !");
        return false;
    }/*  ww w .  ja  va  2  s  .c  o  m*/
    if (!jarFile.exists()) {
        log.warn("File " + jarFile + " does not exist !");
        return false;
    }
    log.debug("Scanning JAR " + jarFile + "...");
    try {
        jarInputStream = new JarInputStream(new FileInputStream(jarFile));
        JarEntry jarEntry = null;
        while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
            String jarPackageName = jarEntry.getName().replaceAll("/", ".");
            if (jarPackageName.endsWith(".")) {
                jarPackageName = jarPackageName.substring(0, jarPackageName.length() - 1);
            }
            if (jarPackageName.equals(packageName)) {
                return true;
            }
        }
    } catch (IOException e) {
        log.error(e);
        ;
    } finally {
        IOUtils.closeQuietly(jarInputStream);
    }
    return false;
}

From source file:org.apache.sling.osgi.obr.Resource.java

public static Resource create(URL file) throws IOException {
    JarInputStream jar = null;//  w  ww  .j av  a  2 s . c o m
    try {
        URLConnection conn = file.openConnection();
        jar = new JarInputStream(conn.getInputStream());
        Manifest manifest = jar.getManifest();
        if (manifest == null) {
            throw new IOException(file + " is not a valid JAR file: Manifest not first entry");
        }
        return new Resource(file, manifest.getMainAttributes(), conn.getContentLength());
    } finally {
        IOUtils.closeQuietly(jar);
    }
}

From source file:org.pentaho.webpackage.deployer.archive.impl.WebPackageURLConnectionTest.java

@Test
public void testClosingStream() throws IOException {
    WebPackageURLConnection connection = new WebPackageURLConnection(
            getResourceUrl("/my-simple-module-1.4.0.zip"));
    connection.connect();//from   w  w  w  . j  av a2s . com

    InputStream inputStream = connection.getInputStream();
    JarInputStream jar = new JarInputStream(inputStream);
    jar.getManifest();
    jar.close();

    try {
        connection.transform_thread.get();
    } catch (Exception exception) {
        fail("Thread failed to execute transform() method: " + exception.getMessage());
    }
}

From source file:net.lightbody.bmp.proxy.jetty.util.JarResource.java

public static void extract(Resource resource, File directory, boolean deleteOnExit) throws IOException {
    if (log.isDebugEnabled())
        log.debug("Extract " + resource + " to " + directory);
    JarInputStream jin = new JarInputStream(resource.getInputStream());
    JarEntry entry = null;//from   www.  jav  a  2 s . co  m
    while ((entry = jin.getNextJarEntry()) != null) {
        File file = new File(directory, entry.getName());
        if (entry.isDirectory()) {
            // Make directory
            if (!file.exists())
                file.mkdirs();
        } else {
            // make directory (some jars don't list dirs)
            File dir = new File(file.getParent());
            if (!dir.exists())
                dir.mkdirs();

            // Make file
            FileOutputStream fout = null;
            try {
                fout = new FileOutputStream(file);
                IO.copy(jin, fout);
            } finally {
                IO.close(fout);
            }

            // touch the file.
            if (entry.getTime() >= 0)
                file.setLastModified(entry.getTime());
        }
        if (deleteOnExit)
            file.deleteOnExit();
    }
}

From source file:WarUtil.java

/**
 * WAR???????????/*  www .ja v  a 2 s .co  m*/
 * 
 * @param warFile
 * @param directory
 * @throws IOException
 */
public static void extractWar(File warFile, File directory) throws IOException {
    try {
        long timestamp = warFile.lastModified();
        File warModifiedTimeFile = new File(directory, LAST_MODIFIED_FILE);
        long lastModified = readLastModifiled(warModifiedTimeFile);

        if (timestamp == lastModified) {
            //      log.info("war file " + warFile.getName() + " not modified.");
            return;
        }
        if (directory.exists()) {
            //         IOUtil.forceRemoveDirectory(directory);
            directory.mkdir();
        }

        //         log.info("war extract start. warfile=" + warFile.getName());

        JarInputStream jin = new JarInputStream(new BufferedInputStream(new FileInputStream(warFile)));
        JarEntry entry = null;

        while ((entry = jin.getNextJarEntry()) != null) {
            File file = new File(directory, entry.getName());

            if (entry.isDirectory()) {
                if (!file.exists()) {
                    file.mkdirs();
                }
            } else {
                File dir = new File(file.getParent());
                if (!dir.exists()) {
                    dir.mkdirs();
                }

                FileOutputStream fout = null;
                try {
                    fout = new FileOutputStream(file);
                    ResourceUtil.copyStream(jin, fout);
                } finally {
                    fout.flush();
                    fout.close();
                    fout = null;
                }

                if (entry.getTime() >= 0) {
                    file.setLastModified(entry.getTime());
                }
            }
        }

        writeLastModifiled(warModifiedTimeFile, timestamp);

        //log.info("war extract success. lastmodified=" + timestamp);
    } catch (IOException ioe) {
        //log.info("war extract fail.");
        throw ioe;
    }
}

From source file:com.izforge.izpack.compiler.helper.CompilerHelper.java

/**
 * Returns the qualified class name for the given class. This method expects as the url param a
 * jar file which contains the given class. It scans the zip entries of the jar file.
 *
 * @param url       url of the jar file which contains the class
 * @param className short name of the class for which the full name should be resolved
 * @return full qualified class name// w w w. j  a v  a2  s.  com
 * @throws IOException if the jar cannot be read
 */
public String getFullClassName(URL url, String className) throws IOException {
    JarInputStream jis = new JarInputStream(url.openStream());
    ZipEntry zentry;
    while ((zentry = jis.getNextEntry()) != null) {
        String name = zentry.getName();
        int lastPos = name.lastIndexOf(".class");
        if (lastPos < 0) {
            continue; // No class file.
        }
        name = name.replace('/', '.');
        int pos = -1;
        int nonCasePos = -1;
        if (className != null) {
            pos = name.indexOf(className);
            nonCasePos = name.toLowerCase().indexOf(className.toLowerCase());
        }
        if (pos != -1 && name.length() == pos + className.length() + 6) // "Main" class found
        {
            jis.close();
            return (name.substring(0, lastPos));
        }

        if (nonCasePos != -1 && name.length() == nonCasePos + className.length() + 6)
        // "Main" class with different case found
        {
            throw new IllegalArgumentException("Fatal error! The declared panel name in the xml file ("
                    + className + ") differs in case to the founded class file (" + name + ").");
        }
    }
    jis.close();
    return (null);
}