com.github.mathiewz.slick.util.ResourceLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of modernized-slick Show documentation
Show all versions of modernized-slick Show documentation
The main purpose of this libraryis to modernize and maintain the slick2D library.
The newest version!
package com.github.mathiewz.slick.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import com.github.mathiewz.slick.SlickException;
/**
* A simple wrapper around resource loading should anyone decide to change
* their minds how this is meant to work in the future.
*
* @author Kevin Glass
*/
public class ResourceLoader {
/** The list of locations to be searched */
private static ArrayList locations = new ArrayList<>();
static {
locations.add(new ClasspathLocation());
locations.add(new FileSystemLocation(new File(".")));
}
/**
* Add a location that will be searched for resources
*
* @param location
* The location that will be searched for resoruces
*/
public static void addResourceLocation(ResourceLocation location) {
locations.add(location);
}
/**
* Remove a location that will be no longer be searched for resources
*
* @param location
* The location that will be removed from the search list
*/
public static void removeResourceLocation(ResourceLocation location) {
locations.remove(location);
}
/**
* Remove all the locations, no resources will be found until
* new locations have been added
*/
public static void removeAllResourceLocations() {
locations.clear();
}
/**
* Get a resource
*
* @param ref
* The reference to the resource to retrieve
* @return A stream from which the resource can be read
*/
public static InputStream getResourceAsStream(String ref) {
InputStream in = null;
for (int i = 0; i < locations.size(); i++) {
ResourceLocation location = locations.get(i);
in = location.getResourceAsStream(ref);
if (in != null) {
break;
}
}
if (in == null) {
throw new SlickException("Resource not found: " + ref);
}
return new BufferedInputStream(in);
}
/**
* Check if a resource is available from any given resource loader
*
* @param ref
* A reference to the resource that should be checked
* @return True if the resource can be located
*/
public static boolean resourceExists(String ref) {
URL url = null;
for (int i = 0; i < locations.size(); i++) {
ResourceLocation location = locations.get(i);
url = location.getResource(ref);
if (url != null) {
return true;
}
}
return false;
}
/**
* Get a resource as a URL
*
* @param ref
* The reference to the resource to retrieve
* @return A URL from which the resource can be read
*/
public static URL getResource(String ref) {
URL url = null;
for (int i = 0; i < locations.size(); i++) {
ResourceLocation location = locations.get(i);
url = location.getResource(ref);
if (url != null) {
break;
}
}
if (url == null) {
throw new SlickException("Resource not found: " + ref);
}
return url;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy