blob: 3b81190bb09639556444c0832fd2942aa57e341e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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();
}
}
}
|