Java URL Load findResourcesFromJar(Collection urls, URL url, String resource, ClassLoader cl)

Here you can find the source of findResourcesFromJar(Collection urls, URL url, String resource, ClassLoader cl)

Description

Find all resources from a given jar file and add them to the provided list.

License

Apache License

Parameter

Parameter Description
urls the non-<code>null</code> collection of resources where to add new found resources
url the non-<code>null</code> url for the jar where to find resources
resource the non-<code>null</code> resource being scanned that corresponds to given package
cl the classloader to find the resources with

Declaration

private static void findResourcesFromJar(Collection<URL> urls, URL url, String resource, ClassLoader cl) 

Method Source Code

//package com.java2s;
/*/*  w  ww  .  j  a v a  2  s.co  m*/
 * Copyright (C) 2015-2015 The Helenus Driver Project Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;

import java.util.Collection;

import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class Main {
    /**
     * Find all resources from a given jar file and add them to the provided
     * list.
     *
     * @author paouelle
     *
     * @param urls the non-<code>null</code> collection of resources where to add new
     *        found resources
     * @param url the non-<code>null</code> url for the jar where to find resources
     * @param resource the non-<code>null</code> resource being scanned that
     *        corresponds to given package
     * @param cl the classloader to find the resources with
     */
    private static void findResourcesFromJar(Collection<URL> urls, URL url, String resource, ClassLoader cl) {
        try {
            final JarURLConnection conn = (JarURLConnection) url.openConnection();
            final URL jurl = conn.getJarFileURL();

            try (final JarInputStream jar = new JarInputStream(jurl.openStream());) {
                while (true) {
                    final JarEntry entry = jar.getNextJarEntry();

                    if (entry == null) {
                        break;
                    }
                    final String name = entry.getName();

                    if (name.startsWith(resource)) {
                        final URL u = cl.getResource(name);

                        if (u != null) {
                            urls.add(u);
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("unable to find resources in package", e);
        }
    }
}

Related

  1. getBundle(String basename, Locale locale, ClassLoader cl, URL url)
  2. getManifestAsString(URLClassLoader cl, String jarBaseName)
  3. load(Iterable urls)
  4. load(String fileOrURL)