scolebourne    2004/08/24 12:13:12

  Modified:    io/src/test/org/apache/commons/io/testtools
                        FileBasedTestCase.java
               io/src/test/org/apache/commons/io IOUtilsTestCase.java
               io/src/java/org/apache/commons/io IOUtils.java
  Log:
  Add toCharArray()

  bug 28979, from Gareth Davis
  
  Revision  Changes    Path
  1.7       +21 -0     
jakarta-commons/io/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java
  
  Index: FileBasedTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FileBasedTestCase.java    23 Feb 2004 05:02:25 -0000      1.6
  +++ FileBasedTestCase.java    24 Aug 2004 19:13:12 -0000      1.7
  @@ -20,6 +20,7 @@
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.OutputStream;
  +import java.io.Reader;
   import java.io.Writer;
   import java.util.Arrays;
   
  @@ -34,6 +35,7 @@
    * Base class for testcases doing tests with files.
    * 
    * @author Jeremias Maerki
  + * @author Gareth Davis
    */
   public abstract class FileBasedTestCase extends TestCase {
   
  @@ -157,6 +159,25 @@
                   );
           } finally {
               is.close();
  +        }
  +    }
  +
  +    /** Assert that the content of a file is equal to that in a char[]. */
  +    protected void assertEqualContent( char[] c0, File file )
  +        throws IOException
  +    {
  +        Reader ir = new java.io.FileReader( file );
  +        try {
  +            char[] c1 = new char[ c0.length ];
  +            int numRead = ir.read( c1 );
  +            assertTrue( "Different number of bytes", numRead == c0.length );
  +            for( int i = 0;
  +                 i < numRead;
  +                 assertTrue( "Byte " + i + " differs (" + c0[ i ] + " != " + c1[ i 
] + ")", 
  +                    c0[ i ] == c1[ i ] ), i++
  +                );
  +        } finally {
  +            ir.close();
           }
       }
   
  
  
  
  1.9       +47 -1     
jakarta-commons/io/src/test/org/apache/commons/io/IOUtilsTestCase.java
  
  Index: IOUtilsTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/IOUtilsTestCase.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- IOUtilsTestCase.java      23 Feb 2004 05:02:25 -0000      1.8
  +++ IOUtilsTestCase.java      24 Aug 2004 19:13:12 -0000      1.9
  @@ -41,6 +41,7 @@
    * multiple tests to fail.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Turner</a>
  + * @author Gareth Davis
    */
   public class IOUtilsTestCase extends FileBasedTestCase {
       /*
  @@ -278,6 +279,51 @@
           deleteFile( destination );
       }
   
  +    public void testInputStreamToCharArray()
  +            throws Exception
  +    {
  +        FileInputStream fin = new FileInputStream( m_testFile );
  +        try {
  +            char[] out = IOUtils.toCharArray( fin );
  +            assertNotNull( out );
  +            assertTrue( "Not all chars were read", fin.available() == 0 );
  +            assertTrue( "Wrong output size: out.length=" + out.length +
  +                        "!=" + FILE_SIZE, out.length == FILE_SIZE );
  +            assertEqualContent( out, m_testFile );
  +        } finally {
  +            fin.close();
  +        }
  +    }
   
  -}
  +    public void testInputStreamToCharArrayWithEncoding()
  +            throws Exception
  +    {
  +        FileInputStream fin = new FileInputStream( m_testFile );
  +        try {
  +            char[] out = IOUtils.toCharArray( fin , "UTF-8" );
  +            assertNotNull( out );
  +            assertTrue( "Not all chars were read", fin.available() == 0 );
  +            assertTrue( "Wrong output size: out.length=" + out.length +
  +                        "!=" + FILE_SIZE, out.length == FILE_SIZE );
  +            assertEqualContent( out, m_testFile );
  +        } finally {
  +            fin.close();
  +        }
  +    }
   
  +    public void testReaderToCharArray()
  +            throws Exception
  +    {
  +        FileReader fr = new FileReader( m_testFile );
  +        try {
  +            char[] out = IOUtils.toCharArray( fr );
  +            assertNotNull( out );
  +            assertTrue( "Wrong output size: out.length=" + out.length +
  +                        "!=" + FILE_SIZE, out.length == FILE_SIZE );
  +            assertEqualContent( out, m_testFile );
  +        } finally {
  +            fr.close();
  +        }
  +    }
  +
  +}
  
  
  
  1.20      +62 -1     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.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- IOUtils.java      13 Aug 2004 23:39:01 -0000      1.19
  +++ IOUtils.java      24 Aug 2004 19:13:12 -0000      1.20
  @@ -17,6 +17,7 @@
   
   import java.io.BufferedInputStream;
   import java.io.BufferedReader;
  +import java.io.CharArrayWriter;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
  @@ -61,6 +62,7 @@
    * @author Jeff Turner
    * @author Matthew Hawthorne
    * @author Stephen Colebourne
  + * @author Gareth Davis
    * @version CVS $Revision$ $Date$
    */
   public class IOUtils {
  @@ -226,6 +228,65 @@
        */
       public static byte[] toByteArray(String input) throws IOException {
           return input.getBytes();
  +    }
  +
  +    // read char[]
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Get the contents of an <code>InputStream</code> as a character array
  +     * using the default character encoding of the platform.
  +     * <p>
  +     * This method buffers the input internally, so there is no need to use a
  +     * <code>BufferedInputStream</code>.
  +     * 
  +     * @param input  the <code>InputStream</code> to read from
  +     * @return the requested character array
  +     * @throws NullPointerException if the input is null
  +     * @throws IOException if an I/O error occurs
  +     */
  +    public static char[] toCharArray(InputStream is) throws IOException {
  +        CharArrayWriter output = new CharArrayWriter();
  +        copy(is, output);
  +        return output.toCharArray();
  +    }
  +
  +    /**
  +     * Get the contents of an <code>InputStream</code> as a character array
  +     * using the specified character encoding.
  +     * <p>
  +     * Character encoding names can be found at
  +     * <a href="http://www.iana.org/assignments/character-sets";>IANA</a>.
  +     * <p>
  +     * This method buffers the input internally, so there is no need to use a
  +     * <code>BufferedInputStream</code>.
  +     * 
  +     * @param input  the <code>InputStream</code> to read from
  +     * @param encoding  the encoding to use, null means platform default
  +     * @return the requested character array
  +     * @throws NullPointerException if the input is null
  +     * @throws IOException if an I/O error occurs
  +     */
  +    public static char[] toCharArray(InputStream is, String encoding) throws 
IOException {
  +        CharArrayWriter output = new CharArrayWriter();
  +        copy(is, output, encoding);
  +        return output.toCharArray();
  +    }
  +
  +    /**
  +     * Get the contents of a <code>Reader</code> as a character array.
  +     * <p>
  +     * This method buffers the input internally, so there is no need to use a
  +     * <code>BufferedReader</code>.
  +     * 
  +     * @param input  the <code>Reader</code> to read from
  +     * @return the requested character array
  +     * @throws NullPointerException if the input is null
  +     * @throws IOException if an I/O error occurs
  +     */
  +    public static char[] toCharArray(Reader input) throws IOException {
  +        CharArrayWriter sw = new CharArrayWriter();
  +        copy(input, sw);
  +        return sw.toCharArray();
       }
   
       // read toString
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to