Description
Get the InputStream input stream to the resource given by the supplied path.
License
Apache License
Parameter
Parameter | Description |
---|
resourcePath | the logical path to the classpath, file, or URL resource |
clazz | the class that should be used to load the resource as a stream; may be null |
classLoader | the classloader that should be used to load the resource as a stream; may be null |
Exception
Parameter | Description |
---|
IllegalArgumentException | if the resource path is null or empty |
Return
an input stream to the resource; or null if the resource could not be found
Declaration
public static InputStream getResourceAsStream(String resourcePath, ClassLoader classLoader, Class<?> clazz)
Method Source Code
//package com.java2s;
/*/*w w w. j a v a 2 s. co m*/
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
/**
* Get the {@link InputStream input stream} to the resource given by the supplied path. If a class loader is supplied, the
* method attempts to resolve the resource using the {@link ClassLoader#getResourceAsStream(String)} method; if the result is
* non-null, it is returned. Otherwise, if a class is supplied, this method attempts to resolve the resource using the
* {@link Class#getResourceAsStream(String)} method; if the result is non-null, it is returned. Otherwise, this method then
* uses the Class' ClassLoader to load the resource; if non-null, it is returned . Otherwise, this method looks for an
* existing and readable {@link File file} at the path; if found, a buffered stream to that file is returned. Otherwise, this
* method attempts to parse the resource path into a valid {@link URL}; if this succeeds, the method attempts to open a stream
* to that URL. If all of these fail, this method returns null.
*
* @param resourcePath the logical path to the classpath, file, or URL resource
* @param clazz the class that should be used to load the resource as a stream; may be null
* @param classLoader the classloader that should be used to load the resource as a stream; may be null
* @return an input stream to the resource; or null if the resource could not be found
* @throws IllegalArgumentException if the resource path is null or empty
*/
public static InputStream getResourceAsStream(String resourcePath, ClassLoader classLoader, Class<?> clazz) {
if (resourcePath == null)
throw new IllegalArgumentException("resourcePath may not be null");
InputStream result = null;
if (classLoader != null) {
// Try using the class loader first ...
result = classLoader.getResourceAsStream(resourcePath);
}
if (result == null && clazz != null) {
// Not yet found, so try the class ...
result = clazz.getResourceAsStream(resourcePath);
if (result == null) {
// Not yet found, so try the class's class loader ...
result = clazz.getClassLoader().getResourceAsStream(resourcePath);
}
}
if (result == null) {
// Still not found, so see if this is an existing File ...
try {
File file = new File(resourcePath);
if (file.exists() && file.canRead()) {
return new BufferedInputStream(new FileInputStream(file));
}
} catch (FileNotFoundException e) {
// just continue ...
}
}
if (result == null) {
// Still not found, so try to construct a URL out of it ...
try {
URL url = new URL(resourcePath);
return url.openStream();
} catch (MalformedURLException e) {
// just continue ...
} catch (IOException err) {
// just continue ...
}
}
// Couldn't find it anywhere ...
return result;
}
}
Related
- getResource(String relativePath)
- getResource(String resourcePath)
- getResourceAsFile(Object relativeTo, String relativePath)
- getResourceAsFile(String relativePath)
- getResourceAsStream(final String path)
- getResourceAsString(String path)
- getResourceFile(Class clazz, String relPath)
- getResourceFile(final Class> baseClass, final String path)
- getResourceFile(String sResourcePath, Class> cRefClass)