de.cau.cs.kieler.kwebs.util
Class Resources

java.lang.Object
  extended by de.cau.cs.kieler.kwebs.util.Resources

public final class Resources
extends Object

Utilities for accessing resources such as files or bundled resources.

Rating proposed yellow
(2011-08-02) reviewed by ckru, mri, msp

Method Summary
static InputStream getFileOrPluginResourceStream(String pluginid, String resource)
          Returns an input stream on a file specified by resource.
static InputStream getResourceStream(String pluginid, String resource)
          Creates an input stream to a bundled resource.
static byte[] readFileAsByteArray(String path)
          Reads a file.
static String readFileAsString(String path)
          Reads a file.
static byte[] readFileOrPluginResourceAsByteArray(String pluginid, String resource)
          Reads data from a path specified by resource.
static byte[] readStreamAsByteArray(InputStream stream)
          Reads from a stream with fixed size.
static String readStreamAsString(InputStream stream)
          Reads from a stream with fixed size.
static void writeFile(String path, byte[] data)
          Writes a file from a string.
static void writeFile(String path, InputStream stream)
          Writes a file from an input stream.
static void writeFile(String path, String data)
          Writes a file from a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readFileAsString

public static String readFileAsString(String path)
                               throws IOException
Reads a file.

Parameters:
path - path to the file
Returns:
String with file contents
Throws:
IOException

readFileAsByteArray

public static byte[] readFileAsByteArray(String path)
                                  throws IOException
Reads a file.

Parameters:
path - path to the file
Returns:
byte array with file contents
Throws:
IOException

writeFile

public static void writeFile(String path,
                             String data)
                      throws IOException
Writes a file from a string.

Parameters:
path - path to the file
data - string with file contents
Throws:
IOException

writeFile

public static void writeFile(String path,
                             byte[] data)
                      throws IOException
Writes a file from a string.

Parameters:
path - path to the file
data - byte array file contents
Throws:
IOException

writeFile

public static void writeFile(String path,
                             InputStream stream)
                      throws IOException
Writes a file from an input stream.

Parameters:
path - path to the file
stream - stream with file contents
Throws:
IOException

readStreamAsString

public static String readStreamAsString(InputStream stream)
                                 throws IOException
Reads from a stream with fixed size.

Parameters:
stream - the stream to be read
Returns:
string with stream contents
Throws:
IOException

readStreamAsByteArray

public static byte[] readStreamAsByteArray(InputStream stream)
                                    throws IOException
Reads from a stream with fixed size.

Parameters:
stream - the stream to be read
Returns:
string with stream contents
Throws:
IOException

readFileOrPluginResourceAsByteArray

public static byte[] readFileOrPluginResourceAsByteArray(String pluginid,
                                                         String resource)
Reads data from a path specified by resource. If not successful the data is read from the plug-in specified by pluginid.

Parameters:
pluginid - the plug-in id for reading from a plug-in
resource - the resource path specifying either the file or the resource identifier
Returns:
a byte array containing the read data or null if neither a matching file or a matching resource could be found

getFileOrPluginResourceStream

public static InputStream getFileOrPluginResourceStream(String pluginid,
                                                        String resource)
Returns an input stream on a file specified by resource. If the file does not exist an input stream to the bundled resource from the plug-in specified by pluginid is created.

Parameters:
pluginid - the plug-in id for reading from a plug-in
resource - the resource path specifying either the file or the resource identifier
Returns:
an input stream on the resource or null if neither a matching file or a matching resource could be found

getResourceStream

public static InputStream getResourceStream(String pluginid,
                                            String resource)
                                     throws IOException
Creates an input stream to a bundled resource.

Parameters:
pluginid - id of the plug-in the resource is bundled with
resource - path to the resource within this plug-in
Returns:
an input stream to the resource
Throws:
IOException - if the specified resource could not be found