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 ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
36
37
38
39
40
41 private static Charset charset;
42
43 Resources() {
44 }
45
46
47
48
49
50
51 public static ClassLoader getDefaultClassLoader() {
52 return classLoaderWrapper.defaultClassLoader;
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 public static URL getResourceURL(String resource) throws IOException {
72
73 return getResourceURL(null, resource);
74 }
75
76
77
78
79
80
81
82
83
84 public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
85 URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
86 if (url == null) {
87 throw new IOException("Could not find resource " + resource);
88 }
89 return url;
90 }
91
92
93
94
95
96
97
98
99 public static InputStream getResourceAsStream(String resource) throws IOException {
100 return getResourceAsStream(null, resource);
101 }
102
103
104
105
106
107
108
109
110
111 public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
112 InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
113 if (in == null) {
114 throw new IOException("Could not find resource " + resource);
115 }
116 return in;
117 }
118
119
120
121
122
123
124
125
126 public static Properties getResourceAsProperties(String resource) throws IOException {
127 Properties props = new Properties();
128 try (InputStream in = getResourceAsStream(resource)) {
129 props.load(in);
130 }
131 return props;
132 }
133
134
135
136
137
138
139
140
141
142 public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
143 Properties props = new Properties();
144 try (InputStream in = getResourceAsStream(loader, resource)) {
145 props.load(in);
146 }
147 return props;
148 }
149
150
151
152
153
154
155
156
157 public static Reader getResourceAsReader(String resource) throws IOException {
158 Reader reader;
159 if (charset == null) {
160 reader = new InputStreamReader(getResourceAsStream(resource));
161 } else {
162 reader = new InputStreamReader(getResourceAsStream(resource), charset);
163 }
164 return reader;
165 }
166
167
168
169
170
171
172
173
174
175 public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
176 Reader reader;
177 if (charset == null) {
178 reader = new InputStreamReader(getResourceAsStream(loader, resource));
179 } else {
180 reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
181 }
182 return reader;
183 }
184
185
186
187
188
189
190
191
192 public static File getResourceAsFile(String resource) throws IOException {
193 return new File(getResourceURL(resource).getFile());
194 }
195
196
197
198
199
200
201
202
203
204 public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
205 return new File(getResourceURL(loader, resource).getFile());
206 }
207
208
209
210
211
212
213
214
215 public static InputStream getUrlAsStream(String urlString) throws IOException {
216 URL url = new URL(urlString);
217 URLConnection conn = url.openConnection();
218 return conn.getInputStream();
219 }
220
221
222
223
224
225
226
227
228 public static Reader getUrlAsReader(String urlString) throws IOException {
229 Reader reader;
230 if (charset == null) {
231 reader = new InputStreamReader(getUrlAsStream(urlString));
232 } else {
233 reader = new InputStreamReader(getUrlAsStream(urlString), charset);
234 }
235 return reader;
236 }
237
238
239
240
241
242
243
244
245 public static Properties getUrlAsProperties(String urlString) throws IOException {
246 Properties props = new Properties();
247 try (InputStream in = getUrlAsStream(urlString)) {
248 props.load(in);
249 }
250 return props;
251 }
252
253
254
255
256
257
258
259
260 public static Class<?> classForName(String className) throws ClassNotFoundException {
261 return classLoaderWrapper.classForName(className);
262 }
263
264 public static Charset getCharset() {
265 return charset;
266 }
267
268 public static void setCharset(Charset charset) {
269 Resources.charset = charset;
270 }
271
272 }