info.rolandkrueger.roklib.io
Class StreamUtilities

java.lang.Object
  extended by info.rolandkrueger.roklib.io.StreamUtilities

public final class StreamUtilities
extends Object

Author:
Roland Krueger

Constructor Summary
private StreamUtilities()
           
 
Method Summary
static void appendFile(File targetFile, String text)
          Appends the given String to the end of a file.
static void copyFiles(File source, File destination)
          Copies the contents of one file to another.
static void copyReaderToWriter(Reader source, Writer destination)
           Copies the data from the given Reader to the given Writer.
static void copyReaderToWriter(Reader source, Writer destination, boolean closeSource, boolean closeDestination)
           Copies the data from the given Reader to the given Writer.
static void copyStreams(InputStream source, OutputStream destination)
           Copies the data from the given InputStream to the given OutputStream.
static void copyStreams(InputStream source, OutputStream destination, boolean closeSource, boolean closeDestination)
           Copies the data from the given InputStream to the given OutputStream.
static String getFileAsString(File file)
          Reads the contents of a file into a String.
static String getReaderAsString(Reader source)
          Transforms the contents of the given Reader into a String object.
static String getStreamAsString(InputStream source)
          Transforms the contents of the given InputStream into a String object.
static String getStreamAsString(InputStream source, Charset charset)
           
static String getStreamAsString(InputStream source, String charsetName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StreamUtilities

private StreamUtilities()
Method Detail

copyReaderToWriter

public static void copyReaderToWriter(Reader source,
                                      Writer destination)
                               throws IOException

Copies the data from the given Reader to the given Writer. Note that the Reader and Writer classes solely process character data and not byte data. Copying byte data must be done with copyStreams(InputStream, OutputStream).

Both source reader and destination writer will be closed after the copy process.

Parameters:
source - a character data source
destination - the target of the copy operation
Throws:
IOException - if an error occurs while copying

copyReaderToWriter

public static void copyReaderToWriter(Reader source,
                                      Writer destination,
                                      boolean closeSource,
                                      boolean closeDestination)
                               throws IOException

Copies the data from the given Reader to the given Writer. Note that the Reader and Writer classes solely process character data and not byte data. Copying byte data must be done with copyStreams(InputStream, OutputStream). The two boolean parameters let you define whether you want the source reader and destination writer to be closed after the operation.

Parameters:
source - a character data source
destination - the target of the copy operation
closeSource - source reader will be closed if true
closeDestination - destination writer will be closed if true
Throws:
IOException - if an error occurs while copying

copyStreams

public static void copyStreams(InputStream source,
                               OutputStream destination)
                        throws IOException

Copies the data from the given InputStream to the given OutputStream.

Both source and destination stream will be closed after the copy process.

Parameters:
source - a byte data source
destination - the target of the copy operation
Throws:
IOException - if an error occurs while copying

copyStreams

public static void copyStreams(InputStream source,
                               OutputStream destination,
                               boolean closeSource,
                               boolean closeDestination)
                        throws IOException

Copies the data from the given InputStream to the given OutputStream. The two boolean parameters let you define whether you want the source and destination streams to be closed after the operation.

Parameters:
source - a byte data source
destination - the target of the copy operation
closeSource - source stream will be closed if true
closeDestination - destination stream will be closed if true
Throws:
IOException - if an error occurs while copying

copyFiles

public static void copyFiles(File source,
                             File destination)
                      throws IOException
Copies the contents of one file to another.

Parameters:
source - the source file
destination - the destination file
Throws:
IOException - if an error occurs while copying

getStreamAsString

public static String getStreamAsString(InputStream source)
                                throws IOException
Transforms the contents of the given InputStream into a String object.

Parameters:
source - an InputStream the contents of which shall be transformed into a String
Returns:
a String containing the contents of the given InputStream
Throws:
IOException - if an error occurs during the transformation

getStreamAsString

public static String getStreamAsString(InputStream source,
                                       Charset charset)
                                throws IOException
Throws:
IOException

getStreamAsString

public static String getStreamAsString(InputStream source,
                                       String charsetName)
                                throws IOException
Throws:
IOException

getReaderAsString

public static String getReaderAsString(Reader source)
                                throws IOException
Transforms the contents of the given Reader into a String object.

Parameters:
source - an Reader the contents of which shall be transformed into a String
Returns:
a String containing the contents of the given Reader
Throws:
IOException - if an error occurs during the transformation

getFileAsString

public static String getFileAsString(File file)
                              throws IOException
Reads the contents of a file into a String.

Parameters:
file - some file
Returns:
the file's contents as a String
Throws:
IOException - if an error occurs while reading the file

appendFile

public static void appendFile(File targetFile,
                              String text)
                       throws IOException
Appends the given String to the end of a file.

Parameters:
targetFile - the file to append the data String to
text - some String
Throws:
IOException - if an error occurred while appending the data


Copyright © 2007-2011. All Rights Reserved.