1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/util/io/Resource.java Fri Jun 26 16:48:50 2009 +0200
1.3 @@ -0,0 +1,132 @@
1.4 +/*
1.5 + * SONEWS 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 org.sonews.util.io;
1.23 +
1.24 +import java.io.BufferedReader;
1.25 +import java.io.IOException;
1.26 +import java.io.InputStream;
1.27 +import java.io.InputStreamReader;
1.28 +import java.net.URL;
1.29 +import java.nio.charset.Charset;
1.30 +
1.31 +/**
1.32 + * Provides method for loading of resources.
1.33 + * @author Christian Lins
1.34 + * @since sonews/0.5.0
1.35 + */
1.36 +public final class Resource
1.37 +{
1.38 +
1.39 + /**
1.40 + * Loads a resource and returns it as URL reference.
1.41 + * The Resource's classloader is used to load the resource, not
1.42 + * the System's ClassLoader so it may be safe to use this method
1.43 + * in a sandboxed environment.
1.44 + * @return
1.45 + */
1.46 + public static URL getAsURL(final String name)
1.47 + {
1.48 + if(name == null)
1.49 + {
1.50 + return null;
1.51 + }
1.52 +
1.53 + return Resource.class.getClassLoader().getResource(name);
1.54 + }
1.55 +
1.56 + /**
1.57 + * Loads a resource and returns an InputStream to it.
1.58 + * @param name
1.59 + * @return
1.60 + */
1.61 + public static InputStream getAsStream(String name)
1.62 + {
1.63 + try
1.64 + {
1.65 + URL url = getAsURL(name);
1.66 + if(url == null)
1.67 + {
1.68 + return null;
1.69 + }
1.70 + else
1.71 + {
1.72 + return url.openStream();
1.73 + }
1.74 + }
1.75 + catch(IOException e)
1.76 + {
1.77 + e.printStackTrace();
1.78 + return null;
1.79 + }
1.80 + }
1.81 +
1.82 + /**
1.83 + * Loads a plain text resource.
1.84 + * @param withNewline If false all newlines are removed from the
1.85 + * return String
1.86 + */
1.87 + public static String getAsString(String name, boolean withNewline)
1.88 + {
1.89 + if(name == null)
1.90 + return null;
1.91 +
1.92 + BufferedReader in = null;
1.93 + try
1.94 + {
1.95 + InputStream ins = getAsStream(name);
1.96 + if(ins == null)
1.97 + return null;
1.98 +
1.99 + in = new BufferedReader(
1.100 + new InputStreamReader(ins, Charset.forName("UTF-8")));
1.101 + StringBuffer buf = new StringBuffer();
1.102 +
1.103 + for(;;)
1.104 + {
1.105 + String line = in.readLine();
1.106 + if(line == null)
1.107 + break;
1.108 +
1.109 + buf.append(line);
1.110 + if(withNewline)
1.111 + buf.append('\n');
1.112 + }
1.113 +
1.114 + return buf.toString();
1.115 + }
1.116 + catch(Exception e)
1.117 + {
1.118 + e.printStackTrace();
1.119 + return null;
1.120 + }
1.121 + finally
1.122 + {
1.123 + try
1.124 + {
1.125 + if(in != null)
1.126 + in.close();
1.127 + }
1.128 + catch(IOException ex)
1.129 + {
1.130 + ex.printStackTrace();
1.131 + }
1.132 + }
1.133 + }
1.134 +
1.135 +}