Java Resource Load getResourceAsStream(final String fileName, ClassLoader classLoader)

Here you can find the source of getResourceAsStream(final String fileName, ClassLoader classLoader)

Description

Gets a resource as InputStream.

License

Open Source License

Parameter

Parameter Description
fileName The path and name of the file relative from the working directory.

Exception

Parameter Description
FileNotFoundException Thrown if the resource cannot be located.

Return

The resource as InputStream.

Declaration

public static InputStream getResourceAsStream(final String fileName, ClassLoader classLoader)
        throws FileNotFoundException 

Method Source Code

//package com.java2s;
/*/*  ww w.  j av  a2 s.co  m*/
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at license/ESCIDOC.LICENSE
* or http://www.escidoc.org/license.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at license/ESCIDOC.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Gets a resource as InputStream.
     *
     * @param fileName The path and name of the file relative from the working directory.
     * @return The resource as InputStream.
     * @throws FileNotFoundException Thrown if the resource cannot be located.
     */
    public static InputStream getResourceAsStream(final String fileName, ClassLoader classLoader)
            throws FileNotFoundException {

        InputStream fileIn;
        fileIn = classLoader.getResourceAsStream(resolveFileName(fileName));

        // Maybe it's in a WAR file
        if (fileIn == null) {
            fileIn = classLoader.getResourceAsStream(resolveFileName("WEB-INF/classes/" + fileName));
        }

        if (fileIn == null) {
            fileIn = new FileInputStream(resolveFileName(fileName));
        }

        return fileIn;
    }

    /**
     * This method resolves /.. in uris
     * @param name
     * @return
     */
    public static String resolveFileName(String name) {
        if (name != null && (name.contains("/..") || name.contains("\\.."))) {
            Pattern pattern1 = Pattern.compile("(\\\\|/)\\.\\.");
            Matcher matcher1 = pattern1.matcher(name);
            if (matcher1.find()) {
                int pos1 = matcher1.start();
                Pattern pattern2 = Pattern.compile("(\\\\|/)[^\\\\/]*$");
                Matcher matcher2 = pattern2.matcher(name.substring(0, pos1));
                if (matcher2.find()) {
                    int pos2 = matcher2.start();
                    return resolveFileName(name.substring(0, pos2) + name.substring(pos1 + 3));
                }
            }
        }

        return name;
    }
}

Related

  1. getResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst)
  2. getResourceAsStream(Class clazz, String fileName)
  3. getResourceAsStream(Class clazz, String resource)
  4. getResourceAsStream(Class clazz, String resourceName)
  5. getResourceAsStream(Class testClass, String resource)
  6. getResourceAsStream(final String name)
  7. getResourceAsStream(final String resourceName, final Class caller)
  8. getResourceAsStream(final String resourceName, final Class caller)
  9. getResourceAsStream(Object context, String resourceName)