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