src/org/sonews/util/io/Resource.java
author cli
Sun, 29 Aug 2010 17:43:58 +0200
changeset 36 c404a87db5b7
parent 35 ed84c8bdd87b
child 37 74139325d305
permissions -rw-r--r--
Add some checks to prevent #13 happen.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.util.io;
chris@1
    20
chris@1
    21
import java.io.BufferedReader;
chris@1
    22
import java.io.IOException;
chris@1
    23
import java.io.InputStream;
chris@1
    24
import java.io.InputStreamReader;
chris@1
    25
import java.net.URL;
chris@1
    26
import java.nio.charset.Charset;
chris@1
    27
chris@1
    28
/**
chris@1
    29
 * Provides method for loading of resources.
chris@1
    30
 * @author Christian Lins
chris@1
    31
 * @since sonews/0.5.0
chris@1
    32
 */
chris@1
    33
public final class Resource
chris@1
    34
{
chris@1
    35
  
chris@1
    36
  /**
chris@1
    37
   * Loads a resource and returns it as URL reference.
chris@1
    38
   * The Resource's classloader is used to load the resource, not
chris@1
    39
   * the System's ClassLoader so it may be safe to use this method
chris@1
    40
   * in a sandboxed environment.
chris@1
    41
   * @return
chris@1
    42
   */
chris@1
    43
  public static URL getAsURL(final String name)
chris@1
    44
  {
chris@1
    45
    if(name == null)
chris@1
    46
    {
chris@1
    47
      return null;
chris@1
    48
    }
chris@1
    49
chris@1
    50
    return Resource.class.getClassLoader().getResource(name);
chris@1
    51
  }
chris@1
    52
  
chris@1
    53
  /**
chris@1
    54
   * Loads a resource and returns an InputStream to it.
chris@1
    55
   * @param name
chris@1
    56
   * @return
chris@1
    57
   */
chris@1
    58
  public static InputStream getAsStream(String name)
chris@1
    59
  {
chris@1
    60
    try
chris@1
    61
    {
chris@1
    62
      URL url = getAsURL(name);
chris@1
    63
      if(url == null)
chris@1
    64
      {
chris@1
    65
        return null;
chris@1
    66
      }
chris@1
    67
      else
chris@1
    68
      {
chris@1
    69
        return url.openStream();
chris@1
    70
      }
chris@1
    71
    }
chris@1
    72
    catch(IOException e)
chris@1
    73
    {
chris@1
    74
      e.printStackTrace();
chris@1
    75
      return null;
chris@1
    76
    }
chris@1
    77
  }
chris@1
    78
chris@1
    79
  /**
chris@1
    80
   * Loads a plain text resource.
chris@1
    81
   * @param withNewline If false all newlines are removed from the 
chris@1
    82
   * return String
chris@1
    83
   */
chris@1
    84
  public static String getAsString(String name, boolean withNewline)
chris@1
    85
  {
chris@1
    86
    if(name == null)
chris@1
    87
      return null;
chris@1
    88
chris@1
    89
    BufferedReader in = null;
chris@1
    90
    try
chris@1
    91
    {
chris@1
    92
      InputStream ins = getAsStream(name);
chris@1
    93
      if(ins == null)
chris@1
    94
        return null;
chris@1
    95
chris@1
    96
      in = new BufferedReader(
chris@1
    97
        new InputStreamReader(ins, Charset.forName("UTF-8")));
chris@1
    98
      StringBuffer buf = new StringBuffer();
chris@1
    99
chris@1
   100
      for(;;)
chris@1
   101
      {
chris@1
   102
        String line = in.readLine();
chris@1
   103
        if(line == null)
chris@1
   104
          break;
chris@1
   105
chris@1
   106
        buf.append(line);
chris@1
   107
        if(withNewline)
chris@1
   108
          buf.append('\n');
chris@1
   109
      }
chris@1
   110
chris@1
   111
      return buf.toString();
chris@1
   112
    }
chris@1
   113
    catch(Exception e)
chris@1
   114
    {
chris@1
   115
      e.printStackTrace();
chris@1
   116
      return null;
chris@1
   117
    }
chris@1
   118
    finally
chris@1
   119
    {
chris@1
   120
      try
chris@1
   121
      {
chris@1
   122
        if(in != null)
chris@1
   123
          in.close();
chris@1
   124
      }
chris@1
   125
      catch(IOException ex)
chris@1
   126
      {
chris@1
   127
        ex.printStackTrace();
chris@1
   128
      }
chris@1
   129
    }
chris@1
   130
  }
chris@1
   131
chris@1
   132
}