src/org/sonews/util/io/Resource.java
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 44 5d7d1adf387f
     1.1 --- a/src/org/sonews/util/io/Resource.java	Sun Aug 29 17:28:58 2010 +0200
     1.2 +++ b/src/org/sonews/util/io/Resource.java	Sun Aug 29 18:17:37 2010 +0200
     1.3 @@ -32,101 +32,89 @@
     1.4   */
     1.5  public final class Resource
     1.6  {
     1.7 -  
     1.8 -  /**
     1.9 -   * Loads a resource and returns it as URL reference.
    1.10 -   * The Resource's classloader is used to load the resource, not
    1.11 -   * the System's ClassLoader so it may be safe to use this method
    1.12 -   * in a sandboxed environment.
    1.13 -   * @return
    1.14 -   */
    1.15 -  public static URL getAsURL(final String name)
    1.16 -  {
    1.17 -    if(name == null)
    1.18 -    {
    1.19 -      return null;
    1.20 -    }
    1.21  
    1.22 -    return Resource.class.getClassLoader().getResource(name);
    1.23 -  }
    1.24 -  
    1.25 -  /**
    1.26 -   * Loads a resource and returns an InputStream to it.
    1.27 -   * @param name
    1.28 -   * @return
    1.29 -   */
    1.30 -  public static InputStream getAsStream(String name)
    1.31 -  {
    1.32 -    try
    1.33 -    {
    1.34 -      URL url = getAsURL(name);
    1.35 -      if(url == null)
    1.36 -      {
    1.37 -        return null;
    1.38 -      }
    1.39 -      else
    1.40 -      {
    1.41 -        return url.openStream();
    1.42 -      }
    1.43 -    }
    1.44 -    catch(IOException e)
    1.45 -    {
    1.46 -      e.printStackTrace();
    1.47 -      return null;
    1.48 -    }
    1.49 -  }
    1.50 +	/**
    1.51 +	 * Loads a resource and returns it as URL reference.
    1.52 +	 * The Resource's classloader is used to load the resource, not
    1.53 +	 * the System's ClassLoader so it may be safe to use this method
    1.54 +	 * in a sandboxed environment.
    1.55 +	 * @return
    1.56 +	 */
    1.57 +	public static URL getAsURL(final String name)
    1.58 +	{
    1.59 +		if (name == null) {
    1.60 +			return null;
    1.61 +		}
    1.62  
    1.63 -  /**
    1.64 -   * Loads a plain text resource.
    1.65 -   * @param withNewline If false all newlines are removed from the 
    1.66 -   * return String
    1.67 -   */
    1.68 -  public static String getAsString(String name, boolean withNewline)
    1.69 -  {
    1.70 -    if(name == null)
    1.71 -      return null;
    1.72 +		return Resource.class.getClassLoader().getResource(name);
    1.73 +	}
    1.74  
    1.75 -    BufferedReader in = null;
    1.76 -    try
    1.77 -    {
    1.78 -      InputStream ins = getAsStream(name);
    1.79 -      if(ins == null)
    1.80 -        return null;
    1.81 +	/**
    1.82 +	 * Loads a resource and returns an InputStream to it.
    1.83 +	 * @param name
    1.84 +	 * @return
    1.85 +	 */
    1.86 +	public static InputStream getAsStream(String name)
    1.87 +	{
    1.88 +		try {
    1.89 +			URL url = getAsURL(name);
    1.90 +			if (url == null) {
    1.91 +				return null;
    1.92 +			} else {
    1.93 +				return url.openStream();
    1.94 +			}
    1.95 +		} catch (IOException e) {
    1.96 +			e.printStackTrace();
    1.97 +			return null;
    1.98 +		}
    1.99 +	}
   1.100  
   1.101 -      in = new BufferedReader(
   1.102 -        new InputStreamReader(ins, Charset.forName("UTF-8")));
   1.103 -      StringBuffer buf = new StringBuffer();
   1.104 +	/**
   1.105 +	 * Loads a plain text resource.
   1.106 +	 * @param withNewline If false all newlines are removed from the
   1.107 +	 * return String
   1.108 +	 */
   1.109 +	public static String getAsString(String name, boolean withNewline)
   1.110 +	{
   1.111 +		if (name == null) {
   1.112 +			return null;
   1.113 +		}
   1.114  
   1.115 -      for(;;)
   1.116 -      {
   1.117 -        String line = in.readLine();
   1.118 -        if(line == null)
   1.119 -          break;
   1.120 +		BufferedReader in = null;
   1.121 +		try {
   1.122 +			InputStream ins = getAsStream(name);
   1.123 +			if (ins == null) {
   1.124 +				return null;
   1.125 +			}
   1.126  
   1.127 -        buf.append(line);
   1.128 -        if(withNewline)
   1.129 -          buf.append('\n');
   1.130 -      }
   1.131 +			in = new BufferedReader(
   1.132 +				new InputStreamReader(ins, Charset.forName("UTF-8")));
   1.133 +			StringBuffer buf = new StringBuffer();
   1.134  
   1.135 -      return buf.toString();
   1.136 -    }
   1.137 -    catch(Exception e)
   1.138 -    {
   1.139 -      e.printStackTrace();
   1.140 -      return null;
   1.141 -    }
   1.142 -    finally
   1.143 -    {
   1.144 -      try
   1.145 -      {
   1.146 -        if(in != null)
   1.147 -          in.close();
   1.148 -      }
   1.149 -      catch(IOException ex)
   1.150 -      {
   1.151 -        ex.printStackTrace();
   1.152 -      }
   1.153 -    }
   1.154 -  }
   1.155 +			for (;;) {
   1.156 +				String line = in.readLine();
   1.157 +				if (line == null) {
   1.158 +					break;
   1.159 +				}
   1.160  
   1.161 +				buf.append(line);
   1.162 +				if (withNewline) {
   1.163 +					buf.append('\n');
   1.164 +				}
   1.165 +			}
   1.166 +
   1.167 +			return buf.toString();
   1.168 +		} catch (Exception e) {
   1.169 +			e.printStackTrace();
   1.170 +			return null;
   1.171 +		} finally {
   1.172 +			try {
   1.173 +				if (in != null) {
   1.174 +					in.close();
   1.175 +				}
   1.176 +			} catch (IOException ex) {
   1.177 +				ex.printStackTrace();
   1.178 +			}
   1.179 +		}
   1.180 +	}
   1.181  }