Java ClassLoader Load getInputStream(String name)

Here you can find the source of getInputStream(String name)

Description

get Input Stream

License

Apache License

Declaration

public static InputStream getInputStream(String name) 

Method Source Code

//package com.java2s;
/*//from  w  ww  . j  a v  a  2  s .  c  om
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * 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.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;

import java.net.URL;

public class Main {
    public static InputStream getInputStream(String name) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream is = null;

        if ((name != null) && (name.indexOf(":/") > -1)) {
            try {
                is = new URL(name).openStream();
            } catch (Exception e) {
            }
        }

        URL url = classLoader.getResource(name);

        if (is == null) {
            try {
                is = classLoader.getResourceAsStream(name);
            } catch (Exception e) {
            }
        }

        if (is == null) {
            try {
                is = classLoader.getResourceAsStream('/' + name);
            } catch (Exception e) {
            }
        }

        if (is == null) {
            try {
                is = classLoader.getResourceAsStream("META-INF/" + name);
            } catch (Exception e) {
            }
        }

        if (is == null) {
            try {
                is = new FileInputStream(name);
            } catch (FileNotFoundException e) {
            }
        }

        return is;
    }

    public static URL getResource(String name) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = null;

        if ((name != null) && (name.indexOf(":/") > -1)) {
            try {
                url = new URL(name);
            } catch (Exception e) {
            }
        }

        if (url == null) {
            try {
                url = classLoader.getResource(name);
            } catch (Exception e) {
            }
        }

        if (url == null) {
            try {
                url = classLoader.getResource('/' + name);
            } catch (Exception e) {
            }
        }

        if (url == null) {
            try {
                url = classLoader.getResource("META-INF/" + name);
            } catch (Exception e) {
            }
        }

        return url;
    }
}

Related

  1. getFileFromPool(String path)
  2. getFileFullPath(String file)
  3. getFilesInPackage(String packageName)
  4. getFullPathByPkg(String pkg)
  5. getImageFilename(String name)
  6. getLibKoyaVersion()
  7. getLocalFile(final String aLocalPath)
  8. getLog4jPropertiesLocation()
  9. getManifestInfo()