1
2
3
4
5
6
7
8
9
10
11
12
13
14 package gr.abiss.mvn.plugins.jstools.utils;
15
16 import java.io.BufferedReader;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import org.apache.log4j.Logger;
22
23 /***
24 * Utility class to load classpath resources
25 * @author manos
26 */
27 public class ClasspathResourceUtils {
28
29 private static Logger log = Logger.getLogger(ClasspathResourceUtils.class);
30
31 /***
32 * Get the content of the classpath resource as an InputStream.
33 * @param path the path representing the classpath resource
34 * @return the resource as an InputStream
35 * @throws ClasspathResourceDiscoveryException if the resource cannot be found
36 */
37 public static InputStream getResourceAsStream(String path) {
38 InputStream is = getResourceAsStreamOrNull(path);
39 if (is == null) {
40 throw new ClasspathResourceDiscoveryException("Could not find resource in classpath: "
41 + path);
42 }
43 return is;
44 }
45
46 /***
47 * Get the content of the classpath resource as an InputStream.
48 * @param path the path representing the classpath resource
49 * @return the resource as an InputStream or null
50 */
51 public static byte[] getResourceBytesOrNull(String path){
52 InputStream is = getResourceAsStreamOrNull(path);
53 byte[] data = null;
54 if(is != null){
55 ByteArrayOutputStream os = new ByteArrayOutputStream();
56 int readP;
57 byte[] bufferP=new byte[1024];
58 try {
59 while((readP=is.read(bufferP))>-1) {
60 os.write(bufferP,0,readP);
61 }
62 } catch (IOException e) {
63 throw new ClasspathResourceDiscoveryException("Could not read resource into memory");
64 }
65 data = os.toByteArray();
66 }
67 return data;
68 }
69
70 /***
71 * Get the content of the classpath resource as an InputStream.
72 * @param path the path representing the classpath resource
73 * @return the resource as an InputStream or null
74 */
75 private static InputStream getResourceAsStreamOrNull(String path) {
76
77
78 return Thread.currentThread().getContextClassLoader()
79 .getResourceAsStream(path);
80 }
81
82 /***
83 * Get the content of the text-based classpath resource as a String.
84 * @param path
85 * @return The content of the resource as a String or null
86 */
87 public static String getResourceAsStringOrNull(String path) {
88 String s = null;
89 InputStream is = getResourceAsStreamOrNull(path);
90 if(log.isDebugEnabled()){
91 log.debug("Result stream of path ["+path+"]: "+is);
92 }
93 if (is != null) {
94 StringBuffer sb;
95 try {
96 sb = inputStreamToStringBuffer(is);
97 } catch (IOException e) {
98 throw new ClasspathResourceDiscoveryException("Could not load classpath resource into memory", e);
99 }
100 s = sb.toString();
101 }
102 return s;
103 }
104
105 /***
106 * Construct a StringBuffer from the text content of the
107 * classpath resource represented by the given path
108 * @param is
109 * @return
110 * @throws IOException
111 */
112 private static StringBuffer inputStreamToStringBuffer(InputStream is) throws IOException {
113 BufferedReader reader = new BufferedReader(
114 new InputStreamReader(is));
115 StringBuffer sb = new StringBuffer();
116
117
118 char[] charBuffer = new char[4096];
119 int nbCharRead = 0;
120 while ((nbCharRead = reader.read(charBuffer)) != -1) {
121
122 sb.append(charBuffer, 0, nbCharRead);
123 }
124 reader.close();
125 return sb;
126 }
127 }