Author: niallp
Date: Wed Feb  6 14:33:04 2008
New Revision: 619188

URL: http://svn.apache.org/viewvc?rev=619188&view=rev
Log:
IO-140 JDK 1.5 changes: Add new CharSequence flavours of methods

Modified:
    commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
    
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
    
commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=619188&r1=619187&r2=619188&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java 
(original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Wed 
Feb  6 14:33:04 2008
@@ -1267,6 +1267,34 @@
     }
 
     /**
+     * Writes a CharSequence to a file creating the file if it does not exist 
using the default encoding for the VM.
+     * 
+     * @param file  the file to write
+     * @param data  the content to write to the file
+     * @throws IOException in case of an I/O error
+     * @since IO 2.0
+     */
+    public static void write(File file, CharSequence data) throws IOException {
+        String str = data == null ? null : data.toString();
+        writeStringToFile(file, str);
+    }
+
+    /**
+     * Writes a CharSequence to a file creating the file if it does not exist.
+     *
+     * @param file  the file to write
+     * @param data  the content to write to the file
+     * @param encoding  the encoding to use, <code>null</code> means platform 
default
+     * @throws IOException in case of an I/O error
+     * @throws java.io.UnsupportedEncodingException if the encoding is not 
supported by the VM
+     * @since IO 2.0
+     */
+    public static void write(File file, CharSequence data, String encoding) 
throws IOException {
+        String str = data == null ? null : data.toString();
+        writeStringToFile(file, str, encoding);
+    }
+
+    /**
      * Writes a byte array to a file creating the file if it does not exist.
      * <p>
      * NOTE: As from v1.3, the parent directories of the file will be created

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java?rev=619188&r1=619187&r2=619188&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java 
(original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java Wed Feb 
 6 14:33:04 2008
@@ -602,6 +602,36 @@
 
     //-----------------------------------------------------------------------
     /**
+     * Convert the specified CharSequence to an input stream, encoded as bytes
+     * using the default character encoding of the platform.
+     *
+     * @param input the CharSequence to convert
+     * @return an input stream
+     * @since IO 2.0
+     */
+    public static InputStream toInputStream(CharSequence input) {
+        return toInputStream(input.toString());
+    }
+
+    /**
+     * Convert the specified CharSequence to an input stream, encoded as bytes
+     * 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 CharSequence to convert
+     * @param encoding the encoding to use, null means platform default
+     * @throws IOException if the encoding is invalid
+     * @return an input stream
+     * @since IO 2.0
+     */
+    public static InputStream toInputStream(CharSequence input, String 
encoding) throws IOException {
+        return toInputStream(input.toString(), encoding);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
      * Convert the specified string to an input stream, encoded as bytes
      * using the default character encoding of the platform.
      *
@@ -767,6 +797,66 @@
         }
     }
 
+    // write CharSequence
+    //-----------------------------------------------------------------------
+    /**
+     * Writes chars from a <code>CharSequence</code> to a <code>Writer</code>.
+     * 
+     * @param data  the <code>CharSequence</code> to write, null ignored
+     * @param output  the <code>Writer</code> to write to
+     * @throws NullPointerException if output is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 2.0
+     */
+    public static void write(CharSequence data, Writer output) throws 
IOException {
+        if (data != null) {
+            write(data.toString(), output);
+        }
+    }
+
+    /**
+     * Writes chars from a <code>CharSequence</code> to bytes on an
+     * <code>OutputStream</code> using the default character encoding of the
+     * platform.
+     * <p>
+     * This method uses [EMAIL PROTECTED] String#getBytes()}.
+     * 
+     * @param data  the <code>CharSequence</code> to write, null ignored
+     * @param output  the <code>OutputStream</code> to write to
+     * @throws NullPointerException if output is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 2.0
+     */
+    public static void write(CharSequence data, OutputStream output)
+            throws IOException {
+        if (data != null) {
+            write(data.toString(), output);
+        }
+    }
+
+    /**
+     * Writes chars from a <code>CharSequence</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 [EMAIL PROTECTED] String#getBytes(String)}.
+     * 
+     * @param data  the <code>CharSequence</code> to write, null ignored
+     * @param output  the <code>OutputStream</code> to write to
+     * @param encoding  the encoding to use, null means platform default
+     * @throws NullPointerException if output is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 2.0
+     */
+    public static void write(CharSequence data, OutputStream output, String 
encoding)
+            throws IOException {
+        if (data != null) {
+            write(data.toString(), output, encoding);
+        }
+    }
+
     // write String
     //-----------------------------------------------------------------------
     /**
@@ -841,6 +931,7 @@
      * @throws NullPointerException if output is null
      * @throws IOException if an I/O error occurs
      * @since Commons IO 1.1
+     * @deprecated replaced by write(CharSequence, Writer)
      */
     public static void write(StringBuffer data, Writer output)
             throws IOException {
@@ -861,6 +952,7 @@
      * @throws NullPointerException if output is null
      * @throws IOException if an I/O error occurs
      * @since Commons IO 1.1
+     * @deprecated replaced by write(CharSequence, OutputStream)
      */
     public static void write(StringBuffer data, OutputStream output)
             throws IOException {
@@ -884,6 +976,7 @@
      * @throws NullPointerException if output is null
      * @throws IOException if an I/O error occurs
      * @since Commons IO 1.1
+     * @deprecated replaced by write(CharSequence, OutputStream, String)
      */
     public static void write(StringBuffer data, OutputStream output,
             String encoding) throws IOException {

Modified: 
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=619188&r1=619187&r2=619188&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java 
(original)
+++ 
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java 
Wed Feb  6 14:33:04 2008
@@ -1108,6 +1108,20 @@
         assertEqualContent(text, file);
     }
 
+    public void testWriteCharSequence1() throws Exception {
+        File file = new File(getTestDirectory(), "write.txt");
+        FileUtils.write(file, "Hello /u1234", "UTF8");
+        byte[] text = "Hello /u1234".getBytes("UTF8");
+        assertEqualContent(text, file);
+    }
+
+    public void testWriteCharSequence2() throws Exception {
+        File file = new File(getTestDirectory(), "write.txt");
+        FileUtils.write(file, "Hello /u1234", null);
+        byte[] text = "Hello /u1234".getBytes();
+        assertEqualContent(text, file);
+    }
+
     public void testWriteByteArrayToFile() throws Exception {
         File file = new File(getTestDirectory(), "write.obj");
         byte[] data = new byte[] {11, 21, 31};

Modified: 
commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java?rev=619188&r1=619187&r2=619188&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java 
(original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java 
Wed Feb  6 14:33:04 2008
@@ -277,6 +277,26 @@
     }
 
     /**
+     * Test for [EMAIL PROTECTED] IOUtils#toInputStream(CharSequence)} and 
[EMAIL PROTECTED] IOUtils#toInputStream(CharSequence, String)}.
+     * Note, this test utilizes on [EMAIL PROTECTED] 
IOUtils#toByteArray(java.io.InputStream)} and so relies on
+     * [EMAIL PROTECTED] #testInputStreamToByteArray()} to ensure this method 
functions correctly.
+     *
+     * @throws Exception on error
+     */
+    public void testCharSequenceToInputStream() throws Exception {
+        CharSequence csq = new StringBuilder("Abc123Xyz!");
+        InputStream inStream = IOUtils.toInputStream(csq);
+        byte[] bytes = IOUtils.toByteArray(inStream);
+        assertEqualContent(csq.toString().getBytes(), bytes);
+        inStream = IOUtils.toInputStream(csq, null);
+        bytes = IOUtils.toByteArray(inStream);
+        assertEqualContent(csq.toString().getBytes(), bytes);
+        inStream = IOUtils.toInputStream(csq, "UTF-8");
+        bytes = IOUtils.toByteArray(inStream);
+        assertEqualContent(csq.toString().getBytes("UTF-8"), bytes);
+    }
+
+    /**
      * Test for [EMAIL PROTECTED] IOUtils#toInputStream(String)} and [EMAIL 
PROTECTED] IOUtils#toInputStream(String, String)}.
      * Note, this test utilizes on [EMAIL PROTECTED] 
IOUtils#toByteArray(java.io.InputStream)} and so relies on
      * [EMAIL PROTECTED] #testInputStreamToByteArray()} to ensure this method 
functions correctly.

Modified: 
commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java?rev=619188&r1=619187&r2=619188&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
 (original)
+++ 
commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
 Wed Feb  6 14:33:04 2008
@@ -183,7 +183,124 @@
         assertEquals("Sizes differ", inData.length, baout.size());
         assertTrue("Content differs", Arrays.equals(inData, 
baout.toByteArray()));
     }
+    //-----------------------------------------------------------------------
+    public void testWrite_charSequenceToOutputStream() throws Exception {
+        CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
+        
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new 
YellOnFlushAndCloseOutputStream(baout, true, true);
+        
+        IOUtils.write(csq, out);
+        out.off();
+        out.flush();
+        
+        assertEquals("Sizes differ", inData.length, baout.size());
+        assertTrue("Content differs", Arrays.equals(inData, 
baout.toByteArray()));
+    }
 
+    public void testWrite_charSequenceToOutputStream_nullData() throws 
Exception {
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new 
YellOnFlushAndCloseOutputStream(baout, true, true);
+        
+        IOUtils.write((CharSequence) null, out);
+        out.off();
+        out.flush();
+
+        assertEquals("Sizes differ", 0, baout.size());
+    }
+
+    public void testWrite_charSequenceToOutputStream_nullStream() throws 
Exception {
+        CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
+        try {
+            IOUtils.write(csq, (OutputStream) null);
+            fail();
+        } catch (NullPointerException ex) {}
+    }
+
+    //-----------------------------------------------------------------------
+    public void testWrite_charSequenceToOutputStream_Encoding() throws 
Exception {
+        CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
+        
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new 
YellOnFlushAndCloseOutputStream(baout, true, true);
+
+        IOUtils.write(csq, out, "UTF16");
+        out.off();
+        out.flush();
+        
+        byte[] bytes = baout.toByteArray();
+        bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
+        assertTrue("Content differs", Arrays.equals(inData, bytes));
+    }
+
+    public void testWrite_charSequenceToOutputStream_Encoding_nullData() 
throws Exception {
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new 
YellOnFlushAndCloseOutputStream(baout, true, true);
+        
+        IOUtils.write((CharSequence) null, out);
+        out.off();
+        out.flush();
+
+        assertEquals("Sizes differ", 0, baout.size());
+    }
+
+    public void testWrite_charSequenceToOutputStream_Encoding_nullStream() 
throws Exception {
+        CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
+        try {
+            IOUtils.write(csq, (OutputStream) null);
+            fail();
+        } catch (NullPointerException ex) {}
+    }
+
+    public void testWrite_charSequenceToOutputStream_nullEncoding() throws 
Exception {
+        CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
+        
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new 
YellOnFlushAndCloseOutputStream(baout, true, true);
+
+        IOUtils.write(csq, out, null);
+        out.off();
+        out.flush();
+
+        assertEquals("Sizes differ", inData.length, baout.size());
+        assertTrue("Content differs", Arrays.equals(inData, 
baout.toByteArray()));
+    }
+
+    //-----------------------------------------------------------------------
+    public void testWrite_charSequenceToWriter() throws Exception {
+        CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
+
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new 
YellOnFlushAndCloseOutputStream(baout, true, true);
+        Writer writer = new OutputStreamWriter(baout, "US-ASCII");
+
+        IOUtils.write(csq, writer);
+        out.off();
+        writer.flush();
+
+        assertEquals("Sizes differ", inData.length, baout.size());
+        assertTrue("Content differs", Arrays.equals(inData, 
baout.toByteArray()));
+    }
+
+    public void testWrite_charSequenceToWriter_Encoding_nullData() throws 
Exception {
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new 
YellOnFlushAndCloseOutputStream(baout, true, true);
+        Writer writer = new OutputStreamWriter(baout, "US-ASCII");
+        
+        IOUtils.write((CharSequence) null, writer);
+        out.off();
+        writer.flush();
+
+        assertEquals("Sizes differ", 0, baout.size());
+    }
+
+    public void testWrite_charSequenceToWriter_Encoding_nullStream() throws 
Exception {
+        CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
+        try {
+            IOUtils.write(csq, (Writer) null);
+            fail();
+        } catch (NullPointerException ex) {}
+    }
     //-----------------------------------------------------------------------
     public void testWrite_stringToOutputStream() throws Exception {
         String str = new String(inData, "US-ASCII");


Reply via email to