org/sonews/util/io/Resource.java
author cli
Sat, 01 May 2010 18:51:57 +0200
changeset 29 60c237bb677a
parent 1 6fceb66e1ad7
permissions -rw-r--r--
Refactor logging to fix NullPointerException.
     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     {
    47       return null;
    48     }
    49 
    50     return Resource.class.getClassLoader().getResource(name);
    51   }
    52   
    53   /**
    54    * Loads a resource and returns an InputStream to it.
    55    * @param name
    56    * @return
    57    */
    58   public static InputStream getAsStream(String name)
    59   {
    60     try
    61     {
    62       URL url = getAsURL(name);
    63       if(url == null)
    64       {
    65         return null;
    66       }
    67       else
    68       {
    69         return url.openStream();
    70       }
    71     }
    72     catch(IOException e)
    73     {
    74       e.printStackTrace();
    75       return null;
    76     }
    77   }
    78 
    79   /**
    80    * Loads a plain text resource.
    81    * @param withNewline If false all newlines are removed from the 
    82    * return String
    83    */
    84   public static String getAsString(String name, boolean withNewline)
    85   {
    86     if(name == null)
    87       return null;
    88 
    89     BufferedReader in = null;
    90     try
    91     {
    92       InputStream ins = getAsStream(name);
    93       if(ins == null)
    94         return null;
    95 
    96       in = new BufferedReader(
    97         new InputStreamReader(ins, Charset.forName("UTF-8")));
    98       StringBuffer buf = new StringBuffer();
    99 
   100       for(;;)
   101       {
   102         String line = in.readLine();
   103         if(line == null)
   104           break;
   105 
   106         buf.append(line);
   107         if(withNewline)
   108           buf.append('\n');
   109       }
   110 
   111       return buf.toString();
   112     }
   113     catch(Exception e)
   114     {
   115       e.printStackTrace();
   116       return null;
   117     }
   118     finally
   119     {
   120       try
   121       {
   122         if(in != null)
   123           in.close();
   124       }
   125       catch(IOException ex)
   126       {
   127         ex.printStackTrace();
   128       }
   129     }
   130   }
   131 
   132 }