1 package net.trajano.commons.testing;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.UnsupportedEncodingException;
9
10
11
12
13
14
15 public final class ResourceUtil {
16
17
18
19
20
21
22
23
24
25
26 public static byte[] getResourceAsBytes(final Class<?> clazz,
27 final String name) {
28
29 try (InputStream is = getResourceAsStream(clazz, name)) {
30 return streamToBytes(is);
31 } catch (final IOException e) {
32 throw new AssertionError(e.getMessage(), e);
33 }
34 }
35
36
37
38
39
40
41
42
43 public static byte[] getResourceAsBytes(final String name) {
44
45 try (InputStream is = getResourceAsStream(name)) {
46 return streamToBytes(is);
47 } catch (final IOException e) {
48 throw new AssertionError(e.getMessage(), e);
49 }
50 }
51
52
53
54
55
56
57
58
59
60
61
62 public static InputStream getResourceAsStream(final Class<?> clazz,
63 final String name) {
64
65 final InputStream is = clazz.getResourceAsStream(name);
66 if (is == null) {
67 throw new AssertionError(String.format("Unable to locate resource %s in %s", name, clazz));
68 }
69 return is;
70 }
71
72
73
74
75
76
77
78
79
80 public static InputStream getResourceAsStream(final String name) {
81
82 final InputStream is = Thread.currentThread()
83 .getContextClassLoader()
84 .getResourceAsStream(name);
85 if (is == null) {
86 throw new AssertionError(String.format("Unable to locate resource %s in context class loader", name));
87 }
88 return is;
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 public static String getResourceAsString(final Class<?> clazz,
103 final String name) {
104
105 try {
106 return new String(getResourceAsBytes(clazz, name), "UTF-8");
107 } catch (final UnsupportedEncodingException e) {
108 throw new AssertionError(e.getMessage(), e);
109 }
110 }
111
112
113
114
115
116
117
118
119
120 public static String getResourceAsString(final String name) {
121
122 try {
123 return new String(getResourceAsBytes(name), "UTF-8");
124 } catch (final UnsupportedEncodingException e) {
125 throw new AssertionError(e.getMessage(), e);
126 }
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140 public static File getResourceCopy(final Class<?> clazz,
141 final String name) {
142
143 try (InputStream is = getResourceAsStream(clazz, name)) {
144 return streamToTempFile(is);
145 } catch (final IOException e) {
146 throw new AssertionError(e.getMessage(), e);
147 }
148 }
149
150
151
152
153
154
155
156
157
158
159 public static File getResourceCopy(final String name) {
160
161 try (InputStream is = getResourceAsStream(name)) {
162 return streamToTempFile(is);
163 } catch (final IOException e) {
164 throw new AssertionError(e.getMessage(), e);
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177 public static byte[] streamToBytes(final InputStream is) {
178
179 try {
180 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
181 int c = is.read();
182 while (c != -1) {
183 baos.write(c);
184 c = is.read();
185 }
186 baos.close();
187 return baos.toByteArray();
188 } catch (final IOException e) {
189 throw new AssertionError(e.getMessage(), e);
190 }
191 }
192
193
194
195
196
197
198
199
200
201
202
203 public static File streamToTempFile(final InputStream is) {
204
205 try {
206 final File file = File.createTempFile("test", ".tmp");
207 final FileOutputStream os = new FileOutputStream(file);
208
209 int c = is.read();
210 while (c != -1) {
211 os.write(c);
212 c = is.read();
213 }
214 os.close();
215 return file;
216 } catch (final IOException e) {
217 throw new AssertionError(e.getMessage(), e);
218 }
219 }
220
221
222
223
224 private ResourceUtil() {
225 }
226 }