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