src/org/sonews/util/io/Resource.java
author Christian Lins <christian@lins.me>
Wed Sep 14 23:25:00 2011 +0200 (2011-09-14)
changeset 62 be4e87479855
parent 37 74139325d305
permissions -rwxr-xr-x
Reformatting XDaemonCommand
     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.File;
    23 import java.io.FileInputStream;
    24 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.InputStreamReader;
    27 import java.net.URL;
    28 import java.nio.charset.Charset;
    29 
    30 /**
    31  * Provides method for loading of resources.
    32  * @author Christian Lins
    33  * @since sonews/0.5.0
    34  */
    35 public final class Resource
    36 {
    37 
    38 	/**
    39 	 * Loads a resource and returns it as URL reference.
    40 	 * The Resource's classloader is used to load the resource, not
    41 	 * the System's ClassLoader so it may be safe to use this method
    42 	 * in a sandboxed environment.
    43 	 * @return
    44 	 */
    45 	public static URL getAsURL(final String name)
    46 	{
    47 		if (name == null) {
    48 			return null;
    49 		}
    50 
    51 		return Resource.class.getClassLoader().getResource(name);
    52 	}
    53 
    54 	/**
    55 	 * Loads a resource and returns an InputStream to it.
    56 	 * @param name
    57 	 * @return
    58 	 */
    59 	public static InputStream getAsStream(String name)
    60 	{
    61 		try {
    62 			URL url = getAsURL(name);
    63 			if (url == null) {
    64 				File file = new File(name);
    65 				if(file.exists()) {
    66 					return new FileInputStream(file);
    67 				}
    68 				return null;
    69 			} else {
    70 				return url.openStream();
    71 			}
    72 		} catch (IOException e) {
    73 			e.printStackTrace();
    74 			return null;
    75 		}
    76 	}
    77 
    78 	/**
    79 	 * Loads a plain text resource.
    80 	 * @param withNewline If false all newlines are removed from the
    81 	 * return String
    82 	 */
    83 	public static String getAsString(String name, boolean withNewline)
    84 	{
    85 		if (name == null) {
    86 			return null;
    87 		}
    88 
    89 		BufferedReader in = null;
    90 		try {
    91 			InputStream ins = getAsStream(name);
    92 			if (ins == null) {
    93 				return null;
    94 			}
    95 
    96 			in = new BufferedReader(
    97 				new InputStreamReader(ins, Charset.forName("UTF-8")));
    98 			StringBuffer buf = new StringBuffer();
    99 
   100 			for (;;) {
   101 				String line = in.readLine();
   102 				if (line == null) {
   103 					break;
   104 				}
   105 
   106 				buf.append(line);
   107 				if (withNewline) {
   108 					buf.append('\n');
   109 				}
   110 			}
   111 
   112 			return buf.toString();
   113 		} catch (Exception e) {
   114 			e.printStackTrace();
   115 			return null;
   116 		} finally {
   117 			try {
   118 				if (in != null) {
   119 					in.close();
   120 				}
   121 			} catch (IOException ex) {
   122 				ex.printStackTrace();
   123 			}
   124 		}
   125 	}
   126 }