Ask for review: java.io.Ouputs

Rémi Forax forax at univ-mlv.fr
Fri Nov 21 01:01:52 PST 2008


Hi alan, hi all,
following your comments :
- ensureCharsetIsSupported now calls ensureCharsetIsSupported
- all writeLines use an Iterable instead of a List.

I wait your agrement before pushing the change.
cheers,
Rémi

diff --git a/src/share/classes/java/io/Outputs.java 
b/src/share/classes/java/io/Outputs.java
--- a/src/share/classes/java/io/Outputs.java
+++ b/src/share/classes/java/io/Outputs.java
@@ -33,7 +33,6 @@ import java.nio.channels.Channels;
 import java.nio.channels.Channels;
 import java.nio.channels.WritableByteChannel;
 import java.util.Arrays;
-import java.util.List;
 
 /**
  * {@note experimental}
@@ -52,10 +51,15 @@ public final class Outputs {
 
     // checks that charset is supported
     private static void ensureCharsetIsSupported(String csn) {
-        if (csn == null)
-            throw new NullPointerException("'csn' is null");
+        ensureCharsetIsNotNull(csn);
         if (!Charset.isSupported(csn))
             throw new UnsupportedCharsetException(csn);
+    }
+   
+    // checks that charset is not null
+    private static void ensureCharsetIsNotNull(Object charset) {
+        if (charset == null)
+            throw new NullPointerException("charset is null");
     }
 
     /**
@@ -105,6 +109,40 @@ public final class Outputs {
      *          The file
      * @param   lines
      *          The list of lines to write (in order)
+     * @param   charset
+     *          The charset to be used
+     *
+     * @throws  java.nio.charset.UnsupportedCharsetException
+     *          If no support for the named charset is available
+     *          in this instance of the Java virtual machine
+     * @throws  java.nio.charset.UnmappableCharacterException
+     *          Where a line contains a character that cannot be mapped 
to an
+     *          output byte sequence
+     * @throws  IOException
+     *          If an I/O error occurs
+     */
+    public static void writeLines(FileRef file, Iterable<? extends 
CharSequence> lines, Charset charset)
+        throws IOException
+    {
+        ensureCharsetIsNotNull(charset);
+        WritableByteChannel wbc = file.newByteChannel(WRITE, CREATE, 
TRUNCATE_EXISTING);
+        BufferedWriter writer = new 
BufferedWriter(Channels.newWriter(wbc, charset.newEncoder(), -1));
+        try {
+            implWriteLines(writer, lines);
+        } finally {
+            writer.close();
+        }
+    }
+   
+    /**
+     * Writes the given lines of text to the specified file. The 
characters in
+     * each line are encoded into bytes using the specified charset. 
When all
+     * lines have been written, or an I/O error occurs, then the file 
is closed.
+     *
+     * @param   file
+     *          The file
+     * @param   lines
+     *          The list of lines to write (in order)
      * @param   csn
      *          The name of the charset to be used
      *
@@ -117,7 +155,7 @@ public final class Outputs {
      * @throws  IOException
      *          If an I/O error occurs
      */
-    public static void writeLines(FileRef file, List<String> lines, 
String csn)
+    public static void writeLines(FileRef file, Iterable<? extends 
CharSequence> lines, String csn)
         throws IOException
     {
         ensureCharsetIsSupported(csn);
@@ -147,10 +185,10 @@ public final class Outputs {
      * @throws  IOException
      *          If an I/O error occurs
      */
-    public static void writeLines(FileRef file, List<String> lines)
-        throws IOException
-    {
-        writeLines(file, lines, Charset.defaultCharset().name());
+    public static void writeLines(FileRef file, Iterable<? extends 
CharSequence> lines)
+        throws IOException
+    {
+        writeLines(file, lines, Charset.defaultCharset());
     }
 
     /**
@@ -170,10 +208,10 @@ public final class Outputs {
      * @throws  IOException
      *          If an I/O error occurs
      */
-    public static void writeLines(FileRef file, String... lines)
-        throws IOException
-    {
-        writeLines(file, Arrays.asList(lines), 
Charset.defaultCharset().name());
+    public static void writeLines(FileRef file, CharSequence... lines)
+        throws IOException
+    {
+        writeLines(file, Arrays.asList(lines), Charset.defaultCharset());
     }
 
     /**
@@ -222,6 +260,40 @@ public final class Outputs {
      *          The file
      * @param   lines
      *          The list of lines to write (in order)
+     * @param   charset
+     *          The charset to be used
+     *
+     * @throws  java.nio.charset.UnsupportedCharsetException
+     *          If no support for the named charset is available
+     *          in this instance of the Java virtual machine
+     * @throws  java.nio.charset.UnmappableCharacterException
+     *          Where a line contains a character that cannot be mapped 
to an
+     *          output byte sequence
+     * @throws  IOException
+     *          If an I/O error occurs
+     */
+    public static void writeLines(File file, Iterable<? extends 
CharSequence> lines, Charset charset)
+        throws IOException
+    {
+        ensureCharsetIsNotNull(charset);
+        FileOutputStream out = new FileOutputStream(file);
+        BufferedWriter writer = new BufferedWriter(new 
OutputStreamWriter(out, charset));
+        try {
+            implWriteLines(writer, lines);
+        } finally {
+            writer.close();
+        }
+    }
+   
+    /**
+     * Writes the given lines of text to the specified file. The 
characters in
+     * each line are encoded into bytes using the specified charset. 
When all
+     * lines have been written, or an I/O error occurs, then the file 
is closed.
+     *
+     * @param   file
+     *          The file
+     * @param   lines
+     *          The list of lines to write (in order)
      * @param   csn
      *          The name of the charset to be used
      *
@@ -234,7 +306,7 @@ public final class Outputs {
      * @throws  IOException
      *          If an I/O error occurs
      */
-    public static void writeLines(File file, List<String> lines, String 
csn)
+    public static void writeLines(File file, Iterable<? extends 
CharSequence> lines, String csn)
         throws IOException
     {
         ensureCharsetIsSupported(csn);
@@ -264,10 +336,10 @@ public final class Outputs {
      * @throws  IOException
      *          If an I/O error occurs
      */
-    public static void writeLines(File file, List<String> lines)
-        throws IOException
-    {
-        writeLines(file, lines, Charset.defaultCharset().name());
+    public static void writeLines(File file, Iterable<? extends 
CharSequence> lines)
+        throws IOException
+    {
+        writeLines(file, lines, Charset.defaultCharset());
     }
 
     /**
@@ -287,10 +359,10 @@ public final class Outputs {
      * @throws  IOException
      *          If an I/O error occurs
      */
-    public static void writeLines(File file, String... lines)
-        throws IOException
-    {
-        writeLines(file, Arrays.asList(lines), 
Charset.defaultCharset().name());
+    public static void writeLines(File file, CharSequence... lines)
+        throws IOException
+    {
+        writeLines(file, Arrays.asList(lines), Charset.defaultCharset());
     }
 
     /**
@@ -301,18 +373,48 @@ public final class Outputs {
      *          The output stream
      * @param   lines
      *          The list of lines to write (in order)
+     * @param   charset
+     *          The charset to be used
+     *
+     * @throws  java.nio.charset.UnmappableCharacterException
+     *          Where a line contains a character that cannot be mapped 
to an
+     *          output byte sequence
+     * @throws  IOException
+     *          If an I/O error occurs
+     */
+    public static void writeLines(OutputStream out, Iterable<? extends 
CharSequence> lines, Charset charset)
+        throws IOException
+    {
+        ensureCharsetIsNotNull(charset);
+        BufferedWriter writer = new BufferedWriter(new 
OutputStreamWriter(out, charset));
+        implWriteLines(writer, lines);
+        writer.flush();
+    }
+   
+    /**
+     * Writes the given lines of text to the specified output stream. The
+     * characters in each line are encoded into bytes using the 
specified charset.
+     *
+     * @param   out
+     *          The output stream
+     * @param   lines
+     *          The list of lines to write (in order)
      * @param   csn
      *          The name of the charset to be used
      *
-     * @throws  java.nio.charset.UnmappableCharacterException
-     *          Where a line contains a character that cannot be mapped 
to an
-     *          output byte sequence
-     * @throws  IOException
-     *          If an I/O error occurs
-     */
-    public static void writeLines(OutputStream out, List<String> lines, 
String csn)
-        throws IOException
-    {
+     * @throws  java.nio.charset.UnsupportedCharsetException
+     *          If no support for the named charset is available
+     *          in this instance of the Java virtual machine
+     * @throws  java.nio.charset.UnmappableCharacterException
+     *          Where a line contains a character that cannot be mapped 
to an
+     *          output byte sequence
+     * @throws  IOException
+     *          If an I/O error occurs
+     */
+    public static void writeLines(OutputStream out, Iterable<? extends 
CharSequence> lines, String csn)
+        throws IOException
+    {
+        ensureCharsetIsSupported(csn);
         BufferedWriter writer = new BufferedWriter(new 
OutputStreamWriter(out, csn));
         implWriteLines(writer, lines);
         writer.flush();
@@ -334,17 +436,17 @@ public final class Outputs {
      * @throws  IOException
      *          If an I/O error occurs
      */
-    public static void writeLines(OutputStream out, List<String> lines)
-        throws IOException
-    {
-        writeLines(out, lines, Charset.defaultCharset().name());
-    }
-
-    private static void implWriteLines(BufferedWriter writer, 
List<String> lines)
-        throws IOException
-    {
-        for (String line: lines) {
-            writer.write(line);
+    public static void writeLines(OutputStream out, Iterable<? extends 
CharSequence> lines)
+        throws IOException
+    {
+        writeLines(out, lines, Charset.defaultCharset());
+    }
+
+    private static void implWriteLines(BufferedWriter writer, 
Iterable<? extends CharSequence> lines)
+        throws IOException
+    {
+        for (CharSequence line: lines) {
+            writer.write(line.toString());
             writer.newLine();
         }
     }




More information about the nio-dev mailing list