Add encoding specific methods, Javadoc, Reformat to standards


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140593 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/commons/io/CopyUtils.java b/src/java/org/apache/commons/io/CopyUtils.java
index f7a8e64..3241229 100644
--- a/src/java/org/apache/commons/io/CopyUtils.java
+++ b/src/java/org/apache/commons/io/CopyUtils.java
@@ -98,12 +98,13 @@
  * @author Peter Donald
  * @author Jeff Turner
  * @author Matthew Hawthorne
- * @version $Id: CopyUtils.java,v 1.6 2004/04/24 23:49:25 bayard Exp $
+ * @author Stephen Colebourne
+ * @version $Id: CopyUtils.java,v 1.7 2004/07/31 09:52:09 scolebourne Exp $
  */
 public class CopyUtils {
 
     /**
-     * The name says it all.
+     * The default buffer size to use.
      */
     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 
@@ -112,75 +113,64 @@
      */
     public CopyUtils() {}
 
-    // ----------------------------------------------------------------
-    // byte[] -> OutputStream
-    // ----------------------------------------------------------------
-
+    // from byte[]
+    //-----------------------------------------------------------------------
     /**
      * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
-     * @param input the byte array to read from
-     * @param output the <code>OutputStream</code> to write to
-     * @throws IOException In case of an I/O problem
+     * 
+     * @param input  the byte array to read from
+     * @param output  the <code>OutputStream</code> to write to
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static void copy(byte[] input, OutputStream output)
-            throws IOException {
+    public static void copy(byte[] input, OutputStream output) throws IOException {
         output.write(input);
     }
 
-    // ----------------------------------------------------------------
-    // byte[] -> Writer
-    // ----------------------------------------------------------------
-
     /**
-     * Copy and convert bytes from a <code>byte[]</code> to chars on a
-     * <code>Writer</code>.
-     * The platform's default encoding is used for the byte-to-char conversion.
-     * @param input the byte array to read from
-     * @param output the <code>Writer</code> to write to
-     * @throws IOException In case of an I/O problem
+     * Copy bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
+     * using the default character encoding of the platform.
+     * 
+     * @param input  the byte array to read from
+     * @param output  the <code>Writer</code> to write to
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static void copy(byte[] input, Writer output)
-            throws IOException {
+    public static void copy(byte[] input, Writer output) throws IOException {
         ByteArrayInputStream in = new ByteArrayInputStream(input);
         copy(in, output);
     }
 
-
     /**
-     * Copy and convert bytes from a <code>byte[]</code> to chars on a
-     * <code>Writer</code>, using the specified encoding.
-     * @param input the byte array to read from
-     * @param output the <code>Writer</code> to write to
-     * @param encoding The name of a supported character encoding. See the
-     * <a href="http://www.iana.org/assignments/character-sets">IANA
-     * Charset Registry</a> for a list of valid encoding types.
-     * @throws IOException In case of an I/O problem
+     * Copy bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
+     * using the specified character encoding.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * 
+     * @param input  the byte array to read from
+     * @param output  the <code>Writer</code> to write to
+     * @param encoding  the encoding to use, null means platform default
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static void copy(
-            byte[] input,
-            Writer output,
-            String encoding)
-                throws IOException {
+    public static void copy(byte[] input, Writer output, String encoding) throws IOException {
         ByteArrayInputStream in = new ByteArrayInputStream(input);
         copy(in, output, encoding);
     }
 
-
-    // ----------------------------------------------------------------
-    // Core copy methods
-    // ----------------------------------------------------------------
-
+    // from InputStream
+    //-----------------------------------------------------------------------
     /**
      * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
-     * @param input the <code>InputStream</code> to read from
-     * @param output the <code>OutputStream</code> to write to
+     * 
+     * @param input  the <code>InputStream</code> to read from
+     * @param output  the <code>OutputStream</code> to write to
      * @return the number of bytes copied
-     * @throws IOException In case of an I/O problem
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static int copy(
-            InputStream input,
-            OutputStream output)
-                throws IOException {
+    public static int copy(InputStream input, OutputStream output) throws IOException {
         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
         int count = 0;
         int n = 0;
@@ -191,21 +181,54 @@
         return count;
     }
 
-    // ----------------------------------------------------------------
-    // Reader -> Writer
-    // ----------------------------------------------------------------
+    /**
+     * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
+     * using the default character encoding of the platform.
+     * 
+     * @param input  the <code>InputStream</code> to read from
+     * @param output  the <code>Writer</code> to write to
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static void copy(InputStream input, Writer output) throws IOException {
+        InputStreamReader in = new InputStreamReader(input);
+        copy(in, output);
+    }
 
     /**
-     * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
-     * @param input the <code>Reader</code> to read from
-     * @param output the <code>Writer</code> to write to
-     * @return the number of characters copied
-     * @throws IOException In case of an I/O problem
+     * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
+     * using the specified character encoding.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * 
+     * @param input  the <code>InputStream</code> to read from
+     * @param output  the <code>Writer</code> to write to
+     * @param encoding  the encoding to use, null means platform default
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static int copy(
-            Reader input,
-            Writer output)
-                throws IOException {
+    public static void copy(InputStream input, Writer output, String encoding) throws IOException {
+        if (encoding == null) {
+            copy(input, output);
+        } else {
+            InputStreamReader in = new InputStreamReader(input, encoding);
+            copy(in, output);
+        }
+    }
+
+    // from Reader
+    //-----------------------------------------------------------------------
+    /**
+     * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
+     * 
+     * @param input  the <code>Reader</code> to read from
+     * @param output  the <code>Writer</code> to write to
+     * @return the number of characters copied
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static int copy(Reader input, Writer output) throws IOException {
         char[] buffer = new char[DEFAULT_BUFFER_SIZE];
         int count = 0;
         int n = 0;
@@ -216,82 +239,76 @@
         return count;
     }
 
-    // ----------------------------------------------------------------
-    // InputStream -> Writer
-    // ----------------------------------------------------------------
-
     /**
-     * Copy and convert bytes from an <code>InputStream</code> to chars on a
-     * <code>Writer</code>.
-     * The platform's default encoding is used for the byte-to-char conversion.
-     * @param input the <code>InputStream</code> to read from
-     * @param output the <code>Writer</code> to write to
-     * @throws IOException In case of an I/O problem
+     * Copy chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>
+     * using the default character encoding of the platform, and calling flush.
+     * <p>
+     * Due to the implementation of OutputStreamWriter, this method performs a flush.
+     * 
+     * @param input  the <code>Reader</code> to read from
+     * @param output  the <code>OutputStream</code> to write to
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static void copy(
-            InputStream input,
-            Writer output)
-                throws IOException {
-        InputStreamReader in = new InputStreamReader(input);
-        copy(in, output);
-    }
-
-    /**
-     * Copy and convert bytes from an <code>InputStream</code> to chars on a
-     * <code>Writer</code>, using the specified encoding.
-     * @param input the <code>InputStream</code> to read from
-     * @param output the <code>Writer</code> to write to
-     * @param encoding The name of a supported character encoding. See the
-     * <a href="http://www.iana.org/assignments/character-sets">IANA
-     * Charset Registry</a> for a list of valid encoding types.
-     * @throws IOException In case of an I/O problem
-     */
-    public static void copy(
-            InputStream input,
-            Writer output,
-            String encoding)
-                throws IOException {
-        InputStreamReader in = new InputStreamReader(input, encoding);
-        copy(in, output);
-    }
-
-
-    // ----------------------------------------------------------------
-    // Reader -> OutputStream
-    // ----------------------------------------------------------------
-
-    /**
-     * Serialize chars from a <code>Reader</code> to bytes on an 
-     * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
-     * @param input the <code>Reader</code> to read from
-     * @param output the <code>OutputStream</code> to write to
-     * @throws IOException In case of an I/O problem
-     */
-    public static void copy(
-            Reader input,
-            OutputStream output)
-                throws IOException {
+    public static void copy(Reader input, OutputStream output) throws IOException {
         OutputStreamWriter out = new OutputStreamWriter(output);
         copy(input, out);
         // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
         out.flush();
     }
 
-    // ----------------------------------------------------------------
-    // String -> OutputStream
-    // ----------------------------------------------------------------
+    /**
+     * Copy chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>
+     * using the specified character encoding, and calling flush.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * <p>
+     * Due to the implementation of OutputStreamWriter, this method performs a flush.
+     * 
+     * @param input  the <code>Reader</code> to read from
+     * @param output  the <code>OutputStream</code> to write to
+     * @param encoding  the encoding to use, null means platform default
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static void copy(Reader input, OutputStream output, String encoding) throws IOException {
+        if (encoding == null) {
+            copy(input, output);
+        } else {
+            OutputStreamWriter out = new OutputStreamWriter(output, encoding);
+            copy(input, out);
+            // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
+            out.flush();
+        }
+    }
+
+    // from String
+    //-----------------------------------------------------------------------
+    /**
+     * Copy chars from a <code>String</code> to a <code>Writer</code>.
+     * 
+     * @param input  the <code>String</code> to read from
+     * @param output  the <code>Writer</code> to write to
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static void copy(String input, Writer output) throws IOException {
+        output.write(input);
+    }
 
     /**
-     * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
-     * flush the <code>OutputStream</code>.
-     * @param input the <code>String</code> to read from
-     * @param output the <code>OutputStream</code> to write to
-     * @throws IOException In case of an I/O problem
+     * Copy chars from a <code>String</code> to bytes on an <code>OutputStream</code>
+     * using the default character encoding of the platform, and calling flush.
+     * <p>
+     * Due to the implementation of OutputStreamWriter, this method performs a flush.
+     * 
+     * @param input  the <code>String</code> to read from
+     * @param output  the <code>OutputStream</code> to write to
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static void copy(
-            String input,
-            OutputStream output)
-                throws IOException {
+    public static void copy(String input, OutputStream output) throws IOException {
         StringReader in = new StringReader(input);
         OutputStreamWriter out = new OutputStreamWriter(output);
         copy(in, out);
@@ -299,19 +316,31 @@
         out.flush();
     }
 
-    // ----------------------------------------------------------------
-    // String -> Writer
-    // ----------------------------------------------------------------
-
     /**
-     * Copy chars from a <code>String</code> to a <code>Writer</code>.
-     * @param input the <code>String</code> to read from
-     * @param output the <code>Writer</code> to write to
-     * @throws IOException In case of an I/O problem
+     * Copy chars from a <code>String</code> to bytes on an <code>OutputStream</code>
+     * using the specified character encoding, and calling flush.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * <p>
+     * Due to the implementation of OutputStreamWriter, this method performs a flush.
+     * 
+     * @param input  the <code>String</code> to read from
+     * @param output  the <code>OutputStream</code> to write to
+     * @param encoding  the encoding to use, null means platform default
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
      */
-    public static void copy(String input, Writer output)
-                throws IOException {
-        output.write(input);
+    public static void copy(String input, OutputStream output, String encoding) throws IOException {
+        if (encoding == null) {
+            copy(input, output);
+        } else {
+            StringReader in = new StringReader(input);
+            OutputStreamWriter out = new OutputStreamWriter(output, encoding);
+            copy(in, out);
+            // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
+            out.flush();
+        }
     }
 
-} // CopyUtils
+}
diff --git a/src/java/org/apache/commons/io/IOUtils.java b/src/java/org/apache/commons/io/IOUtils.java
index a36c1d0..f479043 100644
--- a/src/java/org/apache/commons/io/IOUtils.java
+++ b/src/java/org/apache/commons/io/IOUtils.java
@@ -21,6 +21,7 @@
 import java.io.OutputStream;
 import java.io.Reader;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 
 import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -29,305 +30,302 @@
  * General IO Stream manipulation.
  * <p>
  * This class provides static utility methods for input/output operations.
- * </p>
- * <p>The closeQuietly methods are expected to be used when an IOException 
+ * <p>
+ * 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. </p>
- * <p>The toString and toByteArray methods all rely on CopyUtils.copy 
- * methods in the current implementation. </p>
+ * IOException.
+ * <p>
+ * The toString and toByteArray methods all rely on CopyUtils.copy 
+ * methods in the current implementation.
  *
  * <p>Origin of code: Apache Avalon (Excalibur)</p>
  *
- * @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
- * @version CVS $Revision: 1.16 $ $Date: 2004/06/13 05:38:07 $
+ * @author Peter Donald
+ * @author Jeff Turner
+ * @author Stephen Colebourne
+ * @version CVS $Revision: 1.17 $ $Date: 2004/07/31 09:52:09 $
  */
-public class IOUtils
-{
-    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
+public class IOUtils {
 
     /**
      * Instances should NOT be constructed in standard programming.
      */
-    public IOUtils() {}
+    public IOUtils() {
+    }
 
+    //-----------------------------------------------------------------------
     /**
      * Unconditionally close an <code>Reader</code>.
+     * <p>
      * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
+     * This is typically used in finally blocks.
      *
-     * @param input A (possibly null) Reader
+     * @param input  the Reader to close, may be null or already closed
      */
-    public static void closeQuietly( Reader input )
-    {
-        if( input == null )
-        {
-            return;
-        }
-
-        try
-        {
-            input.close();
-        }
-        catch( IOException ioe )
-        {
+    public static void closeQuietly(Reader input) {
+        try {
+            if (input != null) {
+                input.close();
+            }
+        } catch (IOException ioe) {
+            // ignore
         }
     }
 
     /**
-     * Unconditionally close an <code>Writer</code>.
+     * Unconditionally close a <code>Writer</code>.
+     * <p>
      * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
+     * This is typically used in finally blocks.
      *
-     * @param output A (possibly null) Writer
+     * @param output  the Writer to close, may be null or already closed
      */
-    public static void closeQuietly( Writer output )
-    {
-        if( output == null )
-        {
-            return;
-        }
-
-        try
-        {
-            output.close();
-        }
-        catch( IOException ioe )
-        {
-        }
-    }
-
-    /**
-     * Unconditionally close an <code>OutputStream</code>.
-     * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
-     * @param output A (possibly null) OutputStream
-     */
-    public static void closeQuietly( OutputStream output )
-    {
-        if( output == null )
-        {
-            return;
-        }
-
-        try
-        {
-            output.close();
-        }
-        catch( IOException ioe )
-        {
+    public static void closeQuietly(Writer output) {
+        try {
+            if (output != null) {
+                output.close();
+            }
+        } catch (IOException ioe) {
+            // ignore
         }
     }
 
     /**
      * Unconditionally close an <code>InputStream</code>.
+     * <p>
      * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
-     * @param input A (possibly null) InputStream
+     * This is typically used in finally blocks.
+     *
+     * @param input  the InputStream to close, may be null or already closed
      */
-    public static void closeQuietly( InputStream input )
-    {
-        if( input == null )
-        {
-            return;
-        }
-
-        try
-        {
-            input.close();
-        }
-        catch( IOException ioe )
-        {
+    public static void closeQuietly(InputStream input) {
+        try {
+            if (input != null) {
+                input.close();
+            }
+        } catch (IOException ioe) {
+            // ignore
         }
     }
 
     /**
-     * Get the contents of an <code>InputStream</code> as a String.
-     * The platform's default encoding is used for the byte-to-char conversion.
-     * @param input the <code>InputStream</code> to read from
-     * @return the requested <code>String</code>
-     * @throws IOException In case of an I/O problem
+     * Unconditionally close an <code>OutputStream</code>.
+     * <p>
+     * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
+     * This is typically used in finally blocks.
+     *
+     * @param input  the OutputStream to close, may be null or already closed
      */
-    public static String toString( InputStream input )
-        throws IOException
-    {
-        StringWriter sw = new StringWriter();
-        CopyUtils.copy( input, sw );
-        return sw.toString();
+    public static void closeQuietly( OutputStream output ) {
+        try {
+            if (output != null) {
+                output.close();
+            }
+        } catch (IOException ioe) {
+            // ignore
+        }
     }
 
-    /**
-     * Get the contents of an <code>InputStream</code> as a String.
-     * @param input the <code>InputStream</code> to read from
-     * @param encoding The name of a supported character encoding. See the
-     *   <a href="http://www.iana.org/assignments/character-sets">IANA
-     *   Charset Registry</a> for a list of valid encoding types.
-     * @return the requested <code>String</code>
-     * @throws IOException In case of an I/O problem
-     */
-    public static String toString( InputStream input,
-                                   String encoding )
-        throws IOException
-    {
-        StringWriter sw = new StringWriter();
-        CopyUtils.copy( input, sw, encoding );
-        return sw.toString();
-    }
-
-    ///////////////////////////////////////////////////////////////
-    // InputStream -> byte[]
-
+    // toByteArray
+    //-----------------------------------------------------------------------
     /**
      * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
-     * @param input the <code>InputStream</code> to read from
+     * 
+     * @param input  the <code>InputStream</code> to read from
      * @return the requested byte array
-     * @throws IOException In case of an I/O problem
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
      */
-    public static byte[] toByteArray( InputStream input )
-        throws IOException
-    {
+    public static byte[] toByteArray(InputStream input) throws IOException {
         ByteArrayOutputStream output = new ByteArrayOutputStream();
-        CopyUtils.copy( input, output );
+        CopyUtils.copy(input, output);
         return output.toByteArray();
     }
 
+    /**
+     * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
+     * using the default character encoding of the platform.
+     * 
+     * @param input  the <code>Reader</code> to read from
+     * @return the requested byte array
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static byte[] toByteArray(Reader input) throws IOException {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        CopyUtils.copy(input, output);
+        return output.toByteArray();
+    }
 
-    ///////////////////////////////////////////////////////////////
-    // Derived copy methods
-    // Reader -> *
-    ///////////////////////////////////////////////////////////////
+    /**
+     * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
+     * using the specified character encoding.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * 
+     * @param input  the <code>Reader</code> to read from
+     * @param encoding  the encoding to use, null means platform default
+     * @return the requested byte array
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static byte[] toByteArray(Reader input, String encoding) throws IOException {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        CopyUtils.copy(input, output, encoding);
+        return output.toByteArray();
+    }
 
-    ///////////////////////////////////////////////////////////////
-    // Reader -> String
+    /**
+     * Get the contents of a <code>String</code> as a <code>byte[]</code>
+     * using the default character encoding of the platform.
+     * <p>
+     * This is the same as {@link String#getBytes()}.
+     * 
+     * @param input  the <code>String</code> to convert
+     * @return the requested byte array
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     *  (never happens, but can't remove due to backwards compatibility)
+     */
+    public static byte[] toByteArray(String input) throws IOException {
+        return input.getBytes();
+    }
+
+    /**
+     * Get the contents of a <code>String</code> as a <code>byte[]</code>
+     * using the specified character encoding.
+     * <p>
+     * This is based on {@link String#getBytes(String)}.
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * 
+     * @param input  the <code>String</code> to convert
+     * @param encoding  the encoding to use, null means platform default
+     * @return the requested byte array
+     * @throws NullPointerException if the input is null
+     * @throws UnsupportedEncodingException if the named charset is not supported
+     */
+    public static byte[] toByteArray(String input, String encoding) throws UnsupportedEncodingException {
+        if (encoding == null) {
+            return input.getBytes();
+        }
+        return input.getBytes(encoding);
+    }
+
+    // toString
+    //-----------------------------------------------------------------------
+    /**
+     * Get the contents of an <code>InputStream</code> as a String
+     * using the default character encoding of the platform.
+     * 
+     * @param input  the <code>InputStream</code> to read from
+     * @return the requested String
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static String toString(InputStream input) throws IOException {
+        StringWriter sw = new StringWriter();
+        CopyUtils.copy(input, sw);
+        return sw.toString();
+    }
+
+    /**
+     * Get the contents of an <code>InputStream</code> as a String
+     * using the specified character encoding.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * 
+     * @param input  the <code>InputStream</code> to read from
+     * @param encoding  the encoding to use, null means platform default
+     * @return the requested String
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     */
+    public static String toString(InputStream input, String encoding) throws IOException {
+        StringWriter sw = new StringWriter();
+        CopyUtils.copy(input, sw, encoding);
+        return sw.toString();
+    }
+
     /**
      * Get the contents of a <code>Reader</code> as a String.
-     * @param input the <code>Reader</code> to read from
-     * @return the requested <code>String</code>
-     * @throws IOException In case of an I/O problem
+     * 
+     * @param input  the <code>Reader</code> to read from
+     * @return the requested String
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
      */
-    public static String toString( Reader input )
-        throws IOException
-    {
+    public static String toString(Reader input) throws IOException {
         StringWriter sw = new StringWriter();
-        CopyUtils.copy( input, sw );
+        CopyUtils.copy(input, sw);
         return sw.toString();
     }
 
-
-    ///////////////////////////////////////////////////////////////
-    // Reader -> byte[]
     /**
-     * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
-     * @param input the <code>Reader</code> to read from
-     * @return the requested byte array
-     * @throws IOException In case of an I/O problem
-     */
-    public static byte[] toByteArray( Reader input )
-        throws IOException
-    {
-        ByteArrayOutputStream output = new ByteArrayOutputStream();
-        CopyUtils.copy( input, output );
-        return output.toByteArray();
-    }
-
-
-    ///////////////////////////////////////////////////////////////
-    // Derived copy methods
-    // String -> *
-    ///////////////////////////////////////////////////////////////
-
-
-    ///////////////////////////////////////////////////////////////
-    // String -> byte[]
-    /**
-     * Get the contents of a <code>String</code> as a <code>byte[]</code>.
-     * @param input the <code>String</code> to convert
-     * @return the requested byte array
-     * @throws IOException In case of an I/O problem
-     * @deprecated This is reundant, use java.lang.String.toByteArray.
-     */
-    public static byte[] toByteArray( String input )
-        throws IOException
-    {
-        ByteArrayOutputStream output = new ByteArrayOutputStream();
-        CopyUtils.copy( input, output );
-        return output.toByteArray();
-    }
-
-
-    ///////////////////////////////////////////////////////////////
-    // Derived copy methods
-    // byte[] -> *
-    ///////////////////////////////////////////////////////////////
-
-    ///////////////////////////////////////////////////////////////
-    // byte[] -> String
-
-    /**
-     * Get the contents of a <code>byte[]</code> as a String.
-     * The platform's default encoding is used for the byte-to-char conversion.
+     * Get the contents of a <code>byte[]</code> as a String
+     * using the default character encoding of the platform.
+     * 
      * @param input the byte array to read from
-     * @return the requested <code>String</code>
-     * @throws IOException In case of an I/O problem
+     * @return the requested String
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
      */
-    public static String toString( byte[] input )
-        throws IOException
-    {
+    public static String toString(byte[] input) throws IOException {
         StringWriter sw = new StringWriter();
-        CopyUtils.copy( input, sw );
+        CopyUtils.copy(input, sw);
         return sw.toString();
     }
 
-
     /**
-     * Get the contents of a <code>byte[]</code> as a String.
+     * Get the contents of a <code>byte[]</code> as a String
+     * using the specified character encoding.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     * 
      * @param input the byte array to read from
-     * @param encoding The name of a supported character encoding. See the
-     *   <a href="http://www.iana.org/assignments/character-sets">IANA
-     *   Charset Registry</a> for a list of valid encoding types.
-     * @return the requested <code>String</code>
-     * @throws IOException In case of an I/O problem
+     * @param encoding  the encoding to use, null means platform default
+     * @return the requested String
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
      */
-    public static String toString( byte[] input,
-                                   String encoding )
-        throws IOException
-    {
+    public static String toString(byte[] input, String encoding) throws IOException {
         StringWriter sw = new StringWriter();
-        CopyUtils.copy( input, sw, encoding );
+        CopyUtils.copy(input, sw, encoding);
         return sw.toString();
     }
 
-
+    //-----------------------------------------------------------------------
     /**
      * Compare the contents of two Streams to determine if they are equal or not.
      *
-     * @param input1 the first stream
-     * @param input2 the second stream
+     * @param input1  the first stream
+     * @param input2  the second stream
      * @return 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
+     * @throws NullPointerException if either input is null
+     * @throws IOException if an I/O error occurs
      */
-    public static boolean contentEquals( InputStream input1,
-                                         InputStream input2 )
-        throws IOException
-    {
-        InputStream bufferedInput1 = new BufferedInputStream( input1 );
-        InputStream bufferedInput2 = new BufferedInputStream( input2 );
+    public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException {
+        InputStream bufferedInput1 = new BufferedInputStream(input1);
+        InputStream bufferedInput2 = new BufferedInputStream(input2);
 
         int ch = bufferedInput1.read();
-        while( -1 != ch )
-        {
+        while (-1 != ch) {
             int ch2 = bufferedInput2.read();
-            if( ch != ch2 )
-            {
+            if (ch != ch2) {
                 return false;
             }
             ch = bufferedInput1.read();
         }
 
         int ch2 = bufferedInput2.read();
-        if( -1 != ch2 )
-        {
+        if (-1 != ch2) {
             return false;
-        }
-        else
-        {
+        } else {
             return true;
         }
     }
+
 }