author | cli |
Tue, 07 Jun 2011 09:23:34 +0200 | |
changeset 43 | 7d0e65712a95 |
parent 37 | 74139325d305 |
child 44 | 5d7d1adf387f |
permissions | -rwxr-xr-x |
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 |
|
cli@37 | 36 |
/** |
cli@37 | 37 |
* Loads a resource and returns it as URL reference. |
cli@37 | 38 |
* The Resource's classloader is used to load the resource, not |
cli@37 | 39 |
* the System's ClassLoader so it may be safe to use this method |
cli@37 | 40 |
* in a sandboxed environment. |
cli@37 | 41 |
* @return |
cli@37 | 42 |
*/ |
cli@37 | 43 |
public static URL getAsURL(final String name) |
cli@37 | 44 |
{ |
cli@37 | 45 |
if (name == null) { |
cli@37 | 46 |
return null; |
cli@37 | 47 |
} |
chris@1 | 48 |
|
cli@37 | 49 |
return Resource.class.getClassLoader().getResource(name); |
cli@37 | 50 |
} |
chris@1 | 51 |
|
cli@37 | 52 |
/** |
cli@37 | 53 |
* Loads a resource and returns an InputStream to it. |
cli@37 | 54 |
* @param name |
cli@37 | 55 |
* @return |
cli@37 | 56 |
*/ |
cli@37 | 57 |
public static InputStream getAsStream(String name) |
cli@37 | 58 |
{ |
cli@37 | 59 |
try { |
cli@37 | 60 |
URL url = getAsURL(name); |
cli@37 | 61 |
if (url == null) { |
cli@37 | 62 |
return null; |
cli@37 | 63 |
} else { |
cli@37 | 64 |
return url.openStream(); |
cli@37 | 65 |
} |
cli@37 | 66 |
} catch (IOException e) { |
cli@37 | 67 |
e.printStackTrace(); |
cli@37 | 68 |
return null; |
cli@37 | 69 |
} |
cli@37 | 70 |
} |
chris@1 | 71 |
|
cli@37 | 72 |
/** |
cli@37 | 73 |
* Loads a plain text resource. |
cli@37 | 74 |
* @param withNewline If false all newlines are removed from the |
cli@37 | 75 |
* return String |
cli@37 | 76 |
*/ |
cli@37 | 77 |
public static String getAsString(String name, boolean withNewline) |
cli@37 | 78 |
{ |
cli@37 | 79 |
if (name == null) { |
cli@37 | 80 |
return null; |
cli@37 | 81 |
} |
chris@1 | 82 |
|
cli@37 | 83 |
BufferedReader in = null; |
cli@37 | 84 |
try { |
cli@37 | 85 |
InputStream ins = getAsStream(name); |
cli@37 | 86 |
if (ins == null) { |
cli@37 | 87 |
return null; |
cli@37 | 88 |
} |
chris@1 | 89 |
|
cli@37 | 90 |
in = new BufferedReader( |
cli@37 | 91 |
new InputStreamReader(ins, Charset.forName("UTF-8"))); |
cli@37 | 92 |
StringBuffer buf = new StringBuffer(); |
chris@1 | 93 |
|
cli@37 | 94 |
for (;;) { |
cli@37 | 95 |
String line = in.readLine(); |
cli@37 | 96 |
if (line == null) { |
cli@37 | 97 |
break; |
cli@37 | 98 |
} |
chris@1 | 99 |
|
cli@37 | 100 |
buf.append(line); |
cli@37 | 101 |
if (withNewline) { |
cli@37 | 102 |
buf.append('\n'); |
cli@37 | 103 |
} |
cli@37 | 104 |
} |
cli@37 | 105 |
|
cli@37 | 106 |
return buf.toString(); |
cli@37 | 107 |
} catch (Exception e) { |
cli@37 | 108 |
e.printStackTrace(); |
cli@37 | 109 |
return null; |
cli@37 | 110 |
} finally { |
cli@37 | 111 |
try { |
cli@37 | 112 |
if (in != null) { |
cli@37 | 113 |
in.close(); |
cli@37 | 114 |
} |
cli@37 | 115 |
} catch (IOException ex) { |
cli@37 | 116 |
ex.printStackTrace(); |
cli@37 | 117 |
} |
cli@37 | 118 |
} |
cli@37 | 119 |
} |
chris@1 | 120 |
} |