chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.util.io; chris@1: chris@1: import java.io.BufferedReader; chris@1: import java.io.IOException; chris@1: import java.io.InputStream; chris@1: import java.io.InputStreamReader; chris@1: import java.net.URL; chris@1: import java.nio.charset.Charset; chris@1: chris@1: /** chris@1: * Provides method for loading of resources. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public final class Resource chris@1: { chris@1: cli@37: /** cli@37: * Loads a resource and returns it as URL reference. cli@37: * The Resource's classloader is used to load the resource, not cli@37: * the System's ClassLoader so it may be safe to use this method cli@37: * in a sandboxed environment. cli@37: * @return cli@37: */ cli@37: public static URL getAsURL(final String name) cli@37: { cli@37: if (name == null) { cli@37: return null; cli@37: } chris@1: cli@37: return Resource.class.getClassLoader().getResource(name); cli@37: } chris@1: cli@37: /** cli@37: * Loads a resource and returns an InputStream to it. cli@37: * @param name cli@37: * @return cli@37: */ cli@37: public static InputStream getAsStream(String name) cli@37: { cli@37: try { cli@37: URL url = getAsURL(name); cli@37: if (url == null) { cli@37: return null; cli@37: } else { cli@37: return url.openStream(); cli@37: } cli@37: } catch (IOException e) { cli@37: e.printStackTrace(); cli@37: return null; cli@37: } cli@37: } chris@1: cli@37: /** cli@37: * Loads a plain text resource. cli@37: * @param withNewline If false all newlines are removed from the cli@37: * return String cli@37: */ cli@37: public static String getAsString(String name, boolean withNewline) cli@37: { cli@37: if (name == null) { cli@37: return null; cli@37: } chris@1: cli@37: BufferedReader in = null; cli@37: try { cli@37: InputStream ins = getAsStream(name); cli@37: if (ins == null) { cli@37: return null; cli@37: } chris@1: cli@37: in = new BufferedReader( cli@37: new InputStreamReader(ins, Charset.forName("UTF-8"))); cli@37: StringBuffer buf = new StringBuffer(); chris@1: cli@37: for (;;) { cli@37: String line = in.readLine(); cli@37: if (line == null) { cli@37: break; cli@37: } chris@1: cli@37: buf.append(line); cli@37: if (withNewline) { cli@37: buf.append('\n'); cli@37: } cli@37: } cli@37: cli@37: return buf.toString(); cli@37: } catch (Exception e) { cli@37: e.printStackTrace(); cli@37: return null; cli@37: } finally { cli@37: try { cli@37: if (in != null) { cli@37: in.close(); cli@37: } cli@37: } catch (IOException ex) { cli@37: ex.printStackTrace(); cli@37: } cli@37: } cli@37: } chris@1: }