commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r279170 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/IOUtils.java src/test/org/apache/commons/io/IOUtilsTestCase.java src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
Date Tue, 06 Sep 2005 23:00:03 GMT
Author: scolebourne
Date: Tue Sep  6 15:59:54 2005
New Revision: 279170

URL: http://svn.apache.org/viewcvs?rev=279170&view=rev
Log:
Add IOUtils.writeLines

Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=279170&r1=279169&r2=279170&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Tue Sep  6 15:59:54 2005
@@ -58,6 +58,14 @@
 
 Enhancements from 1.0
 ---------------------
+- FilenameUtils - new class
+    A static utility class for working with filenames
+    Seeks to ease the pain of developing on Windows and deploying on Unix
+
+- FileSystemUtils - new class
+    A static utility class for working with file systems
+    Provides one method at present, to get the free space on the filing system
+
 - IOUtils - toByteArray(Reader,encoding)
     Handles encodings when reading to a byte array
 
@@ -75,6 +83,11 @@
           - toInputStream(String,encoding)
     Creates an input stream that uses the string as a source of data
 
+- IOUtils - writeLines(Collection,lineEnding,OutputStream)
+          - writeLines(Collection,lineEnding,OutputStream,encoding)
+          - writeLines(Collection,lineEnding,Writer)
+    Writes a collection to a file line by line
+
 - IOUtils - write(...)
     Write data to a stream/writer (moved from CopyUtils with better null handling)
 
@@ -98,14 +111,6 @@
 
 - FileUtils - readLines(File,encoding)
     Reads a file line by line into a List of Strings
-
-- FilenameUtils - new class
-    A static utility class for working with filenames
-    Seeks to ease the pain of developing on Windows and deploying on Unix
-
-- FileSystemUtils - new class
-    A static utility class for working with file systems
-    Provides one method at present, to get the free space on the filing system
 
 - ConditionalFileFilter - new interface
     Defines the behaviour of list based filters

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java?rev=279170&r1=279169&r2=279170&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java Tue Sep  6
15:59:54 2005
@@ -17,17 +17,22 @@
 
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.CharArrayWriter;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -77,6 +82,38 @@
     // or return one of them.
 
     /**
+     * The Unix directory separator character.
+     */
+    public static final char DIR_SEPARATOR_UNIX = '/';
+    /**
+     * The Windows directory separator character.
+     */
+    public static final char DIR_SEPARATOR_WINDOWS = '\\';
+    /**
+     * The system directory separator character.
+     */
+    public static final char DIR_SEPARATOR = File.separatorChar;
+    /**
+     * The Unix line separator string.
+     */
+    public static final String LINE_SEPARATOR_UNIX = "\n";
+    /**
+     * The Windows line separator string.
+     */
+    public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
+    /**
+     * The system line separator string.
+     */
+    public static final String LINE_SEPARATOR;
+    static {
+        // avoid security issues
+        StringWriter buf = new StringWriter(4);
+        PrintWriter out = new PrintWriter(buf);
+        out.println();
+        LINE_SEPARATOR = buf.toString();
+    }
+
+    /**
      * The default buffer size to use.
      */
     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
@@ -761,6 +798,102 @@
             } else {
                 output.write(data.toString().getBytes(encoding));
             }
+        }
+    }
+
+    // writeLines
+    //-----------------------------------------------------------------------
+    /**
+     * Writes the <code>toString()</code> value of each item in a collection
to
+     * an <code>OutputStream</code> line by line, using the default character
+     * encoding of the platform and the specified line ending.
+     *
+     * @param lines  the lines to write, null entries produce blank lines
+     * @param lineEnding  the line separator to use, null is system default
+     * @param output  the <code>OutputStream</code> to write to, not null, not
closed
+     * @throws NullPointerException if the output is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 1.1
+     */
+    public static void writeLines(Collection lines, String lineEnding,
+            OutputStream output) throws IOException {
+        if (lines == null) {
+            return;
+        }
+        if (lineEnding == null) {
+            lineEnding = LINE_SEPARATOR;
+        }
+        for (Iterator it = lines.iterator(); it.hasNext(); ) {
+            Object line = it.next();
+            if (line != null) {
+                output.write(line.toString().getBytes());
+            }
+            output.write(lineEnding.getBytes());
+        }
+    }
+
+    /**
+     * Writes the <code>toString()</code> value of each item in a collection
to
+     * an <code>OutputStream</code> line by line, using the specified character
+     * encoding and the specified line ending.
+     * <p>
+     * Character encoding names can be found at
+     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+     *
+     * @param lines  the lines to write, null entries produce blank lines
+     * @param lineEnding  the line separator to use, null is system default
+     * @param output  the <code>OutputStream</code> to write to, not null, not
closed
+     * @param encoding  the encoding to use, null means platform default
+     * @throws NullPointerException if the output is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 1.1
+     */
+    public static void writeLines(Collection lines, String lineEnding,
+            OutputStream output, String encoding) throws IOException {
+        if (encoding == null) {
+            writeLines(lines, lineEnding, output);
+        } else {
+            if (lines == null) {
+                return;
+            }
+            if (lineEnding == null) {
+                lineEnding = LINE_SEPARATOR;
+            }
+            for (Iterator it = lines.iterator(); it.hasNext(); ) {
+                Object line = it.next();
+                if (line != null) {
+                    output.write(line.toString().getBytes(encoding));
+                }
+                output.write(lineEnding.getBytes(encoding));
+            }
+        }
+    }
+
+    /**
+     * Writes the <code>toString()</code> value of each item in a collection
to
+     * a <code>Writer</code> line by line, using the specified line ending.
+     *
+     * @param lines  the lines to write, null entries produce blank lines
+     * @param lineEnding  the line separator to use, null is system default
+     * @param writer  the <code>Writer</code> to write to, not null, not closed
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 1.1
+     */
+    public static void writeLines(Collection lines, String lineEnding,
+            Writer writer) throws IOException {
+        if (lines == null) {
+            return;
+        }
+        if (lineEnding == null) {
+            lineEnding = LINE_SEPARATOR;
+        }
+        for (Iterator it = lines.iterator(); it.hasNext(); ) {
+            Object line = it.next();
+            if (line != null) {
+                writer.write(line.toString());
+            }
+            writer.write(lineEnding);
         }
     }
 

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java?rev=279170&r1=279169&r2=279170&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java Tue
Sep  6 15:59:54 2005
@@ -49,6 +49,9 @@
  * @author Ian Springer
  */
 public class IOUtilsTestCase extends FileBasedTestCase {
+    
+    /** Determine if this is windows. */
+    private static final boolean WINDOWS = (File.separatorChar == '\\');
     /*
      * Note: this is not particularly beautiful code. A better way to check for
      * flush and close status would be to implement "trojan horse" wrapper
@@ -93,6 +96,22 @@
         super( name );
     }
 
+    //-----------------------------------------------------------------------
+    public void testConstants() throws Exception {
+        assertEquals('/', IOUtils.DIR_SEPARATOR_UNIX);
+        assertEquals('\\', IOUtils.DIR_SEPARATOR_WINDOWS);
+        assertEquals("\n", IOUtils.LINE_SEPARATOR_UNIX);
+        assertEquals("\r\n", IOUtils.LINE_SEPARATOR_WINDOWS);
+        if (WINDOWS) {
+            assertEquals('\\', IOUtils.DIR_SEPARATOR);
+            assertEquals("\r\n", IOUtils.LINE_SEPARATOR);
+        } else {
+            assertEquals('/', IOUtils.DIR_SEPARATOR);
+            assertEquals("\n", IOUtils.LINE_SEPARATOR);
+        }
+    }
+
+    //-----------------------------------------------------------------------
     /** Assert that the contents of two byte arrays are the same. */
     private void assertEqualContent( byte[] b0, byte[] b1 )
         throws IOException

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java?rev=279170&r1=279169&r2=279170&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
Tue Sep  6 15:59:54 2005
@@ -15,10 +15,18 @@
  */
 package org.apache.commons.io;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.util.Arrays;
+import java.util.List;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -40,13 +48,6 @@
  */
 public class IOUtilsWriteTestCase extends FileBasedTestCase {
 
-    /*
-     * NOTE this is not particularly beautiful code. A better way to check for
-     * flush and close status would be to implement "trojan horse" wrapper
-     * implementations of the various stream classes, which set a flag when
-     * relevant methods are called. (JT)
-     */
-
     private static final int FILE_SIZE = 1024 * 4 + 1;
 
 
@@ -359,8 +360,6 @@
     }
 
     public void testWrite_charArrayToOutputStream_Encoding_nullData() throws Exception {
-        String str = new String(inData, "US-ASCII");
-        
         ByteArrayOutputStream baout = new ByteArrayOutputStream();
         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
true, true);
         
@@ -425,6 +424,192 @@
         String str = new String(inData, "US-ASCII");
         try {
             IOUtils.write(str.toCharArray(), (Writer) null);
+            fail();
+        } catch (NullPointerException ex) {}
+    }
+
+    //-----------------------------------------------------------------------
+    public void testWriteLines_OutputStream() throws Exception {
+        Object[] data = new Object[] {
+            "hello", new StringBuffer("world"), "", "this is", null, "some text"};
+        List list = Arrays.asList(data);
+        
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
false, true);
+        
+        IOUtils.writeLines(list, "*", out);
+        
+        out.off();
+        out.flush();
+        
+        String expected = "hello*world**this is**some text*";
+        String actual = baout.toString();
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_OutputStream_nullData() throws Exception {
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
false, true);
+        
+        IOUtils.writeLines((List) null, "*", out);
+        out.off();
+        out.flush();
+        
+        assertEquals("Sizes differ", 0, baout.size());
+    }
+
+    public void testWriteLines_OutputStream_nullSeparator() throws Exception {
+        Object[] data = new Object[] {"hello", "world"};
+        List list = Arrays.asList(data);
+            
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
false, true);
+        
+        IOUtils.writeLines(list, (String) null, out);
+        out.off();
+        out.flush();
+        
+        String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
+        String actual = baout.toString();
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_OutputStream_nullStream() throws Exception {
+        Object[] data = new Object[] {"hello", "world"};
+        List list = Arrays.asList(data);
+        try {
+            IOUtils.writeLines(list, "*", (OutputStream) null);
+            fail();
+        } catch (NullPointerException ex) {}
+    }
+
+    //-----------------------------------------------------------------------
+    public void testWriteLines_OutputStream_Encoding() throws Exception {
+        Object[] data = new Object[] {
+            "hello\u8364", new StringBuffer("world"), "", "this is", null, "some text"};
+        List list = Arrays.asList(data);
+        
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
false, true);
+        
+        IOUtils.writeLines(list, "*", out, "UTF-8");
+        
+        out.off();
+        out.flush();
+        
+        String expected = "hello\u8364*world**this is**some text*";
+        String actual = baout.toString("UTF-8");
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_OutputStream_Encoding_nullData() throws Exception {
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
false, true);
+        
+        IOUtils.writeLines((List) null, "*", out, "US-ASCII");
+        out.off();
+        out.flush();
+        
+        assertEquals("Sizes differ", 0, baout.size());
+    }
+
+    public void testWriteLines_OutputStream_Encoding_nullSeparator() throws Exception {
+        Object[] data = new Object[] {"hello", "world"};
+        List list = Arrays.asList(data);
+            
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
false, true);
+        
+        IOUtils.writeLines(list, (String) null, out, "US-ASCII");
+        out.off();
+        out.flush();
+        
+        String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
+        String actual = baout.toString();
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_OutputStream_Encoding_nullStream() throws Exception {
+        Object[] data = new Object[] {"hello", "world"};
+        List list = Arrays.asList(data);
+        try {
+            IOUtils.writeLines(list, "*", (OutputStream) null, "US-ASCII");
+            fail();
+        } catch (NullPointerException ex) {}
+    }
+
+    public void testWriteLines_OutputStream_Encoding_nullEncoding() throws Exception {
+        Object[] data = new Object[] {
+            "hello", new StringBuffer("world"), "", "this is", null, "some text"};
+        List list = Arrays.asList(data);
+        
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
false, true);
+        
+        IOUtils.writeLines(list, "*", out, null);
+        
+        out.off();
+        out.flush();
+        
+        String expected = "hello*world**this is**some text*";
+        String actual = baout.toString();
+        assertEquals(expected, actual);
+    }
+
+    //-----------------------------------------------------------------------
+    public void testWriteLines_Writer() throws Exception {
+        Object[] data = new Object[] {
+            "hello", new StringBuffer("world"), "", "this is", null, "some text"};
+        List list = Arrays.asList(data);
+        
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
true, true);
+        Writer writer = new OutputStreamWriter(baout, "US-ASCII");
+        
+        IOUtils.writeLines(list, "*", writer);
+        
+        out.off();
+        writer.flush();
+        
+        String expected = "hello*world**this is**some text*";
+        String actual = baout.toString();
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_Writer_nullData() throws Exception {
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
true, true);
+        Writer writer = new OutputStreamWriter(baout, "US-ASCII");
+        
+        IOUtils.writeLines((List) null, "*", writer);
+        out.off();
+        writer.flush();
+        
+        assertEquals("Sizes differ", 0, baout.size());
+    }
+
+    public void testWriteLines_Writer_nullSeparator() throws Exception {
+        Object[] data = new Object[] {"hello", "world"};
+        List list = Arrays.asList(data);
+            
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout,
true, true);
+        Writer writer = new OutputStreamWriter(baout, "US-ASCII");
+        
+        IOUtils.writeLines(list, (String) null, writer);
+        out.off();
+        writer.flush();
+        
+        String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
+        String actual = baout.toString();
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_Writer_nullStream() throws Exception {
+        Object[] data = new Object[] {"hello", "world"};
+        List list = Arrays.asList(data);
+        try {
+            IOUtils.writeLines(list, "*", (Writer) null);
             fail();
         } catch (NullPointerException ex) {}
     }



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


Mime
View raw message