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:com.dragome.callbackevictor.serverside.utils.RewritingUtils.java

public static void main(final String[] args) throws FileNotFoundException, IOException {
    ResourceTransformer transformer = new AsmClassTransformer();

    for (int i = 0; i < args.length; i += 2) {
        // System.out.println("rewriting " + args[i]);

        RewritingUtils.rewriteJar(new JarInputStream(new FileInputStream(args[i])), transformer,
                new JarOutputStream(new FileOutputStream(args[i + 1])));
    }/*w w w  .  j  ava2  s. c om*/

    // System.out.println("done");

}

From source file:org.vafer.jdependency.utils.DependencyUtils.java

public static Set<String> getDependenciesOfJar(final InputStream pInputStream) throws IOException {
    final JarInputStream inputStream = new JarInputStream(pInputStream);
    final NullOutputStream nullStream = new NullOutputStream();
    final Set<String> dependencies = new HashSet<String>();
    try {/*  w  ww.  java  2s . c o m*/
        while (true) {
            final JarEntry entry = inputStream.getNextJarEntry();

            if (entry == null) {
                break;
            }

            if (entry.isDirectory()) {
                // ignore directory entries
                IOUtils.copy(inputStream, nullStream);
                continue;
            }

            final String name = entry.getName();

            if (name.endsWith(".class")) {
                final DependenciesClassAdapter v = new DependenciesClassAdapter();
                new ClassReader(inputStream).accept(v, 0);
                dependencies.addAll(v.getDependencies());
            } else {
                IOUtils.copy(inputStream, nullStream);
            }
        }
    } finally {
        inputStream.close();
    }

    return dependencies;
}

From source file:com.igormaznitsa.jcp.it.maven.ITPreprocessorMojo.java

private static void assertMainClass(final String jarFile, final String mainClass) throws Exception {
    JarInputStream jarStream = null;
    try {//  www .j  a va 2  s .  c  om
        jarStream = new JarInputStream(new FileInputStream(jarFile));
        final Manifest manifest = jarStream.getManifest();
        final Attributes attrs = manifest.getMainAttributes();
        assertEquals("Maven plugin must also provide and main class in manifest", mainClass,
                attrs.getValue("Main-Class"));
    } finally {
        IOUtils.closeQuietly(jarStream);
    }
}

From source file:com.fengduo.bee.commons.core.lang.ClassLoaderUtils.java

public static List<String> getClassNamesInPackage(String jarName, String packageName) throws IOException {
    JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName));
    packageName = packageName.replace(".", "/");
    List<String> classes = new ArrayList<String>();

    try {/* w  ww.  j a  va  2 s .  c o  m*/
        for (JarEntry jarEntry; (jarEntry = jarFile.getNextJarEntry()) != null;) {
            if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class"))) {
                classes.add(jarEntry.getName().replace("/", ".").replaceAll(".class", ""));
            }
        }
    } finally {
        jarFile.close();
    }
    return classes;
}

From source file:JarUtils.java

/**
 * Add jar contents to the deployment archive under the given prefix
 *///  w w w.  ja  va 2s  .com
public static String[] addJar(JarOutputStream outputStream, String prefix, File jar) throws IOException {

    ArrayList tmp = new ArrayList();
    FileInputStream fis = new FileInputStream(jar);
    JarInputStream jis = new JarInputStream(fis);
    JarEntry entry = jis.getNextJarEntry();
    while (entry != null) {
        if (entry.isDirectory() == false) {
            String entryName = prefix + entry.getName();
            tmp.add(entryName);
            addJarEntry(outputStream, entryName, jis);
        }
        entry = jis.getNextJarEntry();
    }
    jis.close();
    String[] names = new String[tmp.size()];
    tmp.toArray(names);
    return names;
}

From source file:ezbake.frack.submitter.util.JarUtil.java

public static File addFilesToJar(File sourceJar, List<File> newFiles) throws IOException {
    JarOutputStream target = null;
    JarInputStream source;/* w w w  . j a  v a 2 s .  com*/
    File outputJar = new File(sourceJar.getParentFile(), getRepackagedJarName(sourceJar.getAbsolutePath()));
    log.debug("Output file created at {}", outputJar.getAbsolutePath());
    outputJar.createNewFile();
    try {
        source = new JarInputStream(new FileInputStream(sourceJar));
        target = new JarOutputStream(new FileOutputStream(outputJar));
        ZipEntry entry = source.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            // Add ZIP entry to output stream.
            target.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((len = source.read(buffer)) > 0) {
                target.write(buffer, 0, len);
            }
            entry = source.getNextEntry();
        }
        source.close();
        for (File fileToAdd : newFiles) {
            add(fileToAdd, fileToAdd.getParentFile().getAbsolutePath(), target);
        }
    } finally {
        if (target != null) {
            log.debug("Closing output stream");
            target.close();
        }
    }
    return outputJar;
}

From source file:com.cuubez.visualizer.scanner.reader.JarFileReader.java

public JarFileReader(InputStream is, FileFilter filter) throws IOException {
    this.filter = filter;
    jarInputStream = new JarInputStream(is);
}

From source file:io.fabric8.maven.core.util.MavenUtil.java

public static boolean hasKubernetesJson(File f) throws IOException {
    try (FileInputStream fis = new FileInputStream(f); JarInputStream jis = new JarInputStream(fis)) {
        for (JarEntry entry = jis.getNextJarEntry(); entry != null; entry = jis.getNextJarEntry()) {
            if (entry.getName().equals(DEFAULT_CONFIG_FILE_NAME)) {
                return true;
            }// w  w w . j  a v  a2s . co  m
        }
    }
    return false;
}

From source file:com.github.nullstress.asm.SourceSetScanner.java

public Set<String> analyzeJar(URL url) {
    Set<String> dependencies = new HashSet<String>();
    try {//from  www. j ava 2  s.co m
        JarInputStream in = new JarInputStream(url.openStream());
        JarEntry entry;

        while ((entry = in.getNextJarEntry()) != null) {
            String name = entry.getName();

            if (name.endsWith(".class")) {
                dependencies.add(name.replaceAll("/", "."));
            }
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dependencies;
}

From source file:ru.runa.af.web.system.TaskHandlerClassesInformation.java

private static void init() {
    String deploymentDirPath = IoCommons.getDeploymentDirPath();
    String earFilePath = deploymentDirPath + "/" + SystemProperties.getEARFileName();
    Closer closer = Closer.create();/*from   w w w .jav  a2 s .c om*/
    try {
        ZipInputStream earInputStream = closer.register(new ZipInputStream(new FileInputStream(earFilePath)));
        ZipEntry entry;
        while ((entry = earInputStream.getNextEntry()) != null) {
            if (entry.getName().endsWith(".jar")) {
                searchInJar(entry.getName(), new JarInputStream(earInputStream));
            }
        }
        if (IoCommons.getAppServer() == AppServer.JBOSS4) {
            File deploymentDirectory = new File(deploymentDirPath);
            log.debug("Searching in deployment directory: " + deploymentDirectory);
            for (File file : IoCommons.getJarFiles(deploymentDirectory)) {
                JarInputStream jarInputStream = closer.register(new JarInputStream(new FileInputStream(file)));
                searchInJar(file.getName(), jarInputStream);
            }
        }
        File extensionDirectory = new File(IoCommons.getExtensionDirPath());
        if (extensionDirectory.exists() && extensionDirectory.isDirectory()) {
            log.debug("Searching in extension directory: " + extensionDirectory);
            for (File file : IoCommons.getJarFiles(extensionDirectory)) {
                JarInputStream jarInputStream = closer.register(new JarInputStream(new FileInputStream(file)));
                searchInJar(file.getName(), jarInputStream);
            }
        } else {
            log.debug("No extension directory found: " + extensionDirectory);
        }
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
            log.warn(e);
        }
    }
}