3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.util.io;
21 import java.io.BufferedReader;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
28 import java.nio.charset.Charset;
31 * Provides method for loading of resources.
32 * @author Christian Lins
35 public final class Resource
39 * Loads a resource and returns it as URL reference.
40 * The Resource's classloader is used to load the resource, not
41 * the System's ClassLoader so it may be safe to use this method
42 * in a sandboxed environment.
45 public static URL getAsURL(final String name)
51 return Resource.class.getClassLoader().getResource(name);
55 * Loads a resource and returns an InputStream to it.
59 public static InputStream getAsStream(String name)
62 URL url = getAsURL(name);
64 File file = new File(name);
66 return new FileInputStream(file);
70 return url.openStream();
72 } catch (IOException e) {
79 * Loads a plain text resource.
80 * @param withNewline If false all newlines are removed from the
83 public static String getAsString(String name, boolean withNewline)
89 BufferedReader in = null;
91 InputStream ins = getAsStream(name);
96 in = new BufferedReader(
97 new InputStreamReader(ins, Charset.forName("UTF-8")));
98 StringBuffer buf = new StringBuffer();
101 String line = in.readLine();
112 return buf.toString();
113 } catch (Exception e) {
121 } catch (IOException ex) {
122 ex.printStackTrace();