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;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
26 import java.nio.charset.Charset;
29 * Provides method for loading of resources.
30 * @author Christian Lins
33 public final class Resource
37 * Loads a resource and returns it as URL reference.
38 * The Resource's classloader is used to load the resource, not
39 * the System's ClassLoader so it may be safe to use this method
40 * in a sandboxed environment.
43 public static URL getAsURL(final String name)
49 return Resource.class.getClassLoader().getResource(name);
53 * Loads a resource and returns an InputStream to it.
57 public static InputStream getAsStream(String name)
60 URL url = getAsURL(name);
64 return url.openStream();
66 } catch (IOException e) {
73 * Loads a plain text resource.
74 * @param withNewline If false all newlines are removed from the
77 public static String getAsString(String name, boolean withNewline)
83 BufferedReader in = null;
85 InputStream ins = getAsStream(name);
90 in = new BufferedReader(
91 new InputStreamReader(ins, Charset.forName("UTF-8")));
92 StringBuffer buf = new StringBuffer();
95 String line = in.readLine();
106 return buf.toString();
107 } catch (Exception e) {
115 } catch (IOException ex) {
116 ex.printStackTrace();