Java 8 JarURLConnection - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 JarURLConnection class.

This code example shows how to use the following methods:getJarFile, connect, setAllowUserInteraction, setDoInput, setDoOutput
log.debug("JarHolder: attempting to connect to " + urlpath);
            } 
            URL url = new URL( urlpath );
            conn = (JarURLConnection) url.openConnection();
            conn.setAllowUserInteraction(false);
            conn.setDoInput(true);
            conn.setDoOutput(false);
            conn.connect();
            theJar = conn.getJarFile();
        } 
        catch (IOException ioe)
        { 
            String msg = "JarHolder: error establishing connection to JAR at \""
                         + urlpath + "\"";
            log.error(msg, ioe);
            throw new VelocityException(msg, ioe);
        } 
    } 
 
7
CodeRank
This code example shows how to use the following methods:getJarEntry, getJarFile, getInputStream, setUseCaches
        Support_Resources.copyFile(resources, null, "hyts_att.jar");
        File file = new File(resources.toString() + "/hyts_att.jar");
        URL url = new URL("jar:file:" + file.getPath() + "!/HasAttributes.txt");
 
        JarURLConnection connection = (JarURLConnection) url.openConnection();
        connection.setUseCaches(false);
        connection.getInputStream();
        InputStream in = connection.getInputStream();
        JarFile jarFile1 = connection.getJarFile();
        JarEntry jarEntry1 = connection.getJarEntry();
        in.read();
        in.close();
        JarFile jarFile2 = connection.getJarFile();
        JarEntry jarEntry2 = connection.getJarEntry();
        assertSame(jarFile1, jarFile2);
        assertSame(jarEntry1, jarEntry2);
 
        try { 
            connection.getInputStream();
            fail("should throw IllegalStateException");

6
CodeRank
This code example shows how to use the following methods:getJarFile, connect, setUseCaches
    private JarFile getJarFile(URL jarFileUrl) throws IOException {
	JarFile jarFile = null;
 
	if (jarFileUrl != null) {
	    JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
	    conn.setUseCaches(false);
	    conn.connect();
	    jarFile = conn.getJarFile();
	} 
 
	return jarFile;
    } 
 
} 
6
CodeRank
This code example shows how to use the following methods:getJarEntry, getJarFile
        throw new IOException("Could not find file: " + fileName);
      } 
    } 
    if (url.getProtocol().equals("jar")) {
      JarURLConnection jc = (JarURLConnection) url.openConnection();
      JarFile f = jc.getJarFile();
      JarEntry entry = jc.getJarEntry();
      JarFileModule parent = new JarFileModule(f);
      return new NestedJarFileModule(parent, entry);
    } else if (url.getProtocol().equals("rsrc")) {
      return new ResourceJarFileModule(url);
    } else { 
      String filePath = filePathFromURL(url);
      return new JarFileModule(new JarFile(filePath, false));
    } 
  } 
 
  /** 
   * Properly creates the String file name of a {@link URL}. This works around a 
   * bug in the Sun implementation of {@link URL#getFile()}, which doesn't 
6
CodeRank
This code example shows how to use the following methods:getJarFile, connect, setUseCaches
     
    @Override 
    public JarFile getJarFile() throws IOException {
        URL jarFileUrl = new URL("jar:" + jarUrl + "!/");
        JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
        conn.setUseCaches(false);
        conn.connect();
        return conn.getJarFile();
    } 
        
    @Override 
    public String getUrl() {
        return jarUrl;
    } 
     
    @Override 
    public URL getEntry(String name) {
        try { 
            return new URL("jar:" + jarUrl + "!/" + name);
        } catch (MalformedURLException e) {
5
CodeRank
This code example shows how to use the following methods:getJarEntry, getJarFile
        final String protocol = resourceURL.getProtocol();
        switch (protocol) {
            case "jar": 
                try { 
                    final JarURLConnection jarConnection = (JarURLConnection) resourceURL.openConnection();
                    final JarEntry entry = jarConnection.getJarEntry();
                    if (entry.isDirectory()) {
                        return true; 
                    } 
 
                    // WARNING! Heuristics ahead. 
                    // It turns out that JarEntry#isDirectory() really just tests whether the filename ends in a '/'. 
                    // If you try to open the same URL without a trailing '/', it'll succeed — but the result won't be 
                    // what you want. We try to get around this by calling getInputStream() on the file inside the jar. 
                    // This seems to return null for directories (though that behavior is undocumented as far as I 
                    // can tell). If you have a better idea, please improve this. 
 
                    final String fileName = resourceURL.getFile();
                    // leaves just the relative file path inside the jar 
                    final String relativeFilePath = fileName.substring(fileName.lastIndexOf('!') + 2);

5
CodeRank
This code example shows how to use the following methods:getJarFile, connect
        url = new URL("jar:"+BASE+"!/missing");
 
        JarURLConnection connection = null;
        connection = (JarURLConnection) url.openConnection();
        try { 
            connection.connect();
            fail("Did not throw exception on connect");
        } catch (IOException e) {
            // expected 
        } 
 
        try { 
            connection.getJarFile();
            fail("Did not throw exception after connect");
        } catch (IOException e) {
            // expected 
        } 
 
        File resources = Support_Resources.createTempFolder();
 
5
CodeRank
This code example shows how to use the following methods:getJarFile, setUseCaches
            { 
                // No - so lets look if the root entry exists. 
                try 
                { 
                    JarURLConnection c=(JarURLConnection)((new URL(_jarUrl)).openConnection());
                    c.setUseCaches(getUseCaches());
                    jar_file=c.getJarFile();
                    close_jar_file = !getUseCaches();
                } 
                catch(Exception e)
                { 
                    LOG.ignore(e);
                } 
            } 
 
            // Do we need to look more closely? 
            if (jar_file!=null && _entry==null && !_directory)
            { 
                // OK - we have a JarFile, lets look for the entry 
                JarEntry entry = jar_file.getJarEntry(_path);
5
CodeRank
This code example shows how to use the following methods:getJarFile, connect, setUseCaches
    private JarFile getJarFile(URL jarFileUrl) throws IOException {
        JarFile jarFile = null;
 
        if (jarFileUrl != null) {
            JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
            conn.setUseCaches(false);
            conn.connect();
            jarFile = conn.getJarFile();
        } 
 
        return jarFile;
    } 
 
} 
5
CodeRank
This code example shows how to use the following methods:getJarEntry, setUseCaches
 
  private void getJarEntryProperties(URL url) {
    try { 
      jarConn = (JarURLConnection) url.openConnection();
      // useCaches is usually set to true by default, but make sure here 
      jarConn.setUseCaches(true);
      // See if our entry exists 
      JarEntry jarEntry = jarConn.getJarEntry();
      if (jarEntry != null) {
        exists = true;
        if (!shouldCareAboutLastModified) {
          lastModified = 0;
          return; 
        } 
        // TODO(jbrosenberg): Note the time field for a jarEntry can be 
        // unreliable, and is not always required in a jar file. Consider using 
        // the timestamp on the jar file itself. 
        lastModified = jarEntry.getTime();
      } 
      if (!exists) {
4
CodeRank
See Code Examples for Java 8 JarURLConnection Methods: