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:org.apache.hadoop.util.TestJarFinder.java

@Test
public void testNoManifest() throws Exception {
    File dir = GenericTestUtils.getTestDir(TestJarFinder.class.getName() + "-testNoManifest");
    delete(dir);/* w w w .ja v  a  2  s. c  om*/
    dir.mkdirs();
    File propsFile = new File(dir, "props.properties");
    Writer writer = new FileWriter(propsFile);
    new Properties().store(writer, "");
    writer.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream zos = new JarOutputStream(baos);
    JarFinder.jarDir(dir, "", zos);
    JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Assert.assertNotNull(jis.getManifest());
    jis.close();
}

From source file:petascope.util.IOUtil.java

public static List<String> filesInJarDir(String jarDir) {
    List<String> ret = new ArrayList<String>();
    JarInputStream jfile = null;
    try {/*from  w w  w.  j a v  a  2  s  .com*/
        // path to the jar
        String jarPath = IOUtil.class.getResource("").getPath();
        jarPath = jarPath.substring(0, jarPath.indexOf("!"));

        File file = new File(new URI(jarPath));
        jfile = new JarInputStream(new FileInputStream(file));
        JarEntry entry = null;
        do {
            try {
                entry = jfile.getNextJarEntry();
                if (entry == null) {
                    continue;
                }
                String sentry = entry.toString();
                if (("/" + sentry).contains(jarDir) && !sentry.endsWith("/")) {
                    ret.add(sentry);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } while (entry != null);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            jfile.close();
        } catch (Exception ex) {
        }
        return ret;
    }
}

From source file:org.apache.hadoop.hbase.mapreduce.hadoopbackport.TestJarFinder.java

@Test
public void testNoManifest() throws Exception {
    File dir = new File(System.getProperty("test.build.dir", "target/test-dir"),
            TestJarFinder.class.getName() + "-testNoManifest");
    delete(dir);/*w  ww  .j  a  v a2  s .c  o m*/
    dir.mkdirs();
    File propsFile = new File(dir, "props.properties");
    Writer writer = new FileWriter(propsFile);
    new Properties().store(writer, "");
    writer.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream zos = new JarOutputStream(baos);
    JarFinder.jarDir(dir, "", zos);
    JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Assert.assertNotNull(jis.getManifest());
    jis.close();
}

From source file:org.apache.hadoop.util.TestJarFinder.java

@Test
public void testExistingManifest() throws Exception {
    File dir = GenericTestUtils.getTestDir(TestJarFinder.class.getName() + "-testExistingManifest");
    delete(dir);/*from  w w  w  .ja v a  2s . c  om*/
    dir.mkdirs();

    File metaInfDir = new File(dir, "META-INF");
    metaInfDir.mkdirs();
    File manifestFile = new File(metaInfDir, "MANIFEST.MF");
    Manifest manifest = new Manifest();
    OutputStream os = new FileOutputStream(manifestFile);
    manifest.write(os);
    os.close();

    File propsFile = new File(dir, "props.properties");
    Writer writer = new FileWriter(propsFile);
    new Properties().store(writer, "");
    writer.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream zos = new JarOutputStream(baos);
    JarFinder.jarDir(dir, "", zos);
    JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Assert.assertNotNull(jis.getManifest());
    jis.close();
}

From source file:com.threecrickets.jygments.grammar.Lexer.java

public static Lexer getForFileName(String fileName) throws ResolutionException {
    if (lexerMap.isEmpty()) {
        try {// w w w . j a v a  2  s. c om
            File jarFile = new File(Jygments.class.getProtectionDomain().getCodeSource().getLocation().toURI());
            JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile));
            try {
                for (JarEntry jarEntry = jarInputStream
                        .getNextJarEntry(); jarEntry != null; jarEntry = jarInputStream.getNextJarEntry()) {
                    if (jarEntry.getName().endsWith(".json")) {
                        String lexerName = jarEntry.getName();
                        // strip off the .json
                        lexerName = lexerName.substring(0, lexerName.length() - 5);
                        Lexer lexer = Lexer.getByFullName(lexerName);
                        for (String filename : lexer.filenames)
                            if (filename.startsWith("*."))
                                lexerMap.put(filename.substring(filename.lastIndexOf('.')), lexer);
                    }
                }
            } finally {
                jarInputStream.close();
            }
        } catch (URISyntaxException x) {
            throw new ResolutionException(x);
        } catch (FileNotFoundException x) {
            throw new ResolutionException(x);
        } catch (IOException x) {
            throw new ResolutionException(x);
        }
    }

    return lexerMap.get(fileName.substring(fileName.lastIndexOf('.')));
}

From source file:org.apache.hadoop.hbase.mapreduce.hadoopbackport.TestJarFinder.java

@Test
public void testExistingManifest() throws Exception {
    File dir = new File(System.getProperty("test.build.dir", "target/test-dir"),
            TestJarFinder.class.getName() + "-testExistingManifest");
    delete(dir);//ww  w  .  j  a  v  a 2s . co m
    dir.mkdirs();

    File metaInfDir = new File(dir, "META-INF");
    metaInfDir.mkdirs();
    File manifestFile = new File(metaInfDir, "MANIFEST.MF");
    Manifest manifest = new Manifest();
    OutputStream os = new FileOutputStream(manifestFile);
    manifest.write(os);
    os.close();

    File propsFile = new File(dir, "props.properties");
    Writer writer = new FileWriter(propsFile);
    new Properties().store(writer, "");
    writer.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream zos = new JarOutputStream(baos);
    JarFinder.jarDir(dir, "", zos);
    JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Assert.assertNotNull(jis.getManifest());
    jis.close();
}

From source file:org.pentaho.osgi.platform.webjars.WebjarsURLConnectionTest.java

@Test
public void testClosingStream() throws IOException {
    WebjarsURLConnection connection = new WebjarsURLConnection(
            new URL("mvn:org.webjars/angular-dateparser/1.0.9"));
    connection.connect();//w  ww .j  a  v a2s. co  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:org.apache.brooklyn.core.mgmt.osgi.OsgiStandaloneTest.java

@Test
public void testReadKnownManifest() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    InputStream in = this.getClass().getResourceAsStream(BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    JarInputStream jarIn = new JarInputStream(in);
    ManifestHelper helper = Osgis.ManifestHelper.forManifest(jarIn.getManifest());
    jarIn.close();
    Assert.assertEquals(helper.getVersion().toString(), "0.1.0");
    Assert.assertTrue(helper.getExportedPackages().contains("org.apache.brooklyn.test.osgi.entities"));
}

From source file:org.getobjects.foundation.NSJavaRuntime.java

private static void getClassNamesFromJarFile(String _jarPath, String _pkg, Set<String> classes_) {
    FileInputStream fos;/*ww w. j  a  va 2 s.c o  m*/
    try {
        fos = new FileInputStream(_jarPath);
    } catch (FileNotFoundException e1) {
        return; // TBD: log
    }

    JarInputStream jarFile;
    try {
        jarFile = new JarInputStream(fos);
    } catch (IOException e) {
        return; // TBD: log
    }

    do {
        JarEntry jarEntry;
        try {
            jarEntry = jarFile.getNextJarEntry();
        } catch (IOException e) {
            jarEntry = null;
        }

        if (jarEntry == null)
            break;

        String className = jarEntry.getName();
        if (!className.endsWith(".class"))
            continue;

        className = stripFilenameExtension(className);
        if (className.startsWith(_pkg))
            classes_.add(className.replace('/', '.'));
    } while (true);

    if (jarFile != null) {
        try {
            jarFile.close();
        } catch (IOException e) {
            // TBD: log?
        }
    }
}

From source file:brooklyn.management.osgi.OsgiStandaloneTest.java

@Test
public void testReadKnownManifest() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    InputStream in = this.getClass().getResourceAsStream(BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    JarInputStream jarIn = new JarInputStream(in);
    ManifestHelper helper = Osgis.ManifestHelper.forManifest(jarIn.getManifest());
    jarIn.close();
    Assert.assertEquals(helper.getVersion().toString(), "0.1.0");
    Assert.assertTrue(helper.getExportedPackages().contains("brooklyn.osgi.tests"));
}