Java Code Examples for java.net.JarURLConnection#getInputStream()

The following examples show how to use java.net.JarURLConnection#getInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceScanner.java    From AsyncDao with MIT License 5 votes vote down vote up
private static void findFileInJarWithinJar(String filePath, String packagePath, Set<String> classes) throws IOException {
    URL url = new URL("jar", null, 0, filePath);
    URLConnection con = url.openConnection();
    if (con instanceof JarURLConnection) {
        JarURLConnection jarURLConnection = (JarURLConnection) con;
        JarInputStream jarInputStream = new JarInputStream(jarURLConnection.getInputStream());
        JarEntry entry;
        while ((entry = jarInputStream.getNextJarEntry()) != null) {
            filterClass(entry.getName(), packagePath, classes);
        }
        IOUtils.closeQuietly(jarInputStream);
    }
}
 
Example 2
Source File: JapURLConnection.java    From jolie with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void connect()
	throws IOException {
	if( !connected ) {
		final String urlPath = url.getPath();
		final Matcher matcher = URL_PATTERN.matcher( urlPath );
		if( matcher.matches() ) {
			final String path = matcher.group( 1 );
			final String subPath = matcher.group( 2 );
			final JarURLConnection jarURLConnection = (JarURLConnection) new URL( "jar:" + path ).openConnection();
			inputStream = jarURLConnection.getInputStream();
			if( subPath.isEmpty() == false ) {
				JarFile jar = retrieve( new URL( path ), inputStream );
				final String[] nodes = NESTING_SEPARATION_PATTERN.split( subPath );
				int i;
				final StringBuilder builder = new StringBuilder( path.length() + subPath.length() );
				builder.append( path );
				for( i = 0; i < nodes.length - 1; i++ ) {
					builder.append( "!/" ).append( nodes[ i ] );
					jar = retrieve( new URL( builder.toString() ), inputStream );
				}
				final ZipEntry entry = jar.getEntry( nodes[ i ] );
				entrySize = entry.getSize();
				inputStream = jar.getInputStream( entry );
			}
		} else {
			throw new MalformedURLException( "Invalid JAP URL path: " + urlPath );
		}

		connected = true;
	}
}
 
Example 3
Source File: NativeClassLoader.java    From sensorhub with Mozilla Public License 2.0 4 votes vote down vote up
private String extractResource(String libName, URL url)
{
    try
    {
        URLConnection con = url.openConnection();
        String libPath;
        
        if (con instanceof JarURLConnection)
        {                
            // get resource from jar
            JarURLConnection jarItemConn = (JarURLConnection)con;
            InputStream in = new BufferedInputStream(jarItemConn.getInputStream());
            
            // copy to temp location (folder named as jar file)
            File jarFile = new File(jarItemConn.getJarFile().getName());
            File tmpDir = Files.createTempDirectory(jarFile.getName()).toFile();
            tmpFolders.add(tmpDir);
            File outFile = new File(tmpDir, jarItemConn.getJarEntry().getName());
            outFile.getParentFile().mkdirs();
            OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));            
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) > 0)
                out.write(buffer, 0, len);
            out.close();
            in.close();

            // use path to temp file
            libPath = outFile.getPath();
        }
        else
        {
            // if not in JAR, use filesystem path directly
            libPath = url.getFile();
        }            
        
        loadedLibraries.put(libName, libPath);
        log.debug("Using native library from: " + libPath);
        return libPath;
    }
    catch (java.io.IOException e)
    {
        return null;
    }
}