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 * Provides a utility class to load up resource streams.
12 *
13 * @author Archimedes Trajano
14 */
15 public final class ResourceUtil {
16
17 /**
18 * Reads a resource as bytes from the the given class' class loader.
19 *
20 * @param clazz
21 * class
22 * @param name
23 * name of the resource
24 * @return input stream
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 * Reads a resource as bytes from the context class loader.
38 *
39 * @param name
40 * name of the resource
41 * @return input stream
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 * Reads a resource as a stream from the the given class' class loader.
54 * Callers are expected to close the resource after.
55 *
56 * @param clazz
57 * class
58 * @param name
59 * name of the resource
60 * @return input stream
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 * Reads a resource as a stream from the context class loader. Callers are
74 * expected to close the resource after.
75 *
76 * @param name
77 * name of the resource
78 * @return input stream
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 * Reads a resource as a string from the the given class' class loader.
93 * Callers are expected to close the resource after. The data is expected to
94 * be UTF-8.
95 *
96 * @param clazz
97 * class
98 * @param name
99 * name of the resource
100 * @return input stream
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 * Reads a resource as a String from the from the context class loader.
114 * Callers are expected to close the resource after.
115 *
116 * @param name
117 * name of the resource
118 * @return string
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 * Creates a copy of the resource from the given class' class loader to a
131 * temporary file. It is expected that the test will delete the file
132 * afterwards.
133 *
134 * @param clazz
135 * class
136 * @param name
137 * name of the resource
138 * @return temporary file
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 * Creates a copy of the resource from the context class loader to a
152 * temporary file. It is expected that the test will delete the file
153 * afterwards.
154 *
155 * @param name
156 * name of the resource
157 * @return temporary file
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 * Converts an {@link InputStream} to bytes. This will not throw an
170 * {@link IOException} but wraps it in an {@link AssertionError}. This reads
171 * the {@link InputStream} to end of file, but does not close it.
172 *
173 * @param is
174 * input stream
175 * @return byte array.
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 * Copies the contents of an {@link InputStream} to a temporary file. This
195 * will not throw an {@link IOException} but wraps it in an
196 * {@link AssertionError}. This reads the {@link InputStream} to end of
197 * file, but does not close it.
198 *
199 * @param is
200 * input stream
201 * @return temporary file.
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 * Prevent instantiation of utility class.
223 */
224 private ResourceUtil() {
225 }
226 }