eu.beesoft.gaia.util
Class Streams

java.lang.Object
  extended by eu.beesoft.gaia.util.Streams

public final class Streams
extends java.lang.Object

Utility class for the stream I/O operations.


Method Summary
static void close(java.io.InputStream input)
          Closes given input stream.
static void close(java.io.OutputStream output)
          Closes given output stream.
static void copy(java.io.InputStream input, java.io.OutputStream output)
          Copies all data from input stream to output stream.
static byte[] copyToByteArray(java.io.InputStream input)
          Copies all data from input stream to the returned byte array.
static java.io.InputStream getInputStream(java.io.File resource)
          Returns an input stream from a given File resource.
static java.io.InputStream getInputStream(java.lang.String resource)
          Returns an input stream for given resource.
static java.io.OutputStream getOutputStream(java.io.File file)
          Returns an output stream for a given file.
static java.io.OutputStream getOutputStream(java.lang.String fileName)
          Returns an output stream for a file with given fileName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInputStream

public static java.io.InputStream getInputStream(java.lang.String resource)
Returns an input stream for given resource. First it tries to get input stream from a file on the file-system level. If such file does not exist, it edits name (adds or removes initial slash) and repeats previous step. If there is no file found, it tries to get input stream via Class.getResourceAsStream() method for original and if not successfull for edited name, too.

Parameters:
resource - - a resource name
Returns:
input stream or null if resource does not exist
Throws:
java.lang.NullPointerException - if resource is null
java.lang.RuntimeException - if cannot create an input stream from an existing file

getInputStream

public static java.io.InputStream getInputStream(java.io.File resource)
Returns an input stream from a given File resource.

Parameters:
resource - - a File instance
Returns:
input stream or null if resource does not exist
Throws:
java.lang.NullPointerException - if resource is null
java.lang.RuntimeException - if cannot create an input stream from existing file

getOutputStream

public static java.io.OutputStream getOutputStream(java.lang.String fileName)
Returns an output stream for a file with given fileName.

Parameters:
fileName - - a file name
Returns:
output stream or null if file is directory or some exception was thrown

getOutputStream

public static java.io.OutputStream getOutputStream(java.io.File file)
Returns an output stream for a given file.

Parameters:
file - - a File instance
Returns:
output stream or null if file is directory or some exception was thrown

copy

public static void copy(java.io.InputStream input,
                        java.io.OutputStream output)
Copies all data from input stream to output stream. Output stream is flushed. No stream is closed.

Parameters:
input - - an input stream
output - - an output stream
Throws:
java.lang.RuntimeException - if some exception was thrown

copyToByteArray

public static byte[] copyToByteArray(java.io.InputStream input)
Copies all data from input stream to the returned byte array. Input stream is not closed.

Parameters:
input - - an input stream
Returns:
byte array with data from input stream
Throws:
java.lang.RuntimeException - if some exception was thrown

close

public static void close(java.io.InputStream input)
Closes given input stream. No exception is thrown if some problem occurrs.

Parameters:
input - - an input stream to close

close

public static void close(java.io.OutputStream output)
Closes given output stream. No exception is thrown if some problem occurrs.

Parameters:
output - - an output stream to close