blob: 656e3a81890c2be821ce91c28fa4c11962ae5e21 (
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package ru.olamedia.texture;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
public class ResourceUtil {
private static URL baseURL;
private static ResourceUtil instance;
public static ResourceUtil getInstance() {
if (null == instance) {
instance = new ResourceUtil();
}
return instance;
}
public static URL getInternalBaseURL() {
if (null == baseURL) {
URL url = getInstance().getClass().getResource("ResourceUtil.class");
// URL back = null;
// try {
// back = new URL(url, "..");
// } catch (MalformedURLException e1) {
// e1.printStackTrace();
// }
// System.out.println("Back:" + back);
// System.out.println("Class:" + url);
int p = url.toString().indexOf("jar!");
if (p > 0) {
// in local jar:
// jar:file:/E:/com/mindprod/thepackage/thepackage.jar!/com/mindprod/thepackage/images/blueball.gif
// in remote jar:
// jar:http://mindprod.com/thepackage.jar!/com/mindprod/thepackage/images/blueball.gif
try {
baseURL = new URL(url.toString().substring(0, p + 4));
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else {
// in local file:
// file:/E:/com/mindprod/thepackage/images/blueball.gif
// in remote file:
// http://mindprod.com/com/mindprod/the...s/blueball.gif
int l = url.toString().length() - (ResourceUtil.class.toString()).length();
try {
baseURL = new URL(url.toString().substring(0, l));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
return baseURL;
}
public static String getInternalFilename(String fn) {
return getInternalBaseURL() + fn;
}
public static URL getInternalResource(String internalPath) throws MalformedURLException {
URL url = ResourceUtil.class.getClassLoader().getResource(internalPath);
if (url == null) {
System.out.println(internalPath + " not found");
} else {
System.out.println(url.toString());
}
return url;
// return new URL(getInternalBaseURL(), internalPath);
}
public static URL getURL(String internalPath) {
try {
return new URL(getInternalBaseURL(), internalPath);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
public static String getFilename(String internalPath) {
try {
URL url = new URL(getInternalBaseURL(), internalPath);
return url.getFile();
} catch (MalformedURLException e) {
System.err.println("Problems with " + internalPath);
e.printStackTrace();
}
return null;
}
public static InputStream getInternalInputStream(String internalPath) {
try {
return getInternalResource(internalPath).openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static File getFile(String fn) {
URL url = null;
try {
url = new URL(getInternalFilename(fn));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
System.out.println("Fn:" + fn);
System.out.println("Base:" + getInternalBaseURL());
System.out.println("Internal:" + getInternalFilename(fn));
System.out.println("Url:" + url);
File f;
try {
f = new File(url.toURI());
} catch (URISyntaxException e) {
f = new File(url.getPath());
}
return f;
}
}
|