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