org.apache.commons.io

Class IOUtils


public final class IOUtils
extends java.lang.Object

General IO Stream manipulation.

This class provides static utility methods for input/output operations.

The closeQuietly methods are expected to be used when an IOException would be meaningless. This is usually when in a catch block for an IOException.

The toString and toByteArray methods all rely on CopyUtils.copy methods in the current implementation.

Origin of code: Apache Avalon (Excalibur)

Version:
CVS $Revision: 1.14 $ $Date: 2004/04/24 23:49:25 $
Authors:
Peter Donald
Jeff Turner

Field Summary

private static int
DEFAULT_BUFFER_SIZE

Constructor Summary

IOUtils()
Instances should NOT be constructed in standard programming.

Method Summary

static void
closeQuietly(InputStream input)
Unconditionally close an InputStream.
static void
closeQuietly(OutputStream output)
Unconditionally close an OutputStream.
static void
closeQuietly(Reader input)
Unconditionally close an Reader.
static void
closeQuietly(Writer output)
Unconditionally close an Writer.
static boolean
contentEquals(InputStream input1, InputStream input2)
Compare the contents of two Streams to determine if they are equal or not.
static byte[]
toByteArray(InputStream input)
Get the contents of an InputStream as a byte[].
static byte[]
toByteArray(Reader input)
Get the contents of a Reader as a byte[].
static byte[]
toByteArray(String input)
Get the contents of a String as a byte[].
static String
toString(InputStream input)
Get the contents of an InputStream as a String.
static String
toString(InputStream input, String encoding)
Get the contents of an InputStream as a String.
static String
toString(Reader input)
Get the contents of a Reader as a String.
static String
toString(byte[] input)
Get the contents of a byte[] as a String.
static String
toString(byte[] input, String encoding)
Get the contents of a byte[] as a String.

Field Details

DEFAULT_BUFFER_SIZE

private static final int DEFAULT_BUFFER_SIZE
Field Value:
4096

Constructor Details

IOUtils

public IOUtils()
Instances should NOT be constructed in standard programming.

Method Details

closeQuietly

public static void closeQuietly(InputStream input)
Unconditionally close an InputStream. Equivalent to InputStream.close(), except any exceptions will be ignored.
Parameters:
input - A (possibly null) InputStream

closeQuietly

public static void closeQuietly(OutputStream output)
Unconditionally close an OutputStream. Equivalent to OutputStream.close(), except any exceptions will be ignored.
Parameters:
output - A (possibly null) OutputStream

closeQuietly

public static void closeQuietly(Reader input)
Unconditionally close an Reader. Equivalent to Reader.close(), except any exceptions will be ignored.
Parameters:
input - A (possibly null) Reader

closeQuietly

public static void closeQuietly(Writer output)
Unconditionally close an Writer. Equivalent to Writer.close(), except any exceptions will be ignored.
Parameters:
output - A (possibly null) Writer

contentEquals

public static boolean contentEquals(InputStream input1,
                                    InputStream input2)
            throws IOException
Compare the contents of two Streams to determine if they are equal or not.
Parameters:
input1 - the first stream
input2 - the second stream
Returns:
true if the content of the streams are equal or they both don't exist, false otherwise

toByteArray

public static byte[] toByteArray(InputStream input)
            throws IOException
Get the contents of an InputStream as a byte[].
Parameters:
input - the InputStream to read from
Returns:
the requested byte array

toByteArray

public static byte[] toByteArray(Reader input)
            throws IOException
Get the contents of a Reader as a byte[].
Parameters:
input - the Reader to read from
Returns:
the requested byte array

toByteArray

public static byte[] toByteArray(String input)
            throws IOException
Get the contents of a String as a byte[].
Parameters:
input - the String to convert
Returns:
the requested byte array

toString

public static String toString(InputStream input)
            throws IOException
Get the contents of an InputStream as a String. The platform's default encoding is used for the byte-to-char conversion.
Parameters:
input - the InputStream to read from
Returns:
the requested String

toString

public static String toString(InputStream input,
                              String encoding)
            throws IOException
Get the contents of an InputStream as a String.
Returns:
the requested String

toString

public static String toString(Reader input)
            throws IOException
Get the contents of a Reader as a String.
Parameters:
input - the Reader to read from
Returns:
the requested String

toString

public static String toString(byte[] input)
            throws IOException
Get the contents of a byte[] as a String. The platform's default encoding is used for the byte-to-char conversion.
Parameters:
input - the byte array to read from
Returns:
the requested String

toString

public static String toString(byte[] input,
                              String encoding)
            throws IOException
Get the contents of a byte[] as a String.
Returns:
the requested String