src/org/sonews/util/io/Resource.java
author cli
Sun, 29 Aug 2010 18:17:37 +0200
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 44 5d7d1adf387f
permissions -rw-r--r--
Switch intent style to Original K&R / Linux / Kernel.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 package org.sonews.util.io;
    20 
    21 import java.io.BufferedReader;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.InputStreamReader;
    25 import java.net.URL;
    26 import java.nio.charset.Charset;
    27 
    28 /**
    29  * Provides method for loading of resources.
    30  * @author Christian Lins
    31  * @since sonews/0.5.0
    32  */
    33 public final class Resource
    34 {
    35 
    36 	/**
    37 	 * Loads a resource and returns it as URL reference.
    38 	 * The Resource's classloader is used to load the resource, not
    39 	 * the System's ClassLoader so it may be safe to use this method
    40 	 * in a sandboxed environment.
    41 	 * @return
    42 	 */
    43 	public static URL getAsURL(final String name)
    44 	{
    45 		if (name == null) {
    46 			return null;
    47 		}
    48 
    49 		return Resource.class.getClassLoader().getResource(name);
    50 	}
    51 
    52 	/**
    53 	 * Loads a resource and returns an InputStream to it.
    54 	 * @param name
    55 	 * @return
    56 	 */
    57 	public static InputStream getAsStream(String name)
    58 	{
    59 		try {
    60 			URL url = getAsURL(name);
    61 			if (url == null) {
    62 				return null;
    63 			} else {
    64 				return url.openStream();
    65 			}
    66 		} catch (IOException e) {
    67 			e.printStackTrace();
    68 			return null;
    69 		}
    70 	}
    71 
    72 	/**
    73 	 * Loads a plain text resource.
    74 	 * @param withNewline If false all newlines are removed from the
    75 	 * return String
    76 	 */
    77 	public static String getAsString(String name, boolean withNewline)
    78 	{
    79 		if (name == null) {
    80 			return null;
    81 		}
    82 
    83 		BufferedReader in = null;
    84 		try {
    85 			InputStream ins = getAsStream(name);
    86 			if (ins == null) {
    87 				return null;
    88 			}
    89 
    90 			in = new BufferedReader(
    91 				new InputStreamReader(ins, Charset.forName("UTF-8")));
    92 			StringBuffer buf = new StringBuffer();
    93 
    94 			for (;;) {
    95 				String line = in.readLine();
    96 				if (line == null) {
    97 					break;
    98 				}
    99 
   100 				buf.append(line);
   101 				if (withNewline) {
   102 					buf.append('\n');
   103 				}
   104 			}
   105 
   106 			return buf.toString();
   107 		} catch (Exception e) {
   108 			e.printStackTrace();
   109 			return null;
   110 		} finally {
   111 			try {
   112 				if (in != null) {
   113 					in.close();
   114 				}
   115 			} catch (IOException ex) {
   116 				ex.printStackTrace();
   117 			}
   118 		}
   119 	}
   120 }