1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.io;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.net.URL;
24 import java.net.URLConnection;
25 import java.nio.charset.Charset;
26 import java.util.Properties;
27
28
29
30
31
32
33 public class Resources {
34
35 private static final ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
36
37
38
39
40 private static Charset charset;
41
42 private Resources() {
43 }
44
45
46
47
48
49
50 public static ClassLoader getDefaultClassLoader() {
51 return classLoaderWrapper.defaultClassLoader;
52 }
53
54
55
56
57
58
59
60 public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
61 classLoaderWrapper.defaultClassLoader = defaultClassLoader;
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75 public static URL getResourceURL(String resource) throws IOException {
76
77 return getResourceURL(null, resource);
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
94 URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
95 if (url == null) {
96 throw new IOException("Could not find resource " + resource);
97 }
98 return url;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112 public static InputStream getResourceAsStream(String resource) throws IOException {
113 return getResourceAsStream(null, resource);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
130 InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
131 if (in == null) {
132 throw new IOException("Could not find resource " + resource);
133 }
134 return in;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 public static Properties getResourceAsProperties(String resource) throws IOException {
149 Properties props = new Properties();
150 try (InputStream in = getResourceAsStream(resource)) {
151 props.load(in);
152 }
153 return props;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
170 Properties props = new Properties();
171 try (InputStream in = getResourceAsStream(loader, resource)) {
172 props.load(in);
173 }
174 return props;
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188 public static Reader getResourceAsReader(String resource) throws IOException {
189 Reader reader;
190 if (charset == null) {
191 reader = new InputStreamReader(getResourceAsStream(resource));
192 } else {
193 reader = new InputStreamReader(getResourceAsStream(resource), charset);
194 }
195 return reader;
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
212 Reader reader;
213 if (charset == null) {
214 reader = new InputStreamReader(getResourceAsStream(loader, resource));
215 } else {
216 reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
217 }
218 return reader;
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232 public static File getResourceAsFile(String resource) throws IOException {
233 return new File(getResourceURL(resource).getFile());
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
250 return new File(getResourceURL(loader, resource).getFile());
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264 public static InputStream getUrlAsStream(String urlString) throws IOException {
265 URL url = new URL(urlString);
266 URLConnection conn = url.openConnection();
267 return conn.getInputStream();
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281 public static Reader getUrlAsReader(String urlString) throws IOException {
282 Reader reader;
283 if (charset == null) {
284 reader = new InputStreamReader(getUrlAsStream(urlString));
285 } else {
286 reader = new InputStreamReader(getUrlAsStream(urlString), charset);
287 }
288 return reader;
289 }
290
291
292
293
294
295
296
297
298
299
300
301
302 public static Properties getUrlAsProperties(String urlString) throws IOException {
303 Properties props = new Properties();
304 try (InputStream in = getUrlAsStream(urlString)) {
305 props.load(in);
306 }
307 return props;
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321 public static Class<?> classForName(String className) throws ClassNotFoundException {
322 return classLoaderWrapper.classForName(className);
323 }
324
325 public static Charset getCharset() {
326 return charset;
327 }
328
329 public static void setCharset(Charset charset) {
330 Resources.charset = charset;
331 }
332
333 }