org.apache.commons.io
Class IOUtils

java.lang.Object
  extended by org.apache.commons.io.IOUtils

public final class IOUtils
extends 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-25 00:49:25 +0100 (Sun, 25 Apr 2004) $
Author:
Peter Donald, Jeff Turner

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(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.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtils

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

Method Detail

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

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(InputStream input)
Unconditionally close an InputStream. Equivalent to InputStream.close(), except any exceptions will be ignored.

Parameters:
input - A (possibly null) InputStream

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
Throws:
IOException - In case of an I/O problem

toString

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

Parameters:
input - the InputStream to read from
encoding - The name of a supported character encoding. See the IANA Charset Registry for a list of valid encoding types.
Returns:
the requested String
Throws:
IOException - In case of an I/O problem

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
Throws:
IOException - In case of an I/O problem

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
Throws:
IOException - In case of an I/O problem

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
Throws:
IOException - In case of an I/O problem

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
Throws:
IOException - In case of an I/O problem

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
Throws:
IOException - In case of an I/O problem

toString

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

Parameters:
input - the byte array to read from
encoding - The name of a supported character encoding. See the IANA Charset Registry for a list of valid encoding types.
Returns:
the requested String
Throws:
IOException - In case of an I/O problem

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
Throws:
IOException - In case of an I/O problem


Copyright © 2002-2005 The Apache Software Foundation. All Rights Reserved.