Java Resource Get getResource(String inResource)

Here you can find the source of getResource(String inResource)

Description

trying to fetch data from the following resources CLASSPATH, FILE, URL

License

Open Source License

Parameter

Parameter Description
inResource a parameter

Declaration

public static InputStream getResource(String inResource) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.InputStream;

import java.net.URL;

public class Main {
    /**//from ww w.j a  v  a 2 s  .  co m
     * 
     * trying to fetch data from the following resources
     * 
     * CLASSPATH,
     * FILE,
     * URL
     * 
     * @param inResource
     * @return
     */
    public static InputStream getResource(String inResource) {
        return getResource(inResource, true);
    }

    public static InputStream getResource(String inResource, final boolean inAllowUrlResolution) {

        InputStream theIs = null;

        try {

            theIs = inResource == null ? null : getResourceUrl(inResource, inAllowUrlResolution).openStream();

        } catch (Exception e) {
            // swallow
        }

        return theIs;

    }

    public static URL getResourceUrl(String inResource) {

        return getResourceUrl(inResource, true);
    }

    public static URL getResourceUrl(String inResource, boolean inAllowUrlResolution) {

        if (inResource == null) {
            return null;
        }

        URL theIs = null;

        try {

            String theResource = inResource;
            theResource = theResource != null && theResource.length() > 0 && theResource.charAt(0) == '/'
                    ? theResource.substring(1)
                    : theResource;
            theIs = Thread.currentThread().getContextClassLoader().getResource(theResource);

        } catch (Exception e) {
            // swallow
        }

        try {
            theIs = theIs != null ? theIs : new File(inResource).toURI().toURL();
        } catch (Exception e) {
            // swallow
        }

        try {
            theIs = theIs != null || inAllowUrlResolution == false ? theIs : new URL(inResource);
        } catch (Exception e) {
            // swallow
        }

        return theIs;
    }
}

Related

  1. getResource(final String resource)
  2. getResource(final String resourceName, final Class caller)
  3. getResource(String filename)
  4. getResource(String filename)
  5. getResource(String folder, String name)
  6. getResource(String key, String bundleName, Locale locale)
  7. getResource(String name)
  8. getResource(String name)
  9. getResource(String name)