You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/07/31 11:52:09 UTC

cvs commit: jakarta-commons/io/src/java/org/apache/commons/io CopyUtils.java IOUtils.java

scolebourne    2004/07/31 02:52:09

  Modified:    io/src/java/org/apache/commons/io CopyUtils.java
                        IOUtils.java
  Log:
  Add encoding specific methods, Javadoc, Reformat to standards
  
  Revision  Changes    Path
  1.7       +169 -140  jakarta-commons/io/src/java/org/apache/commons/io/CopyUtils.java
  
  Index: CopyUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/CopyUtils.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CopyUtils.java	24 Apr 2004 23:49:25 -0000	1.6
  +++ CopyUtils.java	31 Jul 2004 09:52:09 -0000	1.7
  @@ -98,12 +98,13 @@
    * @author Peter Donald
    * @author Jeff Turner
    * @author Matthew Hawthorne
  + * @author Stephen Colebourne
    * @version $Id$
    */
   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
  -     */
  -    public static void copy(
  -            byte[] input,
  -            Writer output,
  -            String encoding)
  -                throws IOException {
  +     * 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 {
           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 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 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
  +     * 
  +     * @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
  +     * @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 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
  -     */
  -    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 {
  +     * 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(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
  -     */
  -    public static void copy(
  -            String input,
  -            OutputStream output)
  -                throws IOException {
  +     * 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 {
           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
  -     */
  -    public static void copy(String input, Writer output)
  -                throws IOException {
  -        output.write(input);
  +     * 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, 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
  +}
  
  
  
  1.17      +205 -207  jakarta-commons/io/src/java/org/apache/commons/io/IOUtils.java
  
  Index: IOUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/IOUtils.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- IOUtils.java	13 Jun 2004 05:38:07 -0000	1.16
  +++ IOUtils.java	31 Jul 2004 09:52:09 -0000	1.17
  @@ -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>
  + * @author Peter Donald
  + * @author Jeff Turner
  + * @author Stephen Colebourne
    * @version CVS $Revision$ $Date$
    */
  -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 )
  -        {
  +    public static void closeQuietly(Writer output) {
  +        try {
  +            if (output != null) {
  +                output.close();
  +            }
  +        } catch (IOException ioe) {
  +            // ignore
           }
       }
   
       /**
  -     * Unconditionally close an <code>OutputStream</code>.
  -     * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
  -     * @param output A (possibly null) OutputStream
  +     * Unconditionally close an <code>InputStream</code>.
  +     * <p>
  +     * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
  +     * This is typically used in finally blocks.
  +     *
  +     * @param input  the InputStream to close, may be null or already closed
        */
  -    public static void closeQuietly( OutputStream output )
  -    {
  -        if( output == null )
  -        {
  -            return;
  -        }
  -
  -        try
  -        {
  -            output.close();
  -        }
  -        catch( IOException ioe )
  -        {
  +    public static void closeQuietly(InputStream input) {
  +        try {
  +            if (input != null) {
  +                input.close();
  +            }
  +        } catch (IOException ioe) {
  +            // ignore
           }
       }
   
       /**
  -     * Unconditionally close an <code>InputStream</code>.
  -     * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
  -     * @param input A (possibly null) InputStream
  +     * 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 void closeQuietly( InputStream input )
  -    {
  -        if( input == null )
  -        {
  -            return;
  -        }
  -
  -        try
  -        {
  -            input.close();
  -        }
  -        catch( IOException ioe )
  -        {
  +    public static void closeQuietly( OutputStream output ) {
  +        try {
  +            if (output != null) {
  +                output.close();
  +            }
  +        } catch (IOException ioe) {
  +            // ignore
           }
       }
   
  +    // toByteArray
  +    //-----------------------------------------------------------------------
       /**
  -     * 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
  -     */
  -    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 <code>byte[]</code>.
  +     * 
  +     * @param input  the <code>InputStream</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(InputStream input) throws IOException {
  +        ByteArrayOutputStream output = new ByteArrayOutputStream();
  +        CopyUtils.copy(input, output);
  +        return output.toByteArray();
       }
   
       /**
  -     * 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();
  +     * 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();
       }
   
  -    ///////////////////////////////////////////////////////////////
  -    // InputStream -> byte[]
  -
       /**
  -     * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
  -     * @param input the <code>InputStream</code> to read from
  +     * 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 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(Reader input, String encoding) throws IOException {
           ByteArrayOutputStream output = new ByteArrayOutputStream();
  -        CopyUtils.copy( input, output );
  +        CopyUtils.copy(input, output, encoding);
           return output.toByteArray();
       }
   
  +    /**
  +     * 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();
  +    }
   
  -    ///////////////////////////////////////////////////////////////
  -    // Derived copy methods
  -    // Reader -> *
  -    ///////////////////////////////////////////////////////////////
  +    /**
  +     * 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);
  +    }
   
  -    ///////////////////////////////////////////////////////////////
  -    // Reader -> String
  +    // 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
  -     */
  -    public static String toString( Reader input )
  -        throws IOException
  -    {
  +     * 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 );
  +        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
  +     * 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 byte[] toByteArray( Reader input )
  -        throws IOException
  -    {
  -        ByteArrayOutputStream output = new ByteArrayOutputStream();
  -        CopyUtils.copy( input, output );
  -        return output.toByteArray();
  +    public static String toString(InputStream input, String encoding) throws IOException {
  +        StringWriter sw = new StringWriter();
  +        CopyUtils.copy(input, sw, encoding);
  +        return sw.toString();
       }
   
  -
  -    ///////////////////////////////////////////////////////////////
  -    // 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.
  +     * Get the contents of a <code>Reader</code> as a String.
  +     * 
  +     * @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 byte[] toByteArray( String input )
  -        throws IOException
  -    {
  -        ByteArrayOutputStream output = new ByteArrayOutputStream();
  -        CopyUtils.copy( input, output );
  -        return output.toByteArray();
  +    public static String toString(Reader input) throws IOException {
  +        StringWriter sw = new StringWriter();
  +        CopyUtils.copy(input, sw);
  +        return sw.toString();
       }
   
  -
  -    ///////////////////////////////////////////////////////////////
  -    // 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
  -     */
  -    public static String toString( byte[] input,
  -                                   String encoding )
  -        throws IOException
  -    {
  +     * @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 {
           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;
           }
       }
  +
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org