1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/trunk/com/so/news/io/Resource.java Tue Jan 20 10:21:03 2009 +0100
1.3 @@ -0,0 +1,123 @@
1.4 +/*
1.5 + * StarOffice News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package com.so.news.io;
1.23 +
1.24 +import java.io.BufferedReader;
1.25 +import java.io.File;
1.26 +import java.io.FileInputStream;
1.27 +import java.io.IOException;
1.28 +import java.io.InputStream;
1.29 +import java.io.InputStreamReader;
1.30 +import java.net.URL;
1.31 +import java.nio.charset.Charset;
1.32 +
1.33 +/**
1.34 + * Provides method for loading of resources.
1.35 + * @author Christian Lins
1.36 + */
1.37 +public class Resource
1.38 +{
1.39 + /**
1.40 + * Loads a file as array of byte. As the file is completely loaded into
1.41 + * memory this method should only be used with small files.
1.42 + * @param file
1.43 + * @return
1.44 + */
1.45 + public static byte[] getBytes(File file)
1.46 + {
1.47 + try
1.48 + {
1.49 + FileInputStream in = new FileInputStream(file);
1.50 + byte[] buffer = new byte[(int)file.length()];
1.51 +
1.52 + in.read(buffer);
1.53 +
1.54 + return buffer;
1.55 + }
1.56 + catch(IOException ex)
1.57 + {
1.58 + System.err.println(ex.getLocalizedMessage());
1.59 + return null;
1.60 + }
1.61 + }
1.62 +
1.63 + /**
1.64 + * Loads a resource and returns it as URL reference.
1.65 + * The Resource's classloader is used to load the resource, not
1.66 + * the System's ClassLoader so it may be safe to use this method
1.67 + * in a sandboxed environment.
1.68 + * @return
1.69 + */
1.70 + public static URL getAsURL(String name)
1.71 + {
1.72 + return Resource.class.getClassLoader().getResource(name);
1.73 + }
1.74 +
1.75 + /**
1.76 + * Loads a resource and returns an InputStream to it.
1.77 + * @param name
1.78 + * @return
1.79 + */
1.80 + public static InputStream getAsStream(String name)
1.81 + {
1.82 + try
1.83 + {
1.84 + URL url = getAsURL(name);
1.85 + return url.openStream();
1.86 + }
1.87 + catch(IOException e)
1.88 + {
1.89 + e.printStackTrace();
1.90 + return null;
1.91 + }
1.92 + }
1.93 +
1.94 + /**
1.95 + * Loads a plain text resource.
1.96 + * @param withNewline If false all newlines are removed from the
1.97 + * return String
1.98 + */
1.99 + public static String getAsString(String name, boolean withNewline)
1.100 + {
1.101 + try
1.102 + {
1.103 + BufferedReader in = new BufferedReader(
1.104 + new InputStreamReader(getAsStream(name), Charset.forName("UTF-8")));
1.105 + StringBuffer buf = new StringBuffer();
1.106 +
1.107 + for(;;)
1.108 + {
1.109 + String line = in.readLine();
1.110 + if(line == null)
1.111 + break;
1.112 +
1.113 + buf.append(line);
1.114 + if(withNewline)
1.115 + buf.append('\n');
1.116 + }
1.117 +
1.118 + return buf.toString();
1.119 + }
1.120 + catch(Exception e)
1.121 + {
1.122 + e.printStackTrace();
1.123 + return null;
1.124 + }
1.125 + }
1.126 +}