-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathResource.java
More file actions
executable file
·57 lines (48 loc) · 1.54 KB
/
Copy pathResource.java
File metadata and controls
executable file
·57 lines (48 loc) · 1.54 KB
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
package javaforce.webui;
import java.io.InputStream;
import java.util.HashMap;
import javaforce.*;
/** Resource
*
* @author pquiring
*/
public class Resource {
public byte[] data;
public String id;
public String mime;
public static final String PNG = "image/png";
public static final String JPG = "image/jpeg";
public static final String JPEG = "image/jpeg";
public static final String GIF = "image/gif";
private static int nextID;
private static HashMap<String, Resource> resList = new HashMap<String, Resource>();
private static ClassLoader loader = Resource.class.getClassLoader();
public static void setClassLoader(ClassLoader loader) {
Resource.loader = loader;
}
public static synchronized Resource registerResource(byte[] data, String mime) {
Resource res = new Resource();
res.data = data;
res.id = "r" + nextID++;
res.mime = mime;
resList.put(res.id, res);
return res;
}
public static Resource readResource(String name, String mime) {
InputStream is = loader.getResourceAsStream(name);
if (is == null) {
JFLog.log("Error:Resource not found:" + name);
return null;
}
byte[] data = JF.readAll(is);
try {is.close();} catch (Exception e) {}
return registerResource(data, mime);
}
public static Resource readStream(InputStream is, String mime) {
byte[] data = JF.readAll(is);
return registerResource(data, mime);
}
public static Resource getResource(String id) {
return resList.get(id);
}
}