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 12:40:47 UTC

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

scolebourne    2004/07/31 03:40:47

  Modified:    io/src/test/org/apache/commons/io CopyUtilsTest.java
               io/src/java/org/apache/commons/io CopyUtils.java
  Log:
  Add char[] methods to CopyUtils
  
  Revision  Changes    Path
  1.7       +184 -2    jakarta-commons/io/src/test/org/apache/commons/io/CopyUtilsTest.java
  
  Index: CopyUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/CopyUtilsTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CopyUtilsTest.java	23 Feb 2004 05:02:25 -0000	1.6
  +++ CopyUtilsTest.java	31 Jul 2004 10:40:47 -0000	1.7
  @@ -80,6 +80,7 @@
       // Tests
       // ----------------------------------------------------------------
   
  +    //-----------------------------------------------------------------------
       public void testCopy_byteArrayToOutputStream() throws Exception {
           ByteArrayOutputStream baout = new ByteArrayOutputStream();
           OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  @@ -102,6 +103,32 @@
           assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
       }
   
  +    public void testCopy_byteArrayToWriter_nullEncoding() throws Exception {
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +        Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
  +        
  +        CopyUtils.copy(inData, writer, null);
  +        writer.flush();
  +
  +        assertEquals("Sizes differ", inData.length, baout.size());
  +        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
  +    }
  +
  +    public void testCopy_byteArrayToWriter_Encoding() throws Exception {
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +        Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
  +        
  +        CopyUtils.copy(inData, writer, "UTF8");
  +        writer.flush();
  +
  +        byte[] bytes = baout.toByteArray();
  +        bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
  +        assertTrue("Content differs", Arrays.equals(inData, bytes));
  +    }
  +
  +    //-----------------------------------------------------------------------
       public void testCopy_inputStreamToOutputStream() throws Exception {
           InputStream in = new ByteArrayInputStream(inData);
           in = new YellOnCloseInputStream(in);
  @@ -132,6 +159,40 @@
           assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
       }
   
  +    public void testCopy_inputStreamToWriter_nullEncoding() throws Exception {
  +        InputStream in = new ByteArrayInputStream(inData);
  +        in = new YellOnCloseInputStream(in);
  +
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +        Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
  +        
  +        CopyUtils.copy(in, writer, null);
  +        writer.flush();
  +
  +        assertTrue("Not all bytes were read", in.available() == 0);
  +        assertEquals("Sizes differ", inData.length, baout.size());
  +        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
  +    }
  +
  +    public void testCopy_inputStreamToWriter_Encoding() throws Exception {
  +        InputStream in = new ByteArrayInputStream(inData);
  +        in = new YellOnCloseInputStream(in);
  +
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +        Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
  +        
  +        CopyUtils.copy(in, writer, "UTF8");
  +        writer.flush();
  +
  +        assertTrue("Not all bytes were read", in.available() == 0);
  +        byte[] bytes = baout.toByteArray();
  +        bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
  +        assertTrue("Content differs", Arrays.equals(inData, bytes));
  +    }
  +
  +    //-----------------------------------------------------------------------
       public void testCopy_readerToOutputStream() throws Exception {
           InputStream in = new ByteArrayInputStream(inData);
           in = new YellOnCloseInputStream(in);
  @@ -152,6 +213,39 @@
           assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
       }
   
  +    public void testCopy_readerToOutputStream_nullEncoding() throws Exception {
  +        InputStream in = new ByteArrayInputStream(inData);
  +        in = new YellOnCloseInputStream(in);
  +        Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
  +        
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +
  +        CopyUtils.copy(reader, out, null);
  +        // note: this method *does* flush.
  +        // note: we don't flush here; this IOUtils method does it for us
  +
  +        assertEquals("Sizes differ", inData.length, baout.size());
  +        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
  +    }
  +
  +    public void testCopy_readerToOutputStream_Encoding() throws Exception {
  +        InputStream in = new ByteArrayInputStream(inData);
  +        in = new YellOnCloseInputStream(in);
  +        Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
  +        
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +
  +        CopyUtils.copy(reader, out, "UTF16");
  +        // note: this method *does* flush.
  +        // note: we don't flush here; this IOUtils method does it for us
  +        
  +        byte[] bytes = baout.toByteArray();
  +        bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
  +        assertTrue("Content differs", Arrays.equals(inData, bytes));
  +    }
  +
       public void testCopy_readerToWriter() throws Exception {
           InputStream in = new ByteArrayInputStream(inData);
           in = new YellOnCloseInputStream(in);
  @@ -171,6 +265,7 @@
           assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
       }
   
  +    //-----------------------------------------------------------------------
       public void testCopy_stringToOutputStream() throws Exception {
           String str = new String(inData, "US-ASCII");
           
  @@ -189,6 +284,35 @@
           assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
       }
   
  +    public void testCopy_stringToOutputStream_nullEncoding() throws Exception {
  +        String str = new String(inData, "US-ASCII");
  +        
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +
  +        CopyUtils.copy(str, out, null);
  +        // note: this method *does* flush.
  +        // note: we don't flush here; this IOUtils method does it for us
  +
  +        assertEquals("Sizes differ", inData.length, baout.size());
  +        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
  +    }
  +
  +    public void testCopy_stringToOutputStream_Encoding() throws Exception {
  +        String str = new String(inData, "US-ASCII");
  +        
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +
  +        CopyUtils.copy(str, out, "UTF16");
  +        // note: this method *does* flush.
  +        // note: we don't flush here; this IOUtils method does it for us
  +        
  +        byte[] bytes = baout.toByteArray();
  +        bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
  +        assertTrue("Content differs", Arrays.equals(inData, bytes));
  +    }
  +
       public void testCopy_stringToWriter() throws Exception {
           String str = new String(inData, "US-ASCII");
   
  @@ -203,4 +327,62 @@
           assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
       }
   
  -} // CopyUtilsTest
  +    //-----------------------------------------------------------------------
  +    public void testCopy_charArrayToOutputStream() throws Exception {
  +        String str = new String(inData, "US-ASCII");
  +        
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +
  +        CopyUtils.copy(str.toCharArray(), out);
  +        // note: this method *does* flush.
  +        // note: we don't flush here; this IOUtils method does it for us
  +
  +        assertEquals("Sizes differ", inData.length, baout.size());
  +        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
  +    }
  +
  +    public void testCopy_charArrayToOutputStream_nullEncoding() throws Exception {
  +        String str = new String(inData, "US-ASCII");
  +        
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +
  +        CopyUtils.copy(str.toCharArray(), out, null);
  +        // note: this method *does* flush.
  +        // note: we don't flush here; this IOUtils method does it for us
  +
  +        assertEquals("Sizes differ", inData.length, baout.size());
  +        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
  +    }
  +
  +    public void testCopy_charArrayToOutputStream_Encoding() throws Exception {
  +        String str = new String(inData, "US-ASCII");
  +        
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +
  +        CopyUtils.copy(str.toCharArray(), out, "UTF16");
  +        // note: this method *does* flush.
  +        // note: we don't flush here; this IOUtils method does it for us
  +        
  +        byte[] bytes = baout.toByteArray();
  +        bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
  +        assertTrue("Content differs", Arrays.equals(inData, bytes));
  +    }
  +
  +    public void testCopy_charArrayToWriter() throws Exception {
  +        String str = new String(inData, "US-ASCII");
  +
  +        ByteArrayOutputStream baout = new ByteArrayOutputStream();
  +        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  +        Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
  +
  +        CopyUtils.copy(str.toCharArray(), writer);
  +        writer.flush();
  +
  +        assertEquals("Sizes differ", inData.length, baout.size());
  +        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
  +    }
  +
  +}
  
  
  
  1.8       +76 -4     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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- CopyUtils.java	31 Jul 2004 09:52:09 -0000	1.7
  +++ CopyUtils.java	31 Jul 2004 10:40:47 -0000	1.8
  @@ -16,6 +16,7 @@
   package org.apache.commons.io;
   
   import java.io.ByteArrayInputStream;
  +import java.io.CharArrayReader;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
  @@ -86,6 +87,9 @@
    *
    * 7     copy        byte[]              Writer          3
    * 8     copy        byte[]              OutputStream    (trivial)
  + *
  + * 9     copy        char[]              OutputStream    4
  + * 10    copy        char[]              Writer          (trivial)
    * </pre>
    *
    * <p>Note that only the first two methods shuffle bytes; the rest use these
  @@ -118,7 +122,7 @@
       /**
        * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
        * 
  -     * @param input  the byte array to read from
  +     * @param input  the byte array to read from, do not modify during output
        * @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
  @@ -130,8 +134,10 @@
       /**
        * Copy bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
        * using the default character encoding of the platform.
  +     * <p>
  +     * This method uses {@link ByteArrayInputStream} and {@link InputStreamReader}.
        * 
  -     * @param input  the byte array to read from
  +     * @param input  the byte array to read from, do not modify during output
        * @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
  @@ -147,8 +153,10 @@
        * <p>
        * Character encoding names can be found at
        * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
  +     * <p>
  +     * This method uses {@link ByteArrayInputStream} and {@link InputStreamReader}.
        * 
  -     * @param input  the byte array to read from
  +     * @param input  the byte array to read from, do not modify during output
        * @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
  @@ -184,6 +192,8 @@
       /**
        * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
        * using the default character encoding of the platform.
  +     * <p>
  +     * This method uses {@link InputStreamReader}.
        * 
        * @param input  the <code>InputStream</code> to read from
        * @param output  the <code>Writer</code> to write to
  @@ -201,6 +211,8 @@
        * <p>
        * Character encoding names can be found at
        * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
  +     * <p>
  +     * This method uses {@link InputStreamReader}.
        * 
        * @param input  the <code>InputStream</code> to read from
        * @param output  the <code>Writer</code> to write to
  @@ -217,6 +229,58 @@
           }
       }
   
  +    // from char[]
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Copy chars from a <code>char[]</code> to a <code>Writer</code>
  +     * using the default character encoding of the platform.
  +     * <p>
  +     * This method uses {@link CharArrayReader}.
  +     * 
  +     * @param input  the char array to read from, do not modify during output
  +     * @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(char[] input, Writer output) throws IOException {
  +        output.write(input);
  +    }
  +
  +    /**
  +     * Copy chars from a <code>char[]</code> to bytes on an <code>OutputStream</code>.
  +     * <p>
  +     * This method uses {@link CharArrayReader} and {@link OutputStreamWriter}.
  +     * 
  +     * @param input  the char array to read from, do not modify during output
  +     * @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(char[] input, OutputStream output) throws IOException {
  +        CharArrayReader in = new CharArrayReader(input);
  +        copy(in, output);
  +    }
  +
  +    /**
  +     * Copy chars from a <code>char[]</code> to bytes on an <code>OutputStream</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>.
  +     * <p>
  +     * This method uses {@link CharArrayReader} and {@link OutputStreamWriter}.
  +     * 
  +     * @param input  the char array to read from, do not modify during output
  +     * @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(char[] input, OutputStream output, String encoding) throws IOException {
  +        CharArrayReader in = new CharArrayReader(input);
  +        copy(in, output, encoding);
  +    }
  +
       // from Reader
       //-----------------------------------------------------------------------
       /**
  @@ -244,6 +308,8 @@
        * using the default character encoding of the platform, and calling flush.
        * <p>
        * Due to the implementation of OutputStreamWriter, this method performs a flush.
  +     * <p>
  +     * This method uses {@link OutputStreamWriter}.
        * 
        * @param input  the <code>Reader</code> to read from
        * @param output  the <code>OutputStream</code> to write to
  @@ -265,6 +331,8 @@
        * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
        * <p>
        * Due to the implementation of OutputStreamWriter, this method performs a flush.
  +     * <p>
  +     * This method uses {@link OutputStreamWriter}.
        * 
        * @param input  the <code>Reader</code> to read from
        * @param output  the <code>OutputStream</code> to write to
  @@ -302,6 +370,8 @@
        * using the default character encoding of the platform, and calling flush.
        * <p>
        * Due to the implementation of OutputStreamWriter, this method performs a flush.
  +     * <p>
  +     * This method uses {@link StringReader} and {@link OutputStreamWriter}.
        * 
        * @param input  the <code>String</code> to read from
        * @param output  the <code>OutputStream</code> to write to
  @@ -324,6 +394,8 @@
        * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
        * <p>
        * Due to the implementation of OutputStreamWriter, this method performs a flush.
  +     * <p>
  +     * This method uses {@link StringReader} and {@link OutputStreamWriter}.
        * 
        * @param input  the <code>String</code> to read from
        * @param output  the <code>OutputStream</code> to write to
  
  
  

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