Initial import.
2 * StarOffice News Server
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 com.so.news.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
37 * Loads a file as array of byte. As the file is completely loaded into
38 * memory this method should only be used with small files.
42 public static byte[] getBytes(File file)
46 FileInputStream in = new FileInputStream(file);
47 byte[] buffer = new byte[(int)file.length()];
55 System.err.println(ex.getLocalizedMessage());
61 * Loads a resource and returns it as URL reference.
62 * The Resource's classloader is used to load the resource, not
63 * the System's ClassLoader so it may be safe to use this method
64 * in a sandboxed environment.
67 public static URL getAsURL(String name)
69 return Resource.class.getClassLoader().getResource(name);
73 * Loads a resource and returns an InputStream to it.
77 public static InputStream getAsStream(String name)
81 URL url = getAsURL(name);
82 return url.openStream();
92 * Loads a plain text resource.
93 * @param withNewline If false all newlines are removed from the
96 public static String getAsString(String name, boolean withNewline)
100 BufferedReader in = new BufferedReader(
101 new InputStreamReader(getAsStream(name), Charset.forName("UTF-8")));
102 StringBuffer buf = new StringBuffer();
106 String line = in.readLine();
115 return buf.toString();