1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sourceforge.addam.ddlrun.utils;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.MissingResourceException;
29
30 /**
31 * Used to read resources from within the local classloader.
32 *
33 * @author TIM3
34 * @since Mar 5, 2005
35 */
36 public class ClassLoaderReader implements ResourceReader {
37
38 /**
39 * Allows only the ClassLoaderReaderFactory to construct a ClassLoaderReader.
40 *
41 * @param resourcePath the path to prepend when locating a resource
42 * @param resourceName the name of the resource to locate
43 */
44 ClassLoaderReader(String resourcePath, String resourceName) {
45 if (!resourcePath.endsWith(SEPARATOR) && resourcePath.length() > 0) {
46 resourcePath += SEPARATOR;
47 }
48 this.resourcePath = resourcePath;
49 this.resourceName = resourceName;
50 this.completeName = resourcePath + resourceName;
51 }
52
53 public String readLine() throws IOException {
54 return this.getReader().readLine();
55 }
56
57 private BufferedReader getReader() {
58 if (reader == null) {
59 InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(completeName);
60 if (resourceStream == null) {
61 throw new MissingResourceException("ClassLoader unable to locate resource " + completeName,
62 ClassLoaderReader.class.getName(), resourceName);
63 }
64 reader = new BufferedReader(new InputStreamReader(resourceStream));
65 }
66 return reader;
67 }
68
69 private BufferedReader reader;
70 final String resourcePath;
71 final String resourceName;
72 final String completeName;
73 private static final String SEPARATOR = "/";
74 }