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