net.larsan.urd.util
Class IOUtils

java.lang.Object
  |
  +--net.larsan.urd.util.IOUtils

public class IOUtils
extends java.lang.Object

A collection of static methods for stream and channel utilities. All buffered operations uses a buffer of size DEFAULT_BUFFER_SIZE (2048 bytes) if not specified.

Version:
Alpha-0.1 / 2002-10-06 21:37
Author:
Lars J. Nilsson

Field Summary
static int DEFAULT_BUFFER_SIZE
          Default buffer size for buffered operation in this class, 2048 bytes.
 
Constructor Summary
IOUtils()
           
 
Method Summary
static byte[] toByteArray(java.nio.channels.FileChannel channel)
          Read a complete channel to a byte array.
static byte[] toByteArray(java.io.InputStream in)
          Read a complete stream to a byte array.
static byte[] toByteArray(java.io.InputStream in, int bufferSize)
          Read a complete input stream to a byte array with a non-default buffer size.
static void toFile(java.io.File file, java.io.InputStream stream)
          Write the content of an input stream to a file.
static void toFile(java.io.File file, java.lang.String string)
          Write the content of a string to a file.
static java.lang.String toString(java.io.File file)
          Read a complete file to a string.
static java.lang.String toString(java.io.InputStream in)
          Read an complete input stream to a string.
static long transfer(java.io.InputStream in, java.io.OutputStream out)
          Transfer bytes from one stream to another.
static long transfer(java.io.InputStream in, java.io.OutputStream out, int buffSize)
          Transfer bytes from one stream to another.
 
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
Default buffer size for buffered operation in this class, 2048 bytes.

See Also:
Constant Field Values
Constructor Detail

IOUtils

public IOUtils()
Method Detail

transfer

public static long transfer(java.io.InputStream in,
                            java.io.OutputStream out)
                     throws java.io.IOException
Transfer bytes from one stream to another. This method uses a buffer size of 2048 bytes and will not close the input stream when done.

Parameters:
in - Input stream to read from
out - Output stream to write to
Returns:
The total number of bytes tranfered
Throws:
java.io.IOException - If an IO error occurs

transfer

public static long transfer(java.io.InputStream in,
                            java.io.OutputStream out,
                            int buffSize)
                     throws java.io.IOException
Transfer bytes from one stream to another. This method uses a buffer size given by the method arguments and will not close the input stream when done.

Parameters:
in - Input stream to read from
out - Output stream to write to
buffSize - Buffer size
Returns:
The total number of bytes tranfered
Throws:
java.io.IOException - If an IO error occurs

toByteArray

public static byte[] toByteArray(java.nio.channels.FileChannel channel)
                          throws java.io.IOException
Read a complete channel to a byte array. This method must not be called with non-blocking channels and the file channel must be open to reading.

Parameters:
channel - File channel to read to byte array
Returns:
A byte array with the file content
Throws:
java.io.IOException - If an IO error occur

toByteArray

public static byte[] toByteArray(java.io.InputStream in)
                          throws java.io.IOException
Read a complete stream to a byte array. Uses the default buffer size of the class. This method does not close the stream when finished.

Parameters:
in - Input stream to read from.
Returns:
A byte array with the stream content
Throws:
java.io.IOException - If an IO error occur

toByteArray

public static byte[] toByteArray(java.io.InputStream in,
                                 int bufferSize)
                          throws java.io.IOException
Read a complete input stream to a byte array with a non-default buffer size. This method does not close the stream when finished.

Parameters:
in - Input stream to read from
bufferSize - Size of operation byte buffer
Returns:
A byte array with the stream content
Throws:
java.io.IOException - If an IO error occur

toString

public static java.lang.String toString(java.io.File file)
                                 throws java.io.IOException
Read a complete file to a string. This method asumes the default character encoding of the current platform.

Parameters:
file - File to read from
Returns:
A string read from the file
Throws:
java.io.IOException - If an IO error occur

toString

public static java.lang.String toString(java.io.InputStream in)
                                 throws java.io.IOException
Read an complete input stream to a string. This method asumes the default character encoding of the current platform. This method does not close the stream when it is finished.

Parameters:
in - Input stream to read from
Returns:
A string read from the stream
Throws:
java.io.IOException - If an IO error occur

toFile

public static void toFile(java.io.File file,
                          java.lang.String string)
                   throws java.io.IOException
Write the content of a string to a file. This method asumes the default character encoding of the platform. The string argument must not be null.

Parameters:
file - File to write the string to
string - String content
Throws:
java.io.IOException - If an IO error occur

toFile

public static void toFile(java.io.File file,
                          java.io.InputStream stream)
                   throws java.io.IOException
Write the content of an input stream to a file. This method asumes the default character encoding of the platform. The Stream argument must not be null. Thsi method does not close the strea,

Parameters:
file - File to write the string to
stream - Stream to write
Throws:
java.io.IOException - If an IO error occur