Ask for review: java.io.Ouputs

Xueming Shen Xueming.Shen at Sun.COM
Fri Nov 21 16:15:42 PST 2008


Comments not necessary to the updated version.

(1) The UnsupportedCharsetException should not be "extected" if we pass 
in a Charset, you got one
already:-)

(2)Is it really necessary to check "Charset.isSupproted()"? The csn will 
be checked when the
Charset is created later anyway, why do lookup twice. Yes, understood it 
can catch the problem
1-2 step(s) earlier, I double you want to pay that price.

(3) The UnmappableCharacterException is tricky the current StreamEncoder 
implementation
always sets to use Action.REPLACE if you pass a charset (for "old 
java.io" OutputStreamWriter
case) but goes whatever the "current" setting of the encoder, which will 
be the default "report" if the
action is NOT set to something else, so you might get uce (from all nio 
implementation).
OutputStreamWriter specifically "defines" the behavior when malfromed or 
unmappable situation
occurs (replace, if not the constructor that uses CharsetEncoder), the 
question is what we want to
do for this utility class, we kindly of mix  the "old" io and nio:-) One 
alternative is to explicitly use
CharsetEncoder for OutputStreamWriter.  If you specify the UCE, you 
might also want to say
something about malformed as well.


> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 21 Nov 2008 10:01:52 +0100
> From: R?mi Forax <forax at univ-mlv.fr>
> Subject: Ask for review: java.io.Ouputs
> To: nio-dev at openjdk.java.net, Alan Bateman <Alan.Bateman at Sun.COM>
> Message-ID: <49267900.90409 at univ-mlv.fr>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> 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