Example usage for java.util.jar JarInputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:com.speed.ob.Obfuscator.java

public Obfuscator(final Config config) {
    transforms = new LinkedList<>();
    store = new ClassStore();
    this.config = config;
    //set up logging
    this.LOGGER = Logger.getLogger(this.getClass().getName());
    LOGGER.info("Ob2 is starting");
    String logLvl = config.get("Obfuscator.logging");
    String logDir = config.get("Obfuscator.log_dir");
    level = parseLevel(logLvl);/*from w  ww .j  av  a 2s  . c  om*/
    LOGGER.info("Logger level set to " + level.getName());
    Logger topLevel = Logger.getLogger("");
    topLevel.setLevel(level);
    File logs = new File(logDir);
    if (!logs.exists()) {
        if (!logs.mkdir())
            Logger.getLogger(this.getClass().getName()).warning("Could not create logging directory");
    }
    try {
        if (logs.exists()) {
            fHandler = new FileHandler(logs.getAbsolutePath() + File.separator + "ob%g.log");
            topLevel.addHandler(fHandler);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    for (Handler handler : topLevel.getHandlers()) {
        handler.setLevel(level);
    }
    //populate transforms
    LOGGER.info("Configuring Ob");
    LOGGER.fine("Parsing config");
    if (config.getBoolean("Obfuscator.all_transforms")) {
        LOGGER.fine("Adding all transforms");
        transforms.add(ClassNameTransform.class);
    } else {
        if (config.getBoolean("Obfuscator.classname_obfuscation")) {
            LOGGER.fine("Adding class name transform");
            transforms.add(ClassNameTransform.class);
        }
        if (config.getBoolean("Obfuscator.controlflow_obfuscation")) {
            LOGGER.fine("Control flow obfuscation not added, transform does not exist");
        }
        if (config.getBoolean("Obfuscator.string_obfuscation")) {
            LOGGER.fine("String obfuscation not added, transform does not exist");

        }
        if (config.getBoolean("Obfuscator.fieldname_transforms")) {
            LOGGER.fine("Field name obfuscation not added, transform does not exist");

        }
        if (config.getBoolean("Obfuscator.methodname_transforms")) {
            LOGGER.fine("Method name obfuscation not added, transform does not exist");

        }
    }
    LOGGER.info("Loaded " + transforms.size() + " transforms");
    String inputFile = config.get("Obfuscator.input");
    LOGGER.fine("Checking input file(s) and output directory");
    String outFile = config.get("Obfuscator.out_dir");
    out = new File(outFile);
    if (inputFile == null || inputFile.isEmpty()) {
        LOGGER.severe("Input file not specified in config");
        throw new RuntimeException("Input file not specified");
    } else {
        in = new File(inputFile);
        if (!in.exists()) {
            LOGGER.severe("Input file not found");
            throw new RuntimeException("Input file not found");
        }
        LOGGER.fine("Attempting to initialise classes");
        if (in.isDirectory()) {
            try {
                store.init(in.listFiles(), false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (in.getName().endsWith(".class")) {
            try {
                store.init(new File[] { in }, false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (in.getName().endsWith(".jar")) {
            try {
                JarInputStream in = new JarInputStream(new FileInputStream(this.in));
                store.init(in, out, this.in);
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        LOGGER.info("Loaded " + store.nodes().size() + " classes");
    }
    if (!out.exists()) {
        LOGGER.fine("Attempting to make output directory");
        if (!out.mkdir()) {
            LOGGER.severe("Could not make output directory");
            throw new RuntimeException("Could not create output dir: " + out.getAbsolutePath());
        }
    } else if (!out.isDirectory()) {
        LOGGER.severe("Output directory is a file");
        throw new RuntimeException(out.getName() + " is not a directory, cannot output there");
    } else {
        if (!out.canWrite()) {
            LOGGER.severe("Cannot write to output directory");
            throw new RuntimeException("Cannot write to output dir: " + out.getAbsolutePath());
        }
    }

}

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

public Set<String> analyzeJar(URL url) {
    Set<String> dependencies = new HashSet<String>();
    try {/*  w  ww. j  a va2  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:org.bimserver.plugins.classloaders.JarClassLoader.java

private void loadSubJars(byte[] byteArray) {
    try {//from  w  ww.j  ava2  s .  c om
        JarInputStream jarInputStream = new JarInputStream(new ByteArrayInputStream(byteArray));
        JarEntry entry = jarInputStream.getNextJarEntry();
        while (entry != null) {
            addDataToMap(jarInputStream, entry);
            entry = jarInputStream.getNextJarEntry();
        }
        jarInputStream.close();
    } catch (IOException e) {
        LOGGER.error("", e);
    }
}

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

/**
 * Returns a list which contains the pathes of all files which are included in the given url.
 * This method expects as the url param a jar.
 *
 * @param url url of the jar file/*from w ww .  j  a va 2 s.  co m*/
 * @return full qualified paths of the contained files
 * @throws IOException if the jar cannot be read
 */
public List<String> getContainedFilePaths(URL url) throws IOException {
    JarInputStream jis = new JarInputStream(url.openStream());
    ZipEntry zentry;
    ArrayList<String> fullNames = new ArrayList<String>();
    while ((zentry = jis.getNextEntry()) != null) {
        String name = zentry.getName();
        // Add only files, no directory entries.
        if (!zentry.isDirectory()) {
            fullNames.add(name);
        }
    }
    jis.close();
    return (fullNames);
}

From source file:com.kotcrab.vis.editor.module.editor.PluginLoaderModule.java

private void loadPluginsDescriptors(Array<FileHandle> pluginsFolders) throws IOException {
    for (FileHandle folder : pluginsFolders) {

        FileHandle[] files = folder.list((dir, name) -> name.endsWith("jar"));
        if (files.length > 1 || files.length == 0) {
            Log.error(TAG, "Failed (invalid directory structure): " + folder.name());
            failedPlugins.add(new FailedPluginDescriptor(folder, new IllegalStateException(
                    "Plugin directory must contain only one jar (required libs must be stored in 'lib' subdirectory")));
            continue;
        }//from w  w w  .j  a  v  a  2 s .  c  o m

        FileHandle pluginJar = files[0];

        try {
            JarInputStream jarStream = new JarInputStream(new FileInputStream(pluginJar.file()));
            Manifest mf = jarStream.getManifest();
            jarStream.close();

            PluginDescriptor desc = new PluginDescriptor(pluginJar, mf);
            allPlugins.add(desc);
        } catch (IOException e) {
            Log.error(TAG, "Failed (IO exception): " + folder.name());
            failedPlugins.add(new FailedPluginDescriptor(folder, e));
            Log.exception(e);
        } catch (EditorException e) {
            Log.error(TAG, "Failed: " + folder.name());
            failedPlugins.add(new FailedPluginDescriptor(folder, e));
            Log.exception(e);
        }
    }
}

From source file:org.jahia.admin.components.AssemblerTask.java

private boolean needRewriting(File source) throws FileNotFoundException, IOException {
    final JarInputStream jarIn = new JarInputStream(new FileInputStream(source));
    String webXml = null;// ww w  .j  av  a 2s  .  co  m
    JarEntry jarEntry;
    try {
        // Read the source archive entry by entry
        while ((jarEntry = jarIn.getNextJarEntry()) != null) {
            if (Assembler.SERVLET_XML.equals(jarEntry.getName())) {
                webXml = IOUtils.toString(jarIn);
            }
            jarIn.closeEntry();
        }
    } finally {
        jarIn.close();
    }

    return webXml == null || !webXml.contains(Assembler.DISPATCH_SERVLET_CLASS);
}

From source file:ru.codeinside.adm.ui.UploadDeployer.java

private void checkSupportInterface(Upload.SucceededEvent event, String supportedInterface) throws Exception {
    if (!event.getFilename().endsWith(".jar")) {
        throw new Exception("? jar ");
    }// www  . j  a  v a 2 s .  c o m
    JarInputStream jarStream = null;
    try {
        jarStream = new JarInputStream(new ByteArrayInputStream(fileData));
        if (!isOsgiComponent(jarStream)) {
            throw new Exception("? osgi ");
        }
        if (!hasApiServer(supportedInterface, jarStream)) {
            throw new Exception("?  ? [" + supportedInterface + "]");
        }
    } finally {
        if (jarStream != null) {
            jarStream.close();
        }
    }
}

From source file:org.bimserver.plugins.JarClassLoader.java

private void loadSubJars(byte[] byteArray) {
    try {/*from  w  ww. j ava 2 s.  c om*/
        JarInputStream jarInputStream = new JarInputStream(new ByteArrayInputStream(byteArray));
        JarEntry entry = jarInputStream.getNextJarEntry();
        while (entry != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            IOUtils.copy(jarInputStream, byteArrayOutputStream);
            map.put(entry.getName(), byteArrayOutputStream.toByteArray());
            entry = jarInputStream.getNextJarEntry();
        }
        jarInputStream.close();
    } catch (IOException e) {
        LOGGER.error("", e);
    }
}

From source file:io.joynr.util.JoynrUtil.java

public static void copyDirectoryFromJar(String jarName, String srcDir, File tmpDir) throws IOException {

    JarFile jf = null;//from  w  w w  .  j  a  va2  s .  co  m
    JarInputStream jarInputStream = null;

    try {
        jf = new JarFile(jarName);
        JarEntry je = jf.getJarEntry(srcDir);
        if (je.isDirectory()) {
            FileInputStream fis = new FileInputStream(jarName);
            BufferedInputStream bis = new BufferedInputStream(fis);
            jarInputStream = new JarInputStream(bis);
            JarEntry ze = null;
            while ((ze = jarInputStream.getNextJarEntry()) != null) {
                if (ze.isDirectory()) {
                    continue;
                }
                if (ze.getName().contains(je.getName())) {
                    InputStream is = jf.getInputStream(ze);
                    String name = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                    File tmpFile = new File(tmpDir + "/" + name); //File.createTempFile(file.getName(), "tmp");
                    tmpFile.deleteOnExit();
                    OutputStream outputStreamRuntime = new FileOutputStream(tmpFile);
                    copyStream(is, outputStreamRuntime);
                }
            }
        }
    } finally {
        if (jf != null) {
            jf.close();
        }
        if (jarInputStream != null) {
            jarInputStream.close();
        }
    }
}

From source file:com.geewhiz.pacify.TestArchive.java

@Test
public void checkJar() throws ArchiveException, IOException {
    String testFolder = "testArchive/correct/jar";

    File targetResourceFolder = new File("target/test-resources/", testFolder);

    LinkedHashSet<Defect> defects = createPrepareValidateAndReplace(testFolder,
            createPropertyResolveManager(propertiesToUseWhileResolving));

    Assert.assertEquals("We shouldnt get any defects.", 0, defects.size());

    File expectedArchive = new File(targetResourceFolder, "expectedResult/archive.jar");
    File resultArchive = new File(targetResourceFolder, "package/archive.jar");

    JarInputStream expected = new JarInputStream(new FileInputStream(expectedArchive));
    JarInputStream result = new JarInputStream(new FileInputStream(resultArchive));

    Assert.assertNotNull("SRC jar should contain the manifest as first entry", expected.getManifest());
    Assert.assertNotNull("RESULT jar should contain the manifest as first entry", result.getManifest());

    expected.close();
    result.close();/*from   w  w w  .jav a2 s  . c o  m*/

    checkIfResultIsAsExpected(testFolder);
}