JavaGantt 2011.1 API

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 close(java.io.Reader input)
          Closes given input reader.
static void close(java.io.Writer output)
          Closes given output writer.
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.
static java.io.Reader getReader(java.lang.String resource)
          Returns a reader for given resource and system default encoding.
static java.io.Reader getReader(java.lang.String resource, java.lang.String encoding)
          Returns a reader for given resource and encoding.
static java.io.Writer getWriter(java.lang.String fileName)
          Returns a writer for given fileName and system default encoding.
static java.io.Writer getWriter(java.lang.String fileName, java.lang.String encoding)
          Returns a writer for given fileName and encoding.
 
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 resource and if not successfull for edited name, too. If cannot find a resource, throws RuntimeException.

Parameters:
resource - - a resource name
Returns:
newly created input stream
Throws:
java.lang.NullPointerException - if resource is null
java.lang.RuntimeException - if cannot create an input stream

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:
newly created input stream
Throws:
java.lang.NullPointerException - if resource is null
java.lang.RuntimeException - if cannot create an input stream

getReader

public static java.io.Reader getReader(java.lang.String resource)
Returns a reader for given resource and system default encoding. Invokes getReader(String, String) to obtain Reader instance.

Parameters:
resource - - a resource name
Returns:
newly created reader or null if resource does not exist
Throws:
java.lang.NullPointerException - if resource is null
java.lang.RuntimeException - if cannot create a reader instance from given resource

getReader

public static java.io.Reader getReader(java.lang.String resource,
                                       java.lang.String encoding)
Returns a reader for given resource and encoding. Invokes getInputStream(String) to obtain input stream (see description in that method for information about input stream construction). Then creates a reader for given encoding. If encoding is null, uses default system encoding.

Parameters:
resource - - a resource name
encoding - - character stream encoding (such as UTF-8), may be null
Returns:
newly created reader or null if resource does not exist
Throws:
java.lang.NullPointerException - if resource is null
java.lang.RuntimeException - if cannot create a reader instance from a given resource or if given encoding is not supported

getOutputStream

public static java.io.OutputStream getOutputStream(java.lang.String fileName)
Returns an output stream for a file with given fileName. Creates also all directories that do not exist in the file path.

Parameters:
fileName - - a file name
Returns:
created output stream
Throws:
java.lang.NullPointerException - if fileName is null
java.lang.RuntimeException - if cannot create an output stream

getOutputStream

public static java.io.OutputStream getOutputStream(java.io.File file)
Returns an output stream for a given file. Creates also all directories that do not exist in the file path.

Parameters:
file - - a File instance to streaming
Returns:
created output stream
Throws:
java.lang.NullPointerException - if file is null
java.lang.RuntimeException - if cannot create an output stream

getWriter

public static java.io.Writer getWriter(java.lang.String fileName)
Returns a writer for given fileName and system default encoding. Invokes getWriter(String, String) to obtain Writer instance.

Parameters:
fileName - - a target file name
Returns:
newly created writer
Throws:
java.lang.NullPointerException - if fileName is null
java.lang.RuntimeException - if cannot create a writer

getWriter

public static java.io.Writer getWriter(java.lang.String fileName,
                                       java.lang.String encoding)
Returns a writer for given fileName and encoding. Invokes getOutputStream(String) to obtain output stream (see description in that method for information about output stream construction). Then creates a writer for given encoding. If encoding is null, uses default system encoding.

Parameters:
fileName - - a target file name
encoding - - character stream encoding (such as UTF-8)
Returns:
newly created writer
Throws:
java.lang.NullPointerException - if fileName is null
java.lang.RuntimeException - if cannot create a writer or if given encoding is not supported

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 occurs.

Parameters:
input - - an input stream to close

close

public static void close(java.io.Reader input)
Closes given input reader. No exception is thrown if some problem occurs.

Parameters:
input - - an input stream to close

close

public static void close(java.io.OutputStream output)
Closes given output stream. A stream is flushed before close.

Parameters:
output - - an output stream to close, may be null
Throws:
java.lang.RuntimeException - if an error occurs

close

public static void close(java.io.Writer output)
Closes given output writer. A writer is flushed before close.

Parameters:
output - - an output stream to close, may be null
Throws:
java.lang.RuntimeException - if an error occurs

JavaGantt 2011.1 API