KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > util > ResourceUtils


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.media.util;
9
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.net.URL JavaDoc;
18
19 import org.jboss.util.stream.Streams;
20
21 /**
22  * Resource utilities.
23  *
24  * @version <tt>$Revision: 1.1 $</tt>
25  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo Argüello</a>
26  */

27 public class ResourceUtils
28 {
29    public static InputStream JavaDoc createStreamFromResource(String JavaDoc resourceName)
30    {
31       ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
32       return classLoader.getResourceAsStream(resourceName);
33    }
34
35    public static File JavaDoc createFileFromResource(
36       String JavaDoc resourceName,
37       String JavaDoc filePrefix,
38       String JavaDoc fileSuffix)
39       throws IOException JavaDoc
40    {
41       InputStream JavaDoc inputStream = createStreamFromResource(resourceName);
42
43       try
44       {
45          File JavaDoc tempFile = File.createTempFile(filePrefix, fileSuffix);
46          tempFile.deleteOnExit();
47          OutputStream JavaDoc tempFileStream = new FileOutputStream JavaDoc(tempFile);
48
49          try
50          {
51             Streams.copyb(inputStream, tempFileStream);
52          }
53          finally
54          {
55             tempFileStream.close();
56          }
57
58          return tempFile;
59       }
60       finally
61       {
62          inputStream.close();
63       }
64    }
65
66    /**
67     * Return the content of the resource provided as a byte array.
68     *
69     * @param resource name.
70     * @return content as a byte array.
71     */

72    public static byte[] loadBinaryData(String JavaDoc resourceName) throws IOException JavaDoc
73    {
74       InputStream JavaDoc inputStream =
75          ResourceUtils.createStreamFromResource(resourceName);
76
77       try
78       {
79          ByteArrayOutputStream JavaDoc ouputStream = new ByteArrayOutputStream JavaDoc();
80          try
81          {
82             int byteRead;
83             while ((byteRead = inputStream.read()) != -1)
84                ouputStream.write(byteRead);
85             return ouputStream.toByteArray();
86          }
87          finally
88          {
89             ouputStream.close();
90          }
91       }
92       finally
93       {
94          inputStream.close();
95       }
96    }
97
98    /**
99     * Return the content of the resource provided as a String.
100     *
101     * @param resource name.
102     * @return content as a string.
103     */

104    public static String JavaDoc loadTextData(String JavaDoc resourceName) throws IOException JavaDoc
105    {
106       InputStream JavaDoc inputStream =
107          ResourceUtils.createStreamFromResource(resourceName);
108
109       try
110       {
111          StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
112          try
113          {
114             int byteRead;
115             while ((byteRead = inputStream.read()) != -1)
116                stringWriter.write(byteRead);
117             return stringWriter.toString();
118          }
119          finally
120          {
121             stringWriter.close();
122          }
123       }
124       finally
125       {
126          inputStream.close();
127       }
128    }
129
130    public static InputStream JavaDoc getResourceStream(String JavaDoc resource)
131    {
132       return Thread
133          .currentThread()
134          .getContextClassLoader()
135          .getResourceAsStream(
136          resource);
137    }
138
139    public static File JavaDoc getResourceFile(String JavaDoc resource)
140    {
141       URL JavaDoc u =
142          Thread.currentThread().getContextClassLoader().getResource(resource);
143       String JavaDoc file = u.toExternalForm();
144       return new File JavaDoc(file.substring(6, file.length()));
145    }
146 }
147
Popular Tags