summaryrefslogtreecommitdiffstats
path: root/src/net/java/joglutils
diff options
context:
space:
mode:
authorrodgersgb <[email protected]>2008-04-29 19:48:44 +0000
committerrodgersgb <[email protected]>2008-04-29 19:48:44 +0000
commit273f6cd707d1805fe42025e65556875526a4a96f (patch)
treee3c8f8aef8268e2191c9e28686e47e73f822e165 /src/net/java/joglutils
parent912c5125526d01862307d66cf5cd36c994283f74 (diff)
Added a file remotely
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/joglutils/trunk@86 83d24430-9974-4f80-8418-2cc3294053b9
Diffstat (limited to 'src/net/java/joglutils')
-rw-r--r--src/net/java/joglutils/model/ResourceRetriever.java109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/net/java/joglutils/model/ResourceRetriever.java b/src/net/java/joglutils/model/ResourceRetriever.java
new file mode 100644
index 0000000..3b81190
--- /dev/null
+++ b/src/net/java/joglutils/model/ResourceRetriever.java
@@ -0,0 +1,109 @@
+package net.java.joglutils.model;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.MalformedURLException;
+
+/**
+ * Utility class that allows transparent reading of files from the current
+ * working directory or from the classpath.
+ * <p>
+ * Based on code from author Pepijn Van Eeckhoudt
+ */
+public class ResourceRetriever {
+
+ /**
+ * Retrieves a resource as a URL that corresponds to the file specified
+ * by the passed in filename.
+ *
+ * @param filename The name of the file to retrieve as a URL
+ * @throws IOException
+ * @return URL that corresponds to the filename
+ */
+ public static URL getResource( final String filename ) throws IOException {
+ // Try to load resource from jar
+ URL url = ResourceRetriever.class.getClassLoader().getResource(filename);
+
+ // If not found in jar, then load from disk
+ if (url == null) {
+ return new URL("file", "localhost", filename);
+ } else {
+ return url;
+ }
+ }
+
+ /**
+ * Retrieves a resource as an InputStream that corresponds to the file
+ * specified by the passed in filename.
+ *
+ * @param filename The name of the file to retrieve as an InputStream
+ * @throws IOException
+ * @return InputStream that corresponds to the filename
+ */
+ public static InputStream getResourceAsStream( final String filename ) throws IOException {
+ // Try to load resource from jar
+ String convertedFileName = filename.replace('\\', '/');
+ InputStream stream = ResourceRetriever.class.getClassLoader().getResourceAsStream(convertedFileName);
+ // If not found in jar, then load from disk
+ if (stream == null) {
+ return new FileInputStream(convertedFileName);
+ } else {
+ return stream;
+ }
+ }
+
+ /**
+ * Retrieves a resource as a URL that corresponds to the file specified
+ * by the passed in filename.
+ *
+ * @param filename The name of the file to retrieve as a URL
+ * @throws IOException
+ * @return URL that corresponds to the filename
+ */
+ public static URL getResourceAsUrl( final String filename ) throws IOException {
+ URL result;
+
+ try {
+ result = new URL(filename);
+ } catch (MalformedURLException e) {
+ // When the string was not a valid URL, try to load it as a resource using
+ // an anonymous class in the tree.
+ Object objectpart = new Object() { };
+ Class classpart = objectpart.getClass();
+ ClassLoader loaderpart = classpart.getClassLoader();
+ result = loaderpart.getResource(filename);
+
+ if (result == null) {
+ result = new URL("file", "localhost", filename);
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Retrieves a resource as an InputStream that corresponds to the file
+ * specified by the passed in filename.
+ *
+ * @param filename The name of the file to retrieve as an InputStream
+ * @throws IOException
+ * @return InputStream that corresponds to the filename
+ */
+ public static InputStream getResourceAsInputStream( final String filename ) throws IOException {
+ URL result;
+
+ try {
+ result = getResourceAsUrl(filename);
+ } catch(IOException e) {
+ return new FileInputStream(filename);
+ }
+
+ if (result == null) {
+ return new FileInputStream(filename);
+ } else {
+ return result.openStream();
+ }
+ }
+} \ No newline at end of file