org.yajul.io
Class StreamCopier

java.lang.Object
  extended by org.yajul.io.StreamCopier
All Implemented Interfaces:
java.lang.Runnable

public class StreamCopier
extends java.lang.Object
implements java.lang.Runnable

Provides stream copying capability in a Runnable class. This can be used to redirect streams from a spawned JVM, or to 'pump' a one side of PipedInputStream / PipedOutputStream pair.
Also provides a static method that copies an entire input stream into an output stream.

Author:
Joshua Davis

Field Summary
static int DEFAULT_BUFFER_SIZE
          The default buffer size.
 
Constructor Summary
StreamCopier(java.io.InputStream in, java.io.OutputStream out)
          Creates a new stream copier, that will copy the input stream into the output stream when the run() method is caled.
 
Method Summary
static int copy(java.io.InputStream in, java.io.OutputStream out)
          Copies the input stream into the output stream in a thread safe and efficient manner.
static int copy(java.io.InputStream in, java.io.OutputStream out, int bufsz)
          Copies the input stream into the output stream in a thread safe and efficient manner.
 java.io.IOException getException()
          Returns the exception thrown in the run() method, if any.
 boolean isComplete()
          Returns true if the copying is complete.
static java.util.ArrayList readBlocks(java.io.InputStream in, int blocksz)
          Reads the entire input stream into an array list of byte arrays, each byte array being a maximum of 'blocksz' bytes long.
static byte[] readByteArray(java.io.File file)
          Reads the specified file into a byte array.
static byte[] readByteArray(java.io.InputStream in)
          Reads the entire input stream into a byte array.
static byte[] readByteArray(java.io.InputStream in, int limit)
          Reads the entire input stream into a byte array with a limit.
static byte[] readByteArray(java.io.Reader in)
          Reads the entire input stream into a byte array.
static byte[] readByteArray(java.io.Reader in, int limit)
          Reads the entire input stream into a byte array with a limit.
static byte[] readFileIntoByteArray(java.lang.String fileName)
          Reads the specified file into a byte array.
 void run()
          This method will copy the input into the output until there is no more input.
static byte[] serializeObject(java.lang.Object o)
          Serializes the object into an array of bytes.
static java.lang.Object unserializeObject(byte[] bytes)
          Reads a serialized object from the array of bytes.
static int unsyncCopy(java.io.InputStream in, java.io.OutputStream out, int bufsz)
          Copies the input stream into the output stream in an efficient manner.
static int unsyncCopy(java.io.InputStream in, java.io.OutputStream out, int bufsz, int limit)
          Copies the input stream into the output stream in an efficient manner.
static int unsyncCopy(java.io.Reader in, java.io.Writer out, int bufsz, int limit)
          Copies the input stream (reader) into the output stream (writer) in an efficient manner.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default buffer size.

See Also:
Constant Field Values
Constructor Detail

StreamCopier

public StreamCopier(java.io.InputStream in,
                    java.io.OutputStream out)
Creates a new stream copier, that will copy the input stream into the output stream when the run() method is caled.

Parameters:
in - The input stream to read from.
out - The output stream. If this is null, the input will be discarded, similar to piping to /dev/null on UN*X.
Method Detail

copy

public static int copy(java.io.InputStream in,
                       java.io.OutputStream out,
                       int bufsz)
                throws java.io.IOException
Copies the input stream into the output stream in a thread safe and efficient manner.

Parameters:
in - The input stream.
out - The output stream. If this is null, the input will be discarded, similar to piping to /dev/null on UN*X.
bufsz - The size of the buffer to use.
Returns:
int The number of bytes copied.
Throws:
java.io.IOException - When the stream could not be copied.

unsyncCopy

public static int unsyncCopy(java.io.InputStream in,
                             java.io.OutputStream out,
                             int bufsz)
                      throws java.io.IOException
Copies the input stream into the output stream in an efficient manner. This version does not synchronize on the streams, so it is not safe to use when the streams are being accessed by multiple threads.

Parameters:
in - The input stream.
out - The output stream. If this is null, the input will be discarded, similar to piping to /dev/null on UN*X.
bufsz - The size of the buffer to use.
Returns:
int The number of bytes copied.
Throws:
java.io.IOException - When the stream could not be copied.

unsyncCopy

public static int unsyncCopy(java.io.InputStream in,
                             java.io.OutputStream out,
                             int bufsz,
                             int limit)
                      throws java.io.IOException
Copies the input stream into the output stream in an efficient manner. This version does not synchronize on the streams, so it is not safe to use when the streams are being accessed by multiple threads.

Parameters:
in - The input stream.
out - The output stream. If this is null, the input will be discarded, similar to piping to /dev/null on UN*X.
bufsz - The size of the buffer to use.
limit - The number of bytes to copy, or UNLIMITED (-1) to copy until the end of the input stream.
Returns:
int The number of bytes copied.
Throws:
java.io.IOException - When the stream could not be copied.

unsyncCopy

public static int unsyncCopy(java.io.Reader in,
                             java.io.Writer out,
                             int bufsz,
                             int limit)
                      throws java.io.IOException
Copies the input stream (reader) into the output stream (writer) in an efficient manner. This version does not synchronize on the streams, so it is not safe to use when the streams are being accessed by multiple threads.

Parameters:
in - The input reader
out - The output writer. If this is null, the input will be discarded, similar to piping to /dev/null on UN*X.
bufsz - The size of the buffer to use.
limit - The number of bytes to copy, or UNLIMITED (-1) to copy until the end of the input stream.
Returns:
int The number of bytes copied.
Throws:
java.io.IOException - When the stream could not be copied.

copy

public static int copy(java.io.InputStream in,
                       java.io.OutputStream out)
                throws java.io.IOException
Copies the input stream into the output stream in a thread safe and efficient manner.

Parameters:
in - The input stream.
out - The output stream. If this is null, the input will be discarded, similar to piping to /dev/null on UN*X.
Returns:
int The number of bytes copied.
Throws:
java.io.IOException - When the stream could not be copied.

readBlocks

public static java.util.ArrayList readBlocks(java.io.InputStream in,
                                             int blocksz)
                                      throws java.io.IOException
Reads the entire input stream into an array list of byte arrays, each byte array being a maximum of 'blocksz' bytes long.

Parameters:
blocksz - The block size. Byte arrays in the list will not be longer than this.
in - The input stream.
Returns:
ArrayList - An array list of byte arrays.
Throws:
java.io.IOException - When something happens while reading the stream.

readByteArray

public static byte[] readByteArray(java.io.InputStream in)
                            throws java.io.IOException
Reads the entire input stream into a byte array.

Parameters:
in - The input stream.
Returns:
the byte array
Throws:
java.io.IOException - When something happens while reading the stream.

readByteArray

public static byte[] readByteArray(java.io.InputStream in,
                                   int limit)
                            throws java.io.IOException
Reads the entire input stream into a byte array with a limit.

Parameters:
in - The input reader
limit - The number of bytes to read.
Returns:
An array of bytes read from the input.
Throws:
java.io.IOException - Thrown if there was an error while copying.

readByteArray

public static byte[] readByteArray(java.io.Reader in,
                                   int limit)
                            throws java.io.IOException
Reads the entire input stream into a byte array with a limit.

Parameters:
in - The input reader
limit - The number of bytes to read.
Returns:
An array of bytes read from the input.
Throws:
java.io.IOException - Thrown if there was an error while copying.

readByteArray

public static byte[] readByteArray(java.io.Reader in)
                            throws java.io.IOException
Reads the entire input stream into a byte array.

Parameters:
in - The input reader
Returns:
An array of bytes read from the input.
Throws:
java.io.IOException - Thrown if there was an error while copying.

readByteArray

public static byte[] readByteArray(java.io.File file)
                            throws java.io.IOException
Reads the specified file into a byte array.

Parameters:
file - The file to read.
Returns:
An array of bytes read from the input.
Throws:
java.io.IOException - When something happens while reading the stream.

readFileIntoByteArray

public static byte[] readFileIntoByteArray(java.lang.String fileName)
                                    throws java.io.IOException
Reads the specified file into a byte array.

Parameters:
fileName - The file name to read.
Returns:
An array of bytes read from the input.
Throws:
java.io.IOException - When something happens while reading the stream.

serializeObject

public static byte[] serializeObject(java.lang.Object o)
                              throws java.io.IOException
Serializes the object into an array of bytes.

Parameters:
o - The object to serialize.
Returns:
An array of bytes that contiains the serialized object.
Throws:
java.io.IOException - if something goes wrong

unserializeObject

public static java.lang.Object unserializeObject(byte[] bytes)
                                          throws java.io.IOException,
                                                 java.lang.ClassNotFoundException
Reads a serialized object from the array of bytes.

Parameters:
bytes - The array of bytes.
Returns:
The unserialized object.
Throws:
java.io.IOException - if there was a problem reading the input.
java.lang.ClassNotFoundException - if the class of the object in the input was not found.

run

public void run()
This method will copy the input into the output until there is no more input. Since this method is typically run by a thread, exceptions are not thrown from it. Instead, the exception can be read using the getException() method.

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

The general contract of the method run is that it may take any action whatsoever.

Specified by:
run in interface java.lang.Runnable
See Also:
Thread.run()

getException

public java.io.IOException getException()
Returns the exception thrown in the run() method, if any.

Returns:
IOException - The exception thrown during the run() method, or null if there were no errors.

isComplete

public boolean isComplete()
Returns true if the copying is complete.

Returns:
boolean - true if the copying is complete. Returns false if the copying is in progress, not started, or encountered an error.


Copyright © 2008. All Rights Reserved.