com.calitha.io
Class StreamUtil

java.lang.Object
  extended by com.calitha.io.StreamUtil

public final class StreamUtil
extends Object

This utility class provides several convenience methods for working with input streams, output streams and random access files.

Author:
Robert van Loenhout

Method Summary
static long blockReadToWrite(ReadWrapper in, WriteWrapper out)
          Copies all bytes from a ReadWrapper to a WriteWrapper object in blocks.
static void closeInputStream(InputStream stream)
          Try to close the input stream.
static long copyRange(RandomAccessFile src, long startSrc, RandomAccessFile target, long startTarget, int size)
          Copies a part of a source random access file to a target random access file.
static long fileRangeToStream(OutputStream out, RandomAccessFile raf, long start, int size)
          Copies in blocks a part of a random access file to an output stream.
static byte[] inStreamToByteArray(InputStream in)
          Copies in blocks the bytes from an input stream to a new byte array
static ByteArrayInputStream inStreamToByteInStream(InputStream in)
          Copies in blocks the bytes from an input stream to a new byte array input stream.
static ByteArrayOutputStream inStreamToByteOutStream(InputStream in)
          Copies in blocks the bytes from a input stream to a new byte array output stream.
static long inStreamToOutStream(InputStream in, OutputStream out)
          Copies in blocks the bytes from an input stream to and output stream.
static long inStreamToRAF(InputStream in, RandomAccessFile out)
          Copies in blocks the bytes from an input stream to a random access file.
static long rafToOutStream(RandomAccessFile in, OutputStream out)
          Copies in blocks the bytes from a random access file to a output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

blockReadToWrite

public static long blockReadToWrite(ReadWrapper in,
                                    WriteWrapper out)
                             throws IOException
Copies all bytes from a ReadWrapper to a WriteWrapper object in blocks.

Internally it means it can be used to copy from an InputStream or RandomAccessFile to an OutputStream or RandomAccessFile.

Parameters:
in - ReadWrapper object
out - WriteWrapper object
Returns:
total number of bytes that have been copied
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if an argument is null

inStreamToOutStream

public static long inStreamToOutStream(InputStream in,
                                       OutputStream out)
                                throws IOException
Copies in blocks the bytes from an input stream to and output stream.

Parameters:
in - input stream
out - output stream
Returns:
total number of bytes that have been copied
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if an argument is null

inStreamToRAF

public static long inStreamToRAF(InputStream in,
                                 RandomAccessFile out)
                          throws IOException
Copies in blocks the bytes from an input stream to a random access file.

Parameters:
in - input stream
out - output stream
Returns:
total number of bytes that have been copied
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if an argument is null

rafToOutStream

public static long rafToOutStream(RandomAccessFile in,
                                  OutputStream out)
                           throws IOException
Copies in blocks the bytes from a random access file to a output stream.

Parameters:
in - input stream
out - output stream
Returns:
total number of bytes that have been copied
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if an argument is null

inStreamToByteOutStream

public static ByteArrayOutputStream inStreamToByteOutStream(InputStream in)
                                                     throws IOException
Copies in blocks the bytes from a input stream to a new byte array output stream.

Parameters:
in - input stream
Returns:
new byte array output stream
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if an argument is null

inStreamToByteArray

public static byte[] inStreamToByteArray(InputStream in)
                                  throws IOException
Copies in blocks the bytes from an input stream to a new byte array

Parameters:
in - input stream
Returns:
new byte array
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if an argument is null

inStreamToByteInStream

public static ByteArrayInputStream inStreamToByteInStream(InputStream in)
                                                   throws IOException
Copies in blocks the bytes from an input stream to a new byte array input stream.

Parameters:
in - input stream
Returns:
new byte array input stream
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if an argument is null

fileRangeToStream

public static long fileRangeToStream(OutputStream out,
                                     RandomAccessFile raf,
                                     long start,
                                     int size)
                              throws IOException
Copies in blocks a part of a random access file to an output stream.

Parameters:
out - output stream
raf - random access file that is used as input
start - start position
size - size of the part being copied
Returns:
total number of bytes that have been copied
Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if the out or raf argument is null

copyRange

public static long copyRange(RandomAccessFile src,
                             long startSrc,
                             RandomAccessFile target,
                             long startTarget,
                             int size)
                      throws IOException
Copies a part of a source random access file to a target random access file.

The source and target can be the same object. However in that case the two parts declared by startSrc, startTarget and size must not overlap.

Parameters:
src - source random access file
startSrc - start position for reading in the source
target - target random access file
startTarget - start position for writing in the target
size - total size of the part being copied
Returns:
total number of bytes that have been copied
Throws:
IOException - if an I/O error occurs, possibly because the parts would overlap
IllegalArgumentException - if the source or target argument is null

closeInputStream

public static void closeInputStream(InputStream stream)
Try to close the input stream.

Ignore any error if an I/O exception is thrown or if the stream is null.

Parameters:
stream - the input stream